-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminiLightShow.ino
More file actions
381 lines (318 loc) · 11.4 KB
/
miniLightShow.ino
File metadata and controls
381 lines (318 loc) · 11.4 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include <Adafruit_NeoPixel.h>
#define PIN 13
#define N_LEDS 40
Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_LEDS, PIN, NEO_GRB + NEO_KHZ800);
const uint8_t GRID_WIDTH = 8;
const uint8_t GRID_HEIGHT = N_LEDS / GRID_WIDTH;
// Shape definitions for the magic_shapes effect. Each value represents a pixel:
// 0 = background, >0 = index into the palette for that shape (1-based).
const uint8_t SHAPE_SQUARE[GRID_WIDTH * GRID_HEIGHT] = {
0, 1, 1, 1, 1, 1, 1, 0,
1, 2, 2, 2, 2, 2, 2, 1,
1, 2, 3, 3, 3, 3, 2, 1,
1, 2, 3, 3, 3, 3, 2, 1,
0, 1, 1, 1, 1, 1, 1, 0
};
const uint8_t SHAPE_HEART[GRID_WIDTH * GRID_HEIGHT] = {
0, 1, 0, 0, 0, 0, 1, 0,
1, 2, 1, 0, 0, 1, 2, 1,
1, 3, 2, 1, 1, 2, 3, 1,
0, 1, 3, 2, 2, 3, 1, 0,
0, 0, 1, 3, 3, 1, 0, 0
};
const uint8_t SHAPE_TRIANGLE[GRID_WIDTH * GRID_HEIGHT] = {
0, 0, 0, 1, 1, 0, 0, 0,
0, 0, 1, 2, 2, 1, 0, 0,
0, 1, 2, 3, 3, 2, 1, 0,
1, 2, 3, 3, 3, 3, 2, 1,
1, 1, 1, 1, 1, 1, 1, 1
};
const uint8_t SHAPE_CIRCLE[GRID_WIDTH * GRID_HEIGHT] = {
0, 0, 1, 1, 1, 1, 0, 0,
0, 1, 2, 2, 2, 2, 1, 0,
1, 2, 3, 3, 3, 3, 2, 1,
0, 1, 2, 2, 2, 2, 1, 0,
0, 0, 1, 1, 1, 1, 0, 0
};
void setup() {
strip.begin();
Serial.begin(9600);
randomSeed(analogRead(0));
strip.setBrightness(127);
}
void loop() {
twinkle(strip.Color(8, 8, 8), strip.Color(255, 255, 255), 60, 40);
auroraWave(strip.Color(12, 4, 32), strip.Color(0, 64, 48), 6, 6);
magic_shapes();
twinkle(strip.Color(8, 0, 0), strip.Color(255, 0, 0), 60, 40);
twinkle(strip.Color(0, 8, 0), strip.Color(0, 255, 0), 60, 40);
twinkle(strip.Color(0, 0, 8), strip.Color(0, 0, 255), 60, 40);
auroraWave(strip.Color(8, 0, 0), strip.Color(255, 0, 0), 6, 6);
auroraWave(strip.Color(0, 8, 0), strip.Color(0, 255, 0), 6, 6);
auroraWave(strip.Color(0, 0, 8), strip.Color(0, 0, 255), 6, 6);
twinkle(strip.Color(32, 0, 0), strip.Color(random(0, 256), random(0, 256), random(0, 256)), 60, 40);
twinkle(strip.Color(0, 32, 0), strip.Color(random(0, 256), random(0, 256), random(0, 256)), 60, 40);
twinkle(strip.Color(0, 0, 32), strip.Color(random(0, 256), random(0, 256), random(0, 256)), 60, 40);
chase(strip.Color(255, 0, 0)); // Red
chase(strip.Color(0, 255, 0)); // Green
chase(strip.Color(0, 0, 255)); // Blue
breathe(strip.Color(255, 0, 0)); // Red
breathe(strip.Color(0, 255, 0)); // Green
breathe(strip.Color(0, 0, 255)); // Blue
cycle_red();
cycle_green();
cycle_blue();
twinkle(strip.Color(64, 0, 0), strip.Color(255, 0, 0), 60, 40);
twinkle(strip.Color(0, 64, 0), strip.Color(0, 255, 0), 60, 40);
twinkle(strip.Color(0, 0, 64), strip.Color(0, 0, 255), 60, 40);
auroraWave(strip.Color(64, 0, 0), strip.Color(255, 0, 0), 6, 6);
auroraWave(strip.Color(0, 64, 0), strip.Color(0, 255, 0), 6, 6);
auroraWave(strip.Color(0, 0, 64), strip.Color(0, 0, 255), 6, 6);
breathe(strip.Color(random(0, 256), random(0, 256), random(0, 256)));
auroraWave(strip.Color(random(0, 256), random(0, 256), random(0, 256)), strip.Color(random(0, 256), random(0, 256), random(0, 256)), 6, 6);
chase(strip.Color(random(0, 256), 0, 0)); // Random red
chase(strip.Color(0, random(0, 256), 0)); // Random green
chase(strip.Color(0, 0, random(0, 256))); // Random blue
breathe(strip.Color(random(0, 256), random(0, 256), random(0, 256)));
chase(strip.Color(255, 0, 0)); // Red
chase(strip.Color(0, 0, 255)); // Blue
chase(strip.Color(255, 0, 0)); // Red
chase(strip.Color(0, 0, 255)); // Blue
chase(strip.Color(255, 0, 0)); // Red
chase(strip.Color(0, 0, 255)); // Blue
breathe(strip.Color(255, 0, 0)); // Red
chase(strip.Color(255, 0, 0)); // Red
chase(strip.Color(255, 0, 0)); // Red
chase(strip.Color(255, 0, 0)); // Red
breathe(strip.Color(255, 0, 0)); // Red
breathe(strip.Color(0, 255, 0)); // Green
chase(strip.Color(0, 255, 0)); // Green
chase(strip.Color(0, 255, 0)); // Green
chase(strip.Color(0, 255, 0)); // Green
breathe(strip.Color(0, 255, 0)); // Green
breathe(strip.Color(0, 0, 255)); // Blue
chase(strip.Color(0, 0, 255)); // Blue
chase(strip.Color(0, 0, 255)); // Blue
chase(strip.Color(0, 0, 255)); // Blue
breathe(strip.Color(0, 0, 255)); // Blue
chase(strip.Color(0, 0, 255)); // Blue
chase(strip.Color(0, 255, 0)); // Green
chase(strip.Color(0, 0, 255)); // Blue
chase(strip.Color(0, 255, 0)); // Green
chase(strip.Color(0, 0, 255)); // Blue
chase(strip.Color(0, 255, 0)); // Green
breathe(strip.Color(random(0, 256), random(0, 256), random(0, 256)));
chase(strip.Color(random(0, 256), 0, 0)); // Random red
chase(strip.Color(0, random(0, 256), 0)); // Random green
chase(strip.Color(0, 0, random(0, 256))); // Random blue
breathe(strip.Color(random(0, 256), random(0, 256), random(0, 256)));
chase(strip.Color(0, 255, 0)); // Green
chase(strip.Color(255, 0, 0)); // Red
chase(strip.Color(0, 255, 0)); // Green
chase(strip.Color(255, 0, 0)); // Red
chase(strip.Color(0, 255, 0)); // Green
chase(strip.Color(255, 0, 0)); // Red
chase(strip.Color(255, 0, 0)); // Red
cycle_red();
chase(strip.Color(255, 0, 0)); // Red
chase(strip.Color(0, 255, 0)); // Green
cycle_green();
chase(strip.Color(0, 255, 0)); // Green
chase(strip.Color(0, 0, 255)); // Blue
cycle_blue();
chase(strip.Color(0, 0, 255)); // Blue
auroraWave(strip.Color(12, 4, 32), strip.Color(0, 64, 48), 6, 6);
twinkle(strip.Color(8, 8, 8), strip.Color(255, 255, 255), 60, 40);
}
static void drawMagicShape(const uint8_t *shape, uint32_t backgroundColor, const uint32_t *palette, uint8_t paletteSize) {
strip.setBrightness(127);
for(uint8_t row=0; row<GRID_HEIGHT; row++) {
for(uint8_t col=0; col<GRID_WIDTH; col++) {
uint16_t index = row * GRID_WIDTH + col;
uint8_t value = shape[index];
if(value == 0 || paletteSize == 0) {
strip.setPixelColor(index, backgroundColor);
} else {
uint8_t paletteIndex = (value - 1) % paletteSize;
strip.setPixelColor(index, palette[paletteIndex]);
}
}
}
strip.show();
}
static void shimmerMagicShape(const uint8_t *shape, const uint32_t *palette, uint8_t paletteSize, uint32_t backgroundColor, uint16_t steps, uint16_t stepDelay) {
if(paletteSize == 0) {
return;
}
for(uint16_t step=0; step<steps; step++) {
uint16_t pixel = random(N_LEDS);
if(shape[pixel] == 0) {
continue;
}
uint8_t paletteIndex = (shape[pixel] - 1) % paletteSize;
uint32_t baseColor = palette[paletteIndex];
uint32_t sparkleColor = blendColors(strip.Color(255, 255, 255), baseColor, 192);
strip.setPixelColor(pixel, sparkleColor);
strip.show();
delay(stepDelay);
drawMagicShape(shape, backgroundColor, palette, paletteSize);
}
}
static void magic_shapes() {
const uint8_t *shapes[] = {SHAPE_SQUARE, SHAPE_HEART, SHAPE_TRIANGLE, SHAPE_CIRCLE};
const uint8_t paletteSizes[] = {3, 3, 3, 3};
const uint32_t palettes[][3] = {
{strip.Color(255, 96, 0), strip.Color(255, 180, 0), strip.Color(255, 255, 96)},
{strip.Color(255, 0, 72), strip.Color(255, 64, 144), strip.Color(255, 182, 213)},
{strip.Color(0, 128, 255), strip.Color(0, 200, 180), strip.Color(64, 64, 255)},
{strip.Color(0, 255, 128), strip.Color(0, 200, 255), strip.Color(160, 96, 255)}
};
const uint32_t backgrounds[] = {
strip.Color(0, 0, 16),
strip.Color(8, 0, 16),
strip.Color(0, 8, 16),
strip.Color(0, 0, 16)
};
const uint16_t holdTimes[] = {1300, 1500, 1300, 1500};
for(uint8_t cycle=0; cycle<2; cycle++) {
for(uint8_t shapeIndex=0; shapeIndex<4; shapeIndex++) {
drawMagicShape(shapes[shapeIndex], backgrounds[shapeIndex], palettes[shapeIndex], paletteSizes[shapeIndex]);
shimmerMagicShape(shapes[shapeIndex], palettes[shapeIndex], paletteSizes[shapeIndex], backgrounds[shapeIndex], 18, 40);
delay(holdTimes[shapeIndex]);
}
}
}
static void chase(uint32_t c) {
strip.setBrightness(127);
for(uint16_t i=0; i<strip.numPixels()+4; i++) {
strip.setPixelColor(i , c); // Draw new pixel
strip.setPixelColor(i-4, 0); // Erase pixel a few steps back
strip.show();
delay(25);
}
}
static void breathe(uint32_t c) {
for(uint16_t l=0; l<3; l++) {
for(uint16_t b=10; b<128; b++) {
strip.setBrightness(b);
for(uint16_t i=0; i<40; i++) {
strip.setPixelColor(i , c);
}
strip.show();
delay(10);
}
for(uint16_t b=127; b>10; b--) {
strip.setBrightness(b);
for(uint16_t i=0; i<40; i++) {
strip.setPixelColor(i , c);
}
strip.show();
delay(25);
}
}
}
static void cycle() {
strip.setBrightness(127);
for(uint16_t r=0; r<256; r+=32) {
// RED
for(uint16_t g=0; g<256; g+=32) {
// GREEN
for(uint16_t b=0; b<256; b+=32) {
// BLUE
for(uint16_t i=0; i<40; i++) {
strip.setPixelColor(i, strip.Color(r, g, b));
Serial.println("R: " + (String)r + " - G: " + (String)g + " - B: " + (String)b);
}
strip.show();
delay(25);
}
}
}
}
static void cycle_red() {
strip.setBrightness(127);
for(uint16_t r=0; r<256; r+=32) {
for(uint16_t i=0; i<40; i++) {
strip.setPixelColor(i, strip.Color(r, 0, 0));
}
strip.show();
delay(100);
}
}
static void cycle_green() {
strip.setBrightness(127);
for(uint16_t g=0; g<256; g+=32) {
for(uint16_t i=0; i<40; i++) {
strip.setPixelColor(i, strip.Color(0, g, 0));
}
strip.show();
delay(100);
}
}
static void cycle_blue() {
strip.setBrightness(127);
for(uint16_t b=0; b<256; b+=32) {
for(uint16_t i=0; i<40; i++) {
strip.setPixelColor(i, strip.Color(0, 0, b));
}
strip.show();
delay(100);
}
}
static uint32_t blendColors(uint32_t c1, uint32_t c2, uint8_t amount) {
uint8_t r1 = (uint8_t)(c1 >> 16);
uint8_t g1 = (uint8_t)(c1 >> 8);
uint8_t b1 = (uint8_t)c1;
uint8_t r2 = (uint8_t)(c2 >> 16);
uint8_t g2 = (uint8_t)(c2 >> 8);
uint8_t b2 = (uint8_t)c2;
uint16_t inverse = 255 - amount;
uint8_t r = (uint8_t)(((uint16_t)r1 * inverse + (uint16_t)r2 * amount) / 255);
uint8_t g = (uint8_t)(((uint16_t)g1 * inverse + (uint16_t)g2 * amount) / 255);
uint8_t b = (uint8_t)(((uint16_t)b1 * inverse + (uint16_t)b2 * amount) / 255);
return strip.Color(r, g, b);
}
static void auroraWave(uint32_t colorA, uint32_t colorB, uint8_t waveSpeed, uint8_t passes) {
strip.setBrightness(127);
if(waveSpeed == 0) {
waveSpeed = 1;
}
if(passes == 0) {
passes = 1;
}
uint16_t pixelCount = strip.numPixels();
if(pixelCount == 0) {
return;
}
for(uint16_t step=0; step<(uint16_t)passes * 256; step += waveSpeed) {
for(uint16_t i=0; i<pixelCount; i++) {
uint16_t base = ((uint16_t)i * 256) / pixelCount;
uint8_t wave = (uint8_t)(base + step);
uint8_t blendAmount = wave < 128 ? (uint8_t)(wave * 2) : (uint8_t)((255 - wave) * 2);
uint32_t blended = blendColors(colorA, colorB, blendAmount);
strip.setPixelColor(i, blended);
}
strip.show();
delay(35);
}
}
static void twinkle(uint32_t backgroundColor, uint32_t sparkleColor, uint16_t sparkleCount, uint16_t sparkleDuration) {
strip.setBrightness(127);
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, backgroundColor);
}
strip.show();
uint16_t resetDelay = sparkleDuration / 2;
if(resetDelay == 0) {
resetDelay = 1;
}
for(uint16_t sparkle=0; sparkle<sparkleCount; sparkle++) {
uint16_t pixel = random(strip.numPixels());
strip.setPixelColor(pixel, sparkleColor);
strip.show();
delay(sparkleDuration);
strip.setPixelColor(pixel, backgroundColor);
strip.show();
delay(resetDelay);
}
}