-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patharduino_esp32_code.ino
169 lines (140 loc) · 4.15 KB
/
arduino_esp32_code.ino
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <FastLED.h>
#define LED_NUM 200
#define DATA_PIN 5
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
CRGB leds[LED_NUM];
BLECharacteristic* pCharacteristic;
bool isAnimating = false;
uint8_t currentAnimation = 0;
CRGB currentColor = CRGB::Black;
// Color palettes
CRGBPalette16 rainbowPalette = RainbowColors_p;
CRGBPalette16 pastelPalette = CloudColors_p;
CRGBPalette16 warmPalette = HeatColors_p;
CRGBPalette16 coolPalette = OceanColors_p;
void setup() {
Serial.begin(115200);
// Setup LEDs
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, LED_NUM);
FastLED.setBrightness(100);
fill_solid(leds, LED_NUM, CRGB::Black);
FastLED.show();
// Start BLE
BLEDevice::init("RGB LED Controller");
BLEServer* pServer = BLEDevice::createServer();
BLEService* pService = pServer->createService(SERVICE_UUID);
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY
);
pService->start();
BLEAdvertising* pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->start();
Serial.println("BLE advertising started. Waiting for connection...");
}
void loop() {
String value = String(pCharacteristic->getValue().c_str());
if (value.length() > 0) {
Serial.print("Received: ");
Serial.println(value);
processCommand(value);
pCharacteristic->setValue(""); // Clear after use
}
if (isAnimating) {
switch (currentAnimation) {
case 5: animationPulse(); break;
case 6: animationRainbow(); break;
case 7: animationStrobe(); break;
case 8: animationCylon(); break;
}
FastLED.show();
FastLED.delay(30);
}
}
void processCommand(String command) {
command.trim();
if (command.startsWith("#") && command.length() == 7) {
isAnimating = false;
currentAnimation = 0;
long number = strtol(command.substring(1).c_str(), NULL, 16);
currentColor = CRGB((number >> 16) & 0xFF, (number >> 8) & 0xFF, number & 0xFF);
fill_solid(leds, LED_NUM, currentColor);
FastLED.setBrightness(100);
FastLED.show();
}
else if (command.length() == 1 && command[0] >= '1' && command[0] <= '4') {
isAnimating = false;
currentAnimation = 0;
applyPalette(command.toInt());
}
else if (command.length() == 1 && command[0] >= '5' && command[0] <= '8') {
currentAnimation = command.toInt();
isAnimating = true;
}
}
void applyPalette(uint8_t index) {
CRGBPalette16 palette;
switch (index) {
case 1: palette = rainbowPalette; break;
case 2: palette = pastelPalette; break;
case 3: palette = warmPalette; break;
case 4: palette = coolPalette; break;
default: return;
}
for (int i = 0; i < LED_NUM; i++) {
leds[i] = ColorFromPalette(palette, i * (255 / LED_NUM), 255, LINEARBLEND);
}
FastLED.setBrightness(100);
FastLED.show();
}
// === Animations ===
void animationPulse() {
static uint8_t hue = 0;
fill_solid(leds, LED_NUM, CHSV(hue++, 255, 255));
}
void animationRainbow() {
static uint8_t startIndex = 0;
startIndex++;
fill_rainbow(leds, LED_NUM, startIndex);
}
void animationStrobe() {
static bool on = false;
on = !on;
fill_solid(leds, LED_NUM, on ? CRGB::White : CRGB::Black);
}
void animationCylon() {
static uint8_t hue = 0;
for(int i = 0; i < LED_NUM; i++) {
leds[i] = CHSV(hue++, 255, 255);
FastLED.show();
fadeAll();
delay(10);
}
for(int i = LED_NUM - 1; i >= 0; i--) {
leds[i] = CHSV(hue++, 255, 255);
FastLED.show();
fadeAll();
delay(10);
}
}
void fadeAll() {
for(int i = 0; i < LED_NUM; i++) {
leds[i].nscale8(250);
}
}
// void animationFade() {
// static uint8_t brightness = 0;
// static bool increasing = true;
// brightness += (increasing ? 5 : -5);
// if (brightness >= 255 || brightness <= 0) increasing = !increasing;
// fill_solid(leds, LED_NUM, currentColor);
// FastLED.setBrightness(brightness);
// }