請問如何讓arduino在指定時間內某按鈕有作用,時間到後失效
作用當analogRead(1)按下後,計時10秒按analogRead(0)按鈕才會開始累加,時間到後再按analogRead(0)不累加但目前狀況是程序一執行,還沒按下analogRead(1)按鈕就開始倒數了
arduino新手發問^^感謝指教!!
int number = 0;
int down = 0;
int on = 0;
long gametime = 10000; // 遊戲時間10秒 單位為 MS*1000=S
void loop() {
/*** 計數程序 ***/
unsigned long Time = millis();
if (analogRead(1) > 100) //當按鈕按下時,端口1會獲得高電位
on = 1; //此時將按下狀態置为“真”
if (on && Time < gametime) { // 當按下開始按鈕後,在時間內計分
on = 1; // 將on持續開啟
{
while (analogRead(0) > 100) { //當按鈕按下時,模擬端口0會獲得連續的高電位
down = 1; //此時將按下狀態置为“真”
}
if (down) { //判斷按鈕按下狀態,如果按下過就顯示數字並累加
down = 0;
if (++number == 10000) {
number = 0; //判斷從0到9999後歸0
}
}
setNumber(number); //顯示數字
}
}
setNumber(number); //顯示數字
}
const int buttonPin0 =A0;//A0和A1需要外部下拉
const int buttonPin1 =A1;
int b1State = 0;
long previousMillis = 0;
int num = 0;
int number = 0;
long interval = 100;
void setup()
{
Serial.begin(9600);
}
void loop()
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval)
{
previousMillis = currentMillis;
if(analogRead(buttonPin1) > 1000) //A1被按下(A1为高电平)
{
b1State = 1;
num++;
}
else
{
b1State = 0;
num = 0;
}
if((b1State == 1)&&(num < 100))//如果A1被按下,并且时间没有超过100*100ms,
if(analogRead(buttonPin0) > 1000) //A0被按下(A0为高电平)
number++; //number 累加
if(number > 9999)
number = 0;
Serial.println(number);
}
}
问题出在这句:
if (analogRead(1) > 100) //當按鈕按下時,端口1會獲得高電位
后面少了个{。 fish6823 发表于 2014-11-10 23:38 static/image/common/back.gif
问题出在这句:
if (analogRead(1) > 100) //當按鈕按下時,端口1會獲得高電位
由此可见,好的书写习惯在debug的时候,其重要性就体现出来了
页:
[1]