#include <Arduino.h>
// 定义灯光效果和引脚等
const int ledPin = 5; // 假设LED连接到GPIO 5
// 三种状态定义
#define BREATHE_STATE 1
#define BLINK_STATE 2
#define HIGHLIGHT_STATE 3
// 当前状态
int currentState = BREATHE_STATE;
// 上一次状态
int lastState = BREATHE_STATE;
// 预定义灯光效果变量
int BreatheEffect[] = {0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170, 175, 180, 185, 190, 195, 200, 205, 210, 215, 220, 225, 230, 235, 240, 245, 250, 255, 250, 245, 240, 235, 230, 225, 220, 215, 210, 205, 200, 195, 190, 185, 180, 175, 170, 165, 160, 155, 150, 145, 140, 135, 130, 125, 120, 115, 110, 105, 100, 95, 90, 85, 80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20, 15, 10, 5, 0, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170, 175, 180, 185, 190, 195, 200, 205, 210, 215, 220, 225, 230, 235, 240, 245, 250, 255, 250, 245, 240, 235, 230, 225, 220, 215, 210, 205, 200, 195, 190, 185, 180, 175, 170, 165, 160, 155, 150, 145, 140, 135, 130, 125, 120, 115, 110, 105, 100, 95, 90, 85, 80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20, 15, 10, 5, 0, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170, 175, 180, 185, 190, 195, 200, 205, 210, 215, 220, 225, 230, 235, 240, 245, 250, 255, 250, 245, 240, 235, 230, 225, 220, 215, 210, 205, 200, 195, 190, 185, 180, 175, 170, 165, 160, 155, 150, 145, 140, 135, 130, 125, 120, 115, 110, 105, 100, 95, 90, 85, 80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20, 15, 10, 5, 0, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170, 175, 180, 185, 190, 195, 200, 205, 210, 215, 220, 225, 230, 235, 240, 245, 250, 255, 250, 245, 240, 235, 230, 225, 220, 215, 210, 205, 200, 195, 190, 185, 180, 175, 170, 165, 160, 155, 150, 145, 140, 135, 130, 125, 120, 115, 110, 105, 100, 95, 90, 85, 80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20, 15, 10, 5, 0, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170, 175, 180, 185, 190, 195, 200, 205, 210, 215, 220, 225, 230, 235, 240, 245, 250, 255, 250, 245, 240, 235, 230, 225, 220, 215, 210, 205, 200, 195, 190, 185, 180, 175, 170, 165, 160, 155, 150, 145, 140, 135, 130, 125, 120, 115, 110, 105, 100, 95, 90, 85, 80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20, 15, 10, 5, 0};
int BlinkEffect[] = {10, 210, 90, 120, 205, 45, 75, 200, 175, 85, 65, 25, 65, 25, 105, 45, 140, 170, 10, 20, 40, 245, 95, 215, 105, 95, 145, 180, 215, 160, 10, 220, 210, 55, 0, 135, 155, 200, 30, 40, 10, 130, 55, 250, 85, 235, 200, 140, 40, 240, 5, 235, 55, 125, 190, 140, 245, 125, 95, 140, 30, 85, 50, 100, 165, 180, 25, 110, 245, 110, 85, 55, 10, 165, 205, 95, 10, 25, 130, 120, 230, 155, 155, 115, 75, 225, 55, 115, 130, 75, 160, 255, 150};
int HighlightEffect[] = {255};
int *currentEffect = BreatheEffect; // 当前效果指针
int effectSize = sizeof(BreatheEffect) / sizeof(BreatheEffect[0]); // 效果大小
int effectIndex = 0; // 当前播放的效果索引
// FreeRTOS任务句柄
TaskHandle_t Task1;
TaskHandle_t Task2;
// 播放灯光效果
void playLightEffect(void *parameter) {
for (;;) {
// 检查状态,并更新变量
if (lastState != currentState) {
if (currentState == BREATHE_STATE) {
currentEffect = BreatheEffect;
effectSize = sizeof(BreatheEffect) / sizeof(BreatheEffect[0]);
} else if (currentState == BLINK_STATE) {
currentEffect = BlinkEffect;
effectSize = sizeof(BlinkEffect) / sizeof(BlinkEffect[0]);
} else if (currentState == HIGHLIGHT_STATE) {
currentEffect = HighlightEffect;
effectSize = sizeof(HighlightEffect) / sizeof(HighlightEffect[0]);
}
lastState = currentState;
effectIndex = 0; // 重置索引以从效果的开始播放
}
// 播放当前效果的下一个亮度值
analogWrite(ledPin, currentEffect[effectIndex]);
effectIndex++;
if (effectIndex >= effectSize) effectIndex = 0; // 循环播放
vTaskDelay(pdMS_TO_TICKS(1000 / 25)); // 以25fps的速度播放
}
}
// 三种表演状态切换
void switchState(void *parameter) {
for (;;) {
if (Serial.available() > 0) {
int command = Serial.read();
// 检查并忽略回车和换行符
if (command == '\r' || command == '\n') {
continue; // 直接返回,不处理这些字符
}
if (command == '1') {
Serial.println("呼吸状态");
currentState = BREATHE_STATE;
} else if (command == '2') {
Serial.println("闪烁状态");
currentState = BLINK_STATE;
} else if (command == '3') {
Serial.println("高亮状态");
currentState = HIGHLIGHT_STATE;
} else {
Serial.println("未定义状态!");
}
}
vTaskDelay(10 / portTICK_PERIOD_MS); // 短暂延时以减少CPU使用率
}
}
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
// 创建任务
xTaskCreatePinnedToCore(playLightEffect, "PlayLightEffect", 10000, NULL, 1, &Task1, 0); // 在核心0上播放灯光效果
xTaskCreatePinnedToCore(switchState, "SwitchState", 10000, NULL, 1, &Task2, 0); // 在核心0上监听输入和切换效果
}
void loop() {
// 在这里不需要做任何事情
}