lar初始状态,LED等全灭;按一下按键,LED灯全亮;再按一下,LED开始闪烁;再按一下,LED开始交替亮灭,0,2亮,然后1,3亮。若是再按按键则与第一次按键状态相同,如此循环。
时间: 2023-05-25 08:03:10 浏览: 165
这个功能可以使用状态机来实现。以下是一个示例代码,可以在lar板子上测试。
```c
#include <LAR.h>
#define LED1_MASK 0x01
#define LED2_MASK 0x02
#define LED3_MASK 0x04
#define LED4_MASK 0x08
#define BUTTON_MASK 0x10
enum LedState {
LED_OFF,
LED_ON,
LED_BLINK,
LED_ALTERNATE,
};
enum LedState currentLedState;
void setup() {
// 初始化 LAR 板子
Serial.begin(115200);
LAR.begin(115200);
currentLedState = LED_OFF;
}
void loop() {
// 检查按键状态
int button = LAR.digitalRead(BUTTON);
switch (currentLedState) {
case LED_OFF:
if (button == LOW) {
// 第一次按键,所有 LED 亮
LAR.digitalWrite(LED1, HIGH);
LAR.digitalWrite(LED2, HIGH);
LAR.digitalWrite(LED3, HIGH);
LAR.digitalWrite(LED4, HIGH);
currentLedState = LED_ON;
}
break;
case LED_ON:
if (button == LOW) {
// 第二次按键,所有 LED 闪烁
currentLedState = LED_BLINK;
}
else {
// 等待按键
}
break;
case LED_BLINK:
if (button == LOW) {
// 第三次按键,所有 LED 交替亮灭
LAR.digitalWrite(LED1, HIGH);
LAR.digitalWrite(LED2, LOW);
LAR.digitalWrite(LED3, HIGH);
LAR.digitalWrite(LED4, LOW);
currentLedState = LED_ALTERNATE;
}
else {
// 闪烁所有 LED
LAR.digitalWrite(LED1, !LAR.digitalRead(LED1));
LAR.digitalWrite(LED2, !LAR.digitalRead(LED2));
LAR.digitalWrite(LED3, !LAR.digitalRead(LED3));
LAR.digitalWrite(LED4, !LAR.digitalRead(LED4));
delay(500);
}
break;
case LED_ALTERNATE:
if (button == LOW) {
// 四个 LED 交替亮灭
LAR.digitalWrite(LED1, HIGH);
LAR.digitalWrite(LED2, LOW);
LAR.digitalWrite(LED3, HIGH);
LAR.digitalWrite(LED4, LOW);
delay(500);
LAR.digitalWrite(LED1, LOW);
LAR.digitalWrite(LED2, HIGH);
LAR.digitalWrite(LED3, LOW);
LAR.digitalWrite(LED4, HIGH);
delay(500);
}
else {
// 等待按键
}
break;
}
// 等待一段时间,降低 CPU 占用率
delay(10);
}
```
阅读全文