-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesp32_c6_rgb_led_control.ino
More file actions
53 lines (40 loc) · 1.42 KB
/
Copy pathesp32_c6_rgb_led_control.ino
File metadata and controls
53 lines (40 loc) · 1.42 KB
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
#include <Adafruit_NeoPixel.h>
constexpr uint8_t LED_PIN = 8;
constexpr uint8_t NUM_LEDS = 1;
Adafruit_NeoPixel rgbLed(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
struct RGB {
uint8_t r, g, b;
};
constexpr RGB COLOR_OFF = {0, 0, 0};
constexpr RGB COLOR_RED = {255, 0, 0};
constexpr RGB COLOR_GREEN = {0, 255, 0};
constexpr RGB COLOR_BLUE = {0, 0, 255};
constexpr RGB COLOR_WHITE = {255, 255, 255};
// ...Feel free to add more colors here...
constexpr RGB CUSTOM_COLOR = {255, 0, 255};
void setup() {
rgbLed.begin();
rgbLed.show();
}
void setColor(const RGB& color, uint8_t brightness = 100) {
uint16_t scale = (uint16_t)brightness * 255 / 100;
uint8_t r = (uint8_t)(((uint16_t)color.r * scale) / 255);
uint8_t g = (uint8_t)(((uint16_t)color.g * scale) / 255);
uint8_t b = (uint8_t)(((uint16_t)color.b * scale) / 255);
rgbLed.setPixelColor(0, rgbLed.Color(r, g, b));
rgbLed.show();
}
void blinkColor(const RGB& color, unsigned long duration) {
setColor(color, 50); // 50% brightness
delay(duration);
setColor(COLOR_OFF);
delay(duration);
}
void loop() {
constexpr unsigned long BLINK_DURATION = 500;
blinkColor(COLOR_RED, BLINK_DURATION);
blinkColor(COLOR_GREEN, BLINK_DURATION);
blinkColor(COLOR_BLUE, BLINK_DURATION);
blinkColor(COLOR_WHITE, BLINK_DURATION);
blinkColor(CUSTOM_COLOR, BLINK_DURATION);
}