Mixly按键控制LED灯
实现米思齐按键控制LED
上拉输入的状态时,管脚#12输出为1,当按下按键时输出0。要注意当按下按键时亮灯时,中间的if语句循环了两次
代码
volatile int b1;
volatile int b2;
volatile int click;
volatile int laststate;
volatile boolean toggle;
void setup(){
pinMode(13, OUTPUT);
laststate = digitalRead(12);
digitalWrite(13,toggle);
pinMode(12, INPUT_PULLUP);
b1 = digitalRead(12);
b2 = digitalRead(12);
click = 0;
toggle = LOW;
Serial.begin(9600);
}
void loop(){
Serial.println(digitalRead(12));
b1 = digitalRead(12);
if (b1 != laststate) {
delay(20);
b2 = digitalRead(12);
if (b1 == b2) {
laststate = b1;
click = click + 1;
}
}
if (click == 2) {
click = 0;
toggle = !toggle;
digitalWrite(13,toggle);
}
}