Arduino -当按下按钮时开始计时器



我想启动一个定时器一旦按下按钮。计时器将点亮LED灯5分钟。五分钟后,我想再等两分钟,然后再按下一个按钮。

我知道该怎么做了。
我将使用延迟命令一段时间,而LED是高然后一段时间后,我将设置LED低以此类推。我有我在这里使用的所有代码。

const int LED2 = 12;
const int LED = 13;
const int BUTTON = 7;
int var = 0;
int val = 0;
int old_val = 0;
int state = 0;
void setup(){                               //telling the computer what the LED and the               button are
  pinMode (LED2,OUTPUT);
  pinMode (LED,OUTPUT);
  pinMode (BUTTON,INPUT);
}
void loop(){
  val = digitalRead(BUTTON);
  if ((val == HIGH) && (old_val == LOW)){    
    digitalWrite(LED,HIGH); 
    delay(240000);                         //The period of time to wait before turning    on the LED2
    digitalWrite(LED2,HIGH);
    delay (1000);
    digitalWrite(LED2,LOW);
    delay (490000);
    var = 0;
    while(var < 10){                      //A while loop to flash the LED2 on and off
      digitalWrite(LED2,HIGH);
      delay (500);
      digitalWrite(LED2,LOW);
      delay (500);
      var++;
    }
    digitalWrite(LED,LOW);
    delay(120000);                        //A two minute delay before the button can be pressed again
  }
}

使用内置示例程序"Blink without Delay"并将其与"Button"组合使用。请尽量避免使用delay()函数,因为它会浪费处理器的时间,而且如果存在中断,还会导致计时不准确。

最新更新