Arduino–Mixly按键控制LED灯
通过按下Arduino的按键,控制LED灯的开关,第一次按,灯开,再按灯关,就跟现实的开关灯一样的操作。
volatile boolean b1;
volatile boolean b2;
volatile byte click;
volatile boolean lastState;
volatile boolean toggle;
void setup(){
lastState = 0;
toggle = 0;
click = 0;
b1 = 0;
b2 = 0;
pinMode(8, INPUT_PULLUP);
lastState = digitalRead(8);
pinMode(7, OUTPUT);
}
void loop(){
b1 = digitalRead(8);
if (b1 != lastState) {
delay(20);
b2 = digitalRead(8);
if (b2 == b1) {
lastState = b1;
click = click + 1;
}
}
if (click == 2) {
click = 0;
toggle = !toggle;
digitalWrite(7,toggle);
}
}
代码转换为Mixly图形化