-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtower.ino
More file actions
408 lines (319 loc) · 9.81 KB
/
tower.ino
File metadata and controls
408 lines (319 loc) · 9.81 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include "tower.h"
#define PIN_LED_STRIP_1 5
#define PIN_LED_STRIP_2 6
#define PIN_LED_STRIP_3 9
#define PIN_LED_STRIP_4 12
#define PIN_IR_RECEIVER 4
#define COLOR_ORDER RGB
#define CHIPSET WS2812B
#define NUM_LEDS 450
#define NUM_COLORS 7
static CRGB pride_colors_rgb[NUM_COLORS] = { CRGB(118, 0, 137), CRGB(0, 68, 255), CRGB(0, 129, 31),
CRGB(255, 239, 0), CRGB(255, 140, 0), CRGB(231, 0, 0), CRGB(255, 255, 255) };
IRrecvPCI myReceiver(PIN_IR_RECEIVER); // IR receiver
IRdecode myDecoder; // IR decoder
CRGB leds[4][NUM_LEDS]; // 4 LED strips. Numbered 0-3 internally but 1-4 externally.
uint8_t brightness = 255;
uint8_t color_index = 0;
bool standby = false;
uint8_t blinky_lights_at_top = 1; // 0 - no blinky lights
// 1 - yes blinky lights, they are on now 2- yes blinky lights, they are off now
typedef void (*LOOPFUNC)(void);
LOOPFUNC loopfunc;
void setup()
{
randomSeed(analogRead(0));
loopfunc = &Multihue;
/* Serial.begin(250000);
delay(2000); */
myReceiver.enableIRIn(); // Start the receiver
FastLED.addLeds<CHIPSET, PIN_LED_STRIP_1, COLOR_ORDER>(leds[0], NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.addLeds<CHIPSET, PIN_LED_STRIP_2, COLOR_ORDER>(leds[1], NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.addLeds<CHIPSET, PIN_LED_STRIP_3, COLOR_ORDER>(leds[2], NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.addLeds<CHIPSET, PIN_LED_STRIP_4, COLOR_ORDER>(leds[3], NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness( brightness );
}
void loop()
{
static uint32_t repeat_meaning = 0;
// IR
if (myReceiver.getResults())
{
myDecoder.decode(); //Decode it
// myDecoder.dumpResults(false); //Now print results. Use false for less detail
if (myDecoder.protocolNum == NEC)
{
uint32_t command = myDecoder.value;
if (command == IR_REPEAT && repeat_meaning && repeat_meaning != IR_STANDBY && repeat_meaning != IR_STROBE && repeat_meaning != IR_COLOR)
command = repeat_meaning;
switch(command)
{
case IR_DIMMER_PLUS:
if (brightness < 20)
brightness++;
else
brightness = min(255, brightness + 10);
FastLED.setBrightness(brightness);
break;
case IR_DIMMER_MINUS:
if (brightness < 20)
brightness = max(1, brightness - 1);
else
brightness = brightness - 10;
FastLED.setBrightness(brightness);
break;
case IR_FULL_ON:
brightness = 255;
FastLED.setBrightness(brightness);
break;
case IR_STANDBY:
standby = !standby;
FastLED.setBrightness(standby ? 0 : brightness);
break;
case IR_STROBE: if (blinky_lights_at_top)
blinky_lights_at_top = 0;
else
blinky_lights_at_top = 1;
break;
case IR_COLOR: color_index = (color_index + 1) % NUM_COLORS; break;
case IR_1: loopfunc = &SlowHueFade; break;
case IR_2: loopfunc = &QuickHueFade; break;
case IR_3: loopfunc = &Rainbow; break;
case IR_4: loopfunc = &ColorSineWave; break;
case IR_5: loopfunc = &SineWave; break;
case IR_6: loopfunc = &Rotate; break;
case IR_7: loopfunc = &Multihue; break;
case IR_8: loopfunc = &Fireworks; break;
/*
case IR_9: DebugPrintf("Unimplemented IR_9\n"); break;
case IR_SOUND_ON: DebugPrintf("Unimplemented IR_SOUND_ON\n"); break;
case IR_SHOW_0: DebugPrintf("Unimplemented IR_SHOW_0\n"); break;
case IR_SOUND_OFF: DebugPrintf("Unimplemented IR_SOUND_OFF\n"); break;
case IR_FADE: DebugPrintf("Unimplemented IR_FADE\n"); break;
*/
}
repeat_meaning = command;
}
myReceiver.enableIRIn(); //Restart receiver
}
(*loopfunc)();
if (blinky_lights_at_top)
{
EVERY_N_SECONDS(1)
{
if (blinky_lights_at_top == 1)
{
blinky_lights_at_top = 2;
}
else
{
blinky_lights_at_top = 1;
}
}
if (blinky_lights_at_top == 1)
{
for (int i = 0; i < 4; i++)
{
fill_solid(&(leds[i][NUM_LEDS - 4]), 4, CRGB::Red);
}
}
}
FastLED.show();
delay(1);
}
void Rainbow()
{
for (int x=0; x < 6; x++) {
for (int i=0; i<4; i++)
{
fill_solid(&(leds[i][ (NUM_LEDS / 6) * x ]), NUM_LEDS / 6, pride_colors_rgb[x]);
}
}
}
void ShootUp()
{
static long pos = 0;
for (int i=0; i<4; i++)
{
fill_solid( &(leds[i][0]), NUM_LEDS, CRGB::Black );
fill_solid(&(leds[i][ pos ]), 50, pride_colors_rgb[color_index]);
}
pos += 20;
if (pos > NUM_LEDS - 50)
{
pos = 0;
}
}
void QuickHueFade()
{
static uint8_t hue = 0;
hue += 3;
for (int i=0; i < 4; i++)
{
fill_solid(&(leds[i][0]), NUM_LEDS, CHSV(hue, 255, 255));
}
}
void SlowHueFade()
{
static uint8_t hue = 0;
for (int i=0; i < 4; i++)
{
fill_solid(&(leds[i][0]), NUM_LEDS, CHSV(hue++, 255, 255));
}
delay(100);
}
void SineWave()
{
const uint16_t BLOB_HEIGHT = 50;
static uint16_t theta = 0;
int16_t sin_theta = scale16( 32767 + sin16_avr( theta ), NUM_LEDS - BLOB_HEIGHT );
bool direction = cos16( theta ) < 0;
theta+=1000;
for (uint8_t i = 0; i < 4; i++)
{
fill_solid( &(leds[i][0]), NUM_LEDS, CRGB::Black );
fill_solid( &(leds[i][sin_theta]), BLOB_HEIGHT, pride_colors_rgb[color_index]);
for (int j = 0; j < BLOB_HEIGHT; j++)
{
leds[i][sin_theta + j].fadeLightBy(direction ? j * 256 / BLOB_HEIGHT
: 255 - j * 256 / BLOB_HEIGHT);
}
}
}
void ColorSineWave()
{
const uint16_t BLOB_HEIGHT = 50;
static uint16_t theta = 0;
static uint8_t hue = 0;
int16_t sin_theta = scale16( 32767 + sin16_avr( theta ), NUM_LEDS - BLOB_HEIGHT );
bool direction = cos16( theta ) < 0;
theta+=1000;
for (uint8_t i = 0; i < 4; i++)
{
fill_solid( &(leds[i][0]), NUM_LEDS, CRGB::Black );
fill_solid( &(leds[i][sin_theta]), BLOB_HEIGHT, CHSV(hue, 255, 255));
for (int j = 0; j < BLOB_HEIGHT; j++)
{
leds[i][sin_theta + j].fadeLightBy(direction ? j * 256 / BLOB_HEIGHT
: 255 - j * 256 / BLOB_HEIGHT);
}
}
hue++;
}
static uint8_t rgfade[8] = {0, 64, 128, 192, 224, 240, 248, 252 };
void Rotate()
{
static uint8_t t = 0; // 0 -> 31 time interval cycle of where the light should be pointed
CRGB rgbColor;
EVERY_N_MILLIS(25)
{
t = (t + 1) % 32;
}
for (int i = 0; i < 4; i++)
{
CRGB rgbColor = CRGB::Black;
int peak = i * 8; // the position where the light should be max brightness
int dist = abs(t - peak); // the distance from the peak
if (i == 0 && t > 24)
dist = 32 - t; // special case for the first strip so it wraps around
if (dist < 8)
{
rgbColor = pride_colors_rgb[color_index];
// dim it by the distance from 8
rgbColor.fadeLightBy(rgfade[dist]);
}
fill_solid(&(leds[i][0]), NUM_LEDS, rgbColor);
}
}
void Multihue()
{
static uint8_t hue = 0;
static long pushing = 0;
static long pushing_growth = 1;
static long fade_mode = 0; // when > 0, fade to black
if (fade_mode)
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < NUM_LEDS; j++)
leds[i][j].fadeToBlackBy( 32 );
fade_mode--;
if (fade_mode == 0)
{
for (int i = 0; i < 4; i++)
fill_solid(&(leds[i][0]), NUM_LEDS, CRGB::Black);
pushing = 0;
pushing_growth = 1;
}
}
else
{
// push in a random amount of new hue
long amt = random( pushing );
pushing += (pushing_growth++);
if (pushing > 200)
{
fade_mode = 25;
}
else
{
for (int i = 0; i < 4; i++)
{
// shift up everything by amt
memmove( &(leds[i][amt]), &(leds[i][0]), (NUM_LEDS - amt) * sizeof(CRGB) );
fill_solid(&(leds[i][0]), amt, CHSV(hue, 255, 255));
}
}
hue+=10;
}
}
void Fireworks(void)
{
static uint8_t hue = 0;
static long pushing = 0;
static long pushing_growth = 1;
static long firework_center = random( 25, NUM_LEDS - 25 );
static long amt_total = 0;
static long fade_mode = 0; // when > 0, fade to black
if (fade_mode)
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < NUM_LEDS; j++)
if (random(10) == 0) leds[i][j] = CRGB::Black;
fade_mode--;
if (fade_mode == 0)
{
for (int i = 0; i < 4; i++)
fill_solid(&(leds[i][0]), NUM_LEDS, CRGB::Black);
pushing = 0;
pushing_growth = 1;
amt_total = 0;
firework_center = random( 25, NUM_LEDS - 25 );
}
}
else
{
// how much new hue do we want to push in?
long amt = random( pushing );
// will that fit?
if (amt_total + amt + firework_center >= NUM_LEDS ||
firework_center - amt_total - amt < 0 ||
amt_total > 40)
{
// NO!
fade_mode = 25;
}
else
{
pushing += (pushing_growth++);
for (int i = 0; i < 4; i++)
{
// shift up out by amt
memmove( &(leds[i][firework_center + amt]), &(leds[i][firework_center]), amt_total * sizeof(CRGB) );
memmove( &(leds[i][firework_center - amt_total - amt]), &(leds[i][firework_center - amt_total]), amt_total * sizeof(CRGB) );
fill_solid(&(leds[i][firework_center - amt]), amt * 2, CHSV(hue, 255, 255));
}
amt_total += amt;
}
hue += 15;
}
}