使用arduino写一个收到串口数据“1”时电机旋转ws2812b灯带长亮绿色且9号引脚高电平 10秒后熄灭9号引脚低电平收到串口数据“2”时ws2812b灯带红色闪烁 10秒后熄灭 并添加注释
时间: 2023-04-11 09:01:03 浏览: 67
可以回答这个问题,以下是代码:
```
#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUM_LEDS 8
Adafruit_NeoPixel strip(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(9600);
pinMode(9, OUTPUT);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
if (Serial.available() > 0) {
int data = Serial.read();
if (data == '1') {
digitalWrite(9, HIGH);
strip.fill(strip.Color(0, 255, 0)); // set all pixels to green
strip.show();
delay(10000);
digitalWrite(9, LOW);
strip.clear(); // turn off all pixels
strip.show();
} else if (data == '2') {
strip.fill(strip.Color(255, 0, 0)); // set all pixels to red
strip.show();
delay(10000);
strip.clear(); // turn off all pixels
strip.show();
}
}
}
```
注释已添加在代码中。