一、实验目的及要求
button开关led。
二、实验原理与内容
实现米思齐按键控制LED(改写方法3)
三、代码片段
volatile boolean b1;
volatile boolean b2;
volatile byte click;
volatile boolean laststage;
volatile boolean toggle;
void setup(){
pinMode(13, INPUT);
Serial.begin(9600);
pinMode(9, OUTPUT);
laststage = digitalRead(13);
click = 0;
toggle = LOW;
b1 = 0;
b2 = 0;
pinMode(13, INPUT_PULLUP);
pinMode(9, OUTPUT);
digitalWrite(9,toggle);
}
void loop(){
b1 = digitalRead(13);
if (b1 != laststage) {
delay(50);
b2 = digitalRead(13);
if (b2 == b1) {
laststage = b1;
click = click + 1;
Serial.println(click);
Serial.println(laststage);
Serial.println(b1);
Serial.println(b2);
}
}
if (click >= 2) {
click = 0;
toggle = !toggle;
digitalWrite(9,toggle);
}
}