ESP32
2026-04-14 23:48阅读:评论:
ESP32-S3 控制 WS2812B灯珠
需要FastLED库 #include <FastLED.h> #define DATA_PIN 7 // 数据引脚连接到LED灯带的DIN #define LED_COUNT 16 // LED灯带的LED数量 #define BRIGHTNESS 120 // 亮度(0-255) CR
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <FastLED.h> #define DATA_PIN 7 // 数据引脚连接到LED灯带的DIN#define LED_COUNT 16 // LED灯带的LED数量#define BRIGHTNESS 120 // 亮度(0-255) CRGB leds[LED_COUNT];#define DELAY_TIME 3000 // ==================== 函数声明(修复所有报错) ====================void breathingRed();void rainbowCycle();void theaterChase();void strobe();void meteorRain();void colorTwinkle();void fullRainbow();// ================================================================== void setup() { FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, LED_COUNT); FastLED.setBrightness(BRIGHTNESS);} void loop() { breathingRed(); // 1. 红色呼吸 rainbowCycle(); // 2. 彩虹流动 theaterChase(); // 3. 跑马灯追逐 strobe(); // 4. 爆闪 meteorRain(); // 5. 流星 colorTwinkle(); // 6. 彩色闪烁 fullRainbow(); // 7. 全屏彩虹} // ------------------- 1. 呼吸灯 -------------------void breathingRed() { unsigned long end = millis() + DELAY_TIME; while (millis() < end) { for (int i = 0; i < 255; i++) { fill_solid(leds, LED_COUNT, CRGB(i, 0, 0)); FastLED.show(); delay(4); } for (int i = 255; i > 0; i--) { fill_solid(leds, LED_COUNT, CRGB(i, 0, 0)); FastLED.show(); delay(4); } }} // ------------------- 2. 彩虹流动 -------------------void rainbowCycle() { unsigned long end = millis() + DELAY_TIME; uint8_t hue = 0; while (millis() < end) { fill_rainbow(leds, LED_COUNT, hue++, 7); FastLED.show(); delay(20); }} // ------------------- 3. 跑马灯追逐 -------------------void theaterChase() { unsigned long end = millis() + DELAY_TIME; while (millis() < end) { for (int a = 0; a < 3; a++) { for (int b = 0; b < LED_COUNT; b += 3) leds[b + a] = CRGB::Blue; FastLED.show(); delay(50); for (int b = 0; b < LED_COUNT; b += 3) leds[b + a] = CRGB::Black; } }} // ------------------- 4. 爆闪 -------------------void strobe() { unsigned long end = millis() + DELAY_TIME; while (millis() < end) { fill_solid(leds, LED_COUNT, CRGB::White); FastLED.show(); delay(20); fill_solid(leds, LED_COUNT, CRGB::Black); FastLED.show(); delay(20); }} // ------------------- 5. 流星 -------------------void meteorRain() { unsigned long end = millis() + DELAY_TIME; while (millis() < end) { for (int i = 0; i < LED_COUNT + 10; i++) { fadeToBlackBy(leds, LED_COUNT, 30); if (i < LED_COUNT) leds[i] = CRGB::Cyan; FastLED.show(); delay(25); } }} // ------------------- 6. 彩色闪烁 -------------------void colorTwinkle() { unsigned long end = millis() + DELAY_TIME; while (millis() < end) { leds[random(LED_COUNT)] = CHSV(random(255), 255, 255); FastLED.show(); fadeToBlackBy(leds, LED_COUNT, 20); delay(30); }} // ------------------- 7. 全屏彩虹 -------------------void fullRainbow() { unsigned long end = millis() + DELAY_TIME; uint8_t hue = 0; while (millis() < end) { fill_solid(leds, LED_COUNT, CHSV(hue++, 255, 255)); FastLED.show(); delay(15); }}