2016-04-27 36 views
0

我正在製作帶有振動電機和簡單按鈕的arduino作爲界面的秒錶。C++/C Arduino秒錶代碼檢查

我正在接受這個項目,因爲它不會工作,我一直在測試我的uno,迄今爲止沒有成功,我想知道如果有人能給我一個快速的小跑,看看他們是否能夠發現我忽視的任何重大問題。

沒有錯誤的代碼,我認爲這可能是一個邏輯錯誤代表我甚至可能是我的董事會的錯誤,但我懷疑這一點!

void setup() { 
    Serial.begin(9600); 
} 
int lengthOf(int i) 
{ 
    if (i < 0){i = -i;} 
    if (i < 10){ return 1;} 
    if (i < 100){ return 2;} 
    if (i < 1000){ return 3;} 
    if (i < 10000){ return 4;} 
    if (i < 100000){ return 5;} 
    if (i < 1000000){ return 6;} 
    if (i < 10000000){ return 7;} 
    if (i < 100000000){ return 8;} 
    if (i < 1000000000){ return 9;} 
    return 10; 
} 
void loop() { 

    int ButtonSwitch = 4; 
    pinMode(4, INPUT); 
    int motor = 5; 
    int timerA = 0; int timed; 
    bool checker = false; //checker acts to see if the current state is timing/counting 
    bool shown; //Shown acts as a check to show if the time has already been shown 

    if (analogRead(ButtonSwitch) == HIGH && checker == false)//When the button is pressed and the state is false then 
    { 
     checker = true;//sets checker to true, meaning the timing should begin 
     shown = false;//sets the shown variable to false so as to 
     timerA = 0;//reset timer to a 0 value 
    } 

    while (checker == true)//while the timer is active then do the following 
    { 
     timerA++;//Increment the timer 
     if (digitalRead(ButtonSwitch) == HIGH) 
     { 
      checker = false; 
      break; 
     } 
     delay(1000);//No needfor the simpleTimer library as I don't need to run any code inbetween each second 
    } 

    //Sets the timed to the real value of 
    timed = lengthOf(timerA);//Grabs the length of timerA (1223 would be 4) 
    int recTime[timed - 1]; //creates an array of the same length as the timer 


    //append int to chars and by extent an array 
    char str[timed]; 
    sprintf(str, "%d", timerA); 

    int numbers; 

    for (int i = 0; i < timed; i++) 
    { 
     recTime[i] = str[i] - '0';//This grabs STR which is an empty array of the length of the time then sets recTime to be the same 
    } 

    while ((analogRead(ButtonSwitch) == LOW) && (checker == false) && (shown == false))//Loop checks that the button is not pressed, the checker is false, and that the time has not been shown, 
    { 
     for (int i = 0; i < timed; i++)// 
     { 
      for (int o = 0; o < recTime[i]; o++) 
      { 
       digitalWrite(motor, HIGH);//Motor set to vibrate 
       delay(500);//1/2 second delay 
       digitalWrite(motor, LOW);//motor off 
       delay(300);//3/10 second delay 
      } 
      delay(3000);//3 second delay 
     } 
     shown = true; 
    } 
} 

如果有任何更多的信息,我可以提供,然後讓我知道,

任何幫助是極大的讚賞!

+0

當你說「它不會工作」時會出現什麼問題?你看到的任何錯誤? – fluter

+0

對不起,我應該指定 - 沒有任何類型的輸出,就我所知,代碼功能完全正常,但是當我使用電機和按鈕設置電路時,不會發生任何動作。 – user3068449

+0

這是沒有調試服務。而Arduino完全不是C,也不完全是C++。 – Olaf

回答

0

我已經通讀了您的代碼並提出了一些建議。首先,讓我們保持一致,在循環例程的頂部和底部附近使用digitalRead而不是analogRead。讓我們在安裝例程之前移動所有的變量聲明。 (您仍然需要初始化timerA,checker並顯示在循環頂部)此外,您只需將該按鈕的pinMode設置爲一次,以便我們將該行移至設置例程,而當我們處於它讓我們在pinMode命令中使用變量ButtonSwitch。例如

int ButtonSwitch = 4; 
int motor = 5; 
int timerA = 0; 
int timed; 
bool checker = false; 
bool shown; 

void setup() { 
    Serial.begin(9600); 
    pinMode(ButtonSwitch, INPUT); 
} 

但我認爲真正的問題可能與程序的邏輯流程有關。假設循環開始,檢查器是假的,按鈕沒有按下。當你按下按鈕時,檢查器被設置爲true。緊接着,在幾個時鐘週期內,輸入下一個塊(因爲檢查器爲真),並且如果仍然按下按鈕,則將檢查器設置爲假並退出塊。你在第一個塊和第二個塊之間釋放按鈕的機率幾乎爲零。所以你的程序只是將timerA設置爲0,然後遞增1並繼續。延遲(1000)永遠不會達到,並且timerA永遠不會超過1,recTime數組將始終定義爲recTime [0],並且str數組將始終定義爲str [1]。 (digitalRead(ButtonSwitch)== HIGH)if(digitalRead(ButtonSwitch)== LOW)在第二個塊中,while循環,我認爲你想要更改 if(digitalRead(ButtonSwitch)== HIGH)。

我認爲,如果你糾正這些問題,你會很快獲得git工作!

+0

太棒了,謝謝!我剛剛提出了一些你所建議的修改,那麼我應該在「while(checker == true)」的開頭添加延遲嗎?在我看來,這似乎有助於解決您發現的問題。 – user3068449

+0

它會給你時間釋放按鈕。但如果你的意圖是按下按鈕的長度,我認爲只是進行上面提到的改變(將「if(digitalRead(ButtonSwitch)== HIGH」)改爲「if(digitalRead(ButtonSwitch)== LOW)」應該這樣做: –

+0

啊,我明白了!現在進行這些更改,將會測試和更新。 – user3068449

0

在C中,當timed是編寫數字的十進制數字所需的字符數時,則應該爲終止'\0'分配一個字符。

char str[timed+1]; 

然後,我想知道你在做什麼與recTime值。實際上,無論何時對於值123,str將包含"123",即['1','2','3'],則retTime將包含[1,2,3]

+0

嗨Didier,我剛剛'timed'變化,謝謝你! recTime的原因是我不確定'1'是否可以作爲int來處理,所以我只是將它轉換成了case。你認爲這樣做毫無意義? – user3068449

+0

@ user3068449我想知道爲什麼在程序結束時的循環中,如果值爲123,那麼您希望使1循環,然後是2循環,然後是3循環? –

+0

好吧,那麼背後的思想是第一個是檢查是否是時間打開電機,我應該改變它爲一個if第二個是數字的長度(19292是5長),第三個是設置振動前一個循環的值的次數(第二個循環振動9次),這是否有意義? – user3068449