我正在製作帶有振動電機和簡單按鈕的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;
}
}
如果有任何更多的信息,我可以提供,然後讓我知道,
任何幫助是極大的讚賞!
當你說「它不會工作」時會出現什麼問題?你看到的任何錯誤? – fluter
對不起,我應該指定 - 沒有任何類型的輸出,就我所知,代碼功能完全正常,但是當我使用電機和按鈕設置電路時,不會發生任何動作。 – user3068449
這是沒有調試服務。而Arduino完全不是C,也不完全是C++。 – Olaf