#include <Adafruit_NeoPixel.h>
#define LED_PIN 21 // 数据引脚连接到 ESP32 的 GPIO 5
#define NUM_LEDS 18 // WS2812 灯带的 LED 数量
#define BRIGHTNESS 255 // 设置亮度级别,范围从 0 到 255
#define LED_TYPE NEO_GRB // WS2812 颜色顺序
#define COLOR_ORDER NEO_GRB // WS2812 颜色顺序
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, LED_TYPE);
void setup() {
// 初始化 NeoPixel 库
strip.begin();
strip.setBrightness(BRIGHTNESS);
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
// 运行不同的灯光效果
colorWipe(strip.Color(255, 0, 0), 50); // 红色
delay(500);
colorWipe(strip.Color(0, 255, 0), 50); // 绿色
delay(500);
colorWipe(strip.Color(0, 0, 255), 50); // 蓝色
delay(500);
// 其他效果
rainbow(20);
delay(5000);
}
// 填充整个灯带为单一颜色
void colorWipe(uint32_t color, int wait) {
for(int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, color);
strip.show();
delay(wait);
}
}
// 创建彩虹效果
void rainbow(int wait) {
int i;
for(i = 0; i < 256; i++) {
for(int j = 0; j < strip.numPixels(); j++) {
int hue = (j * 256 / strip.numPixels() + i) & 255;
strip.setPixelColor(j, strip.ColorHSV(hue));
}
strip.show();
delay(wait);
}
}