我愿再次按下按钮,启动一个定时器。定时器将点亮的LED五分钟。经过五分钟以上的过期我想它注册另一个按钮按下前等待两分钟。
我已经找到了如何做到这一点。 我将使用延迟命令一段时间,而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
}
}
使用内置的示例程序“闪烁无延迟”,并以“按钮”结合起来。这两个程序都可以作为http://arduino.cc/en/Tutorial/HomePage教程尽量避免延迟()函数,因为它浪费了处理器时间,并具有定时不准确,如果有中断。