Mixly按键控制LED 灯。
Mixly模块如下所示:
代码:
volatile byte LED;
volatile byte SW;
volatile boolean b1;
volatile boolean b2;
volatile byte click;
volatile boolean lastState;
volatile boolean toggle;
int mixly_digitalRead(uint8_t pin) {
pinMode(pin, INPUT);
return digitalRead(pin);
}
void setup(){
LED = 13;
SW = 8;
lastState = LOW;
toggle = LOW;
click = 0;
pinMode(LED, OUTPUT);
digitalWrite(LED,LOW);
b1 = mixly_digitalRead(SW);
b2 = mixly_digitalRead(SW);
}
void loop(){
lastState = mixly_digitalRead(SW);
if (b1 != lastState) {
delay(20);
if (b1 == b2) {
lastState = b1;
click = click + 1;
}
}
if (click == 2) {
click = 0;
toggle = !toggle;
pinMode(LED, OUTPUT);
digitalWrite(LED,toggle);
}
}
实现效果:
初始灯是灭的
当按一次按键,灯亮
再按一次按键,灯灭
来源:https://blog.csdn.net/rxx980608/article/details/102730245