使用arduino写一个收到串口数据“1”时ws2812灯带长亮绿色 10秒后熄灭 收到串口数据“2”时ws2812灯带红色闪烁 10秒后熄灭
时间: 2023-04-11 11:00:56 浏览: 181
可以使用以下代码实现:
#include <Adafruit_NeoPixel.h>
#define LED_PIN 6
#define LED_COUNT 60
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin();
strip.show(); // 初始化所有LED为关闭状态
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
int data = Serial.read();
if (data == '1') {
strip.fill(strip.Color(0, 255, 0)); // 绿色
strip.show();
delay(10000); // 10秒后熄灭
strip.fill(strip.Color(0, 0, 0)); // 关闭所有LED
strip.show();
} else if (data == '2') {
for (int i = 0; i < 10; i++) {
strip.fill(strip.Color(255, 0, 0)); // 红色
strip.show();
delay(500); // 闪烁500毫秒
strip.fill(strip.Color(0, 0, 0)); // 关闭所有LED
strip.show();
delay(500); // 闪烁500毫秒
}
strip.fill(strip.Color(0, 0, 0)); // 关闭所有LED
strip.show();
}
}
}
注意:在使用该代码前,需要先安装Adafruit_NeoPixel库。
阅读全文
相关推荐

















