/
jubilee.pde
383 lines (335 loc) · 9.93 KB
/
jubilee.pde
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
#include "LPD8806/LPD8806.h"
#include "SPI.h"
#include <avr/sleep.h>
#include "carrot.h"
//since data is on 11 and clock is on 13, we can use hardware SPI
LPD8806 strip = LPD8806(96);
int powerPin = 4;
int upModePin = 3;
int upColorPin = 2;
int upModeButtonState = HIGH;
int upModeButtonCycles = 0;
int upColorButtonState = HIGH;
int upColorButtonCycles = 0;
int CYCLES_DEBOUNCE = 2; //check the button for X ticks to see if it is bouncing
int MAX_COLORS = 8;
int MAX_MODES = 7;
int MAX_STRIPES = 5;
unsigned long tick = 0;
int mode = 1;
int color = 1;
uint16_t i, j, x, y ;
uint32_t c, d;
// Set the first variable to the NUMBER of pixels. 32 = 32 pixels in a row
// The LED strips are 32 LEDs per meter but you can extend/cut the strip
void ISR_Wake() {
detachInterrupt(0);
detachInterrupt(1);
}
void blackout() {
for(int i=0; i < strip.numPixels()+1; i++) {
strip.setPixelColor(i, strip.Color(0,0,0));
}
strip.show();
}
void triggerSleep() {
blackout();
attachInterrupt(0,ISR_Wake,LOW); //pin 2
attachInterrupt(1,ISR_Wake,LOW); //pin 3
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_mode();
//sleeping, until rudely interrupted
sleep_disable();
}
void triggerModeUp() {
++mode;
blackout();
}
void triggerColorUp() {
color++;
blackout();
}
void handleButtons() {
if(digitalRead(powerPin) == LOW) {
triggerSleep();
}
// software debounce
if(digitalRead(upModePin) != upModeButtonState) {
upModeButtonCycles++;
if(upModeButtonCycles > CYCLES_DEBOUNCE) {
upModeButtonCycles = 0;
upModeButtonState = digitalRead(upModePin);
if(upModeButtonState == LOW) {
triggerModeUp();
}
}
}
// software debounce
if(digitalRead(upColorPin) != upColorButtonState) {
upColorButtonCycles++;
if(upColorButtonCycles > CYCLES_DEBOUNCE) {
upColorButtonCycles = 0;
upColorButtonState = digitalRead(upColorPin);
if(upColorButtonState == LOW) {
triggerColorUp();
}
}
}
}
void handleStrip() {
switch(mode%MAX_MODES) {
case 0: //solid
c = GetColor(color%MAX_COLORS);
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
}
break;
case 1:
c = GetColor((tick%3+color)% MAX_COLORS);
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
}
break;
case 2:
if(tick % 15 == 0) {
c = GetColor(color%MAX_COLORS);
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
}
}
if(tick % 15 == 1) {
c = strip.Color(0,0,0);
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
}
}
break;
case 3: //chasers
d = (color / MAX_COLORS) % MAX_STRIPES + 1; //chaser
c = GetColor(color % MAX_COLORS); //color
j = tick % (strip.numPixels()/d);
for(i=0; i < strip.numPixels(); i++) {
if(i % (strip.numPixels()/d) == j) {
strip.setPixelColor(i, c);
}
else {
strip.setPixelColor(i, strip.Color(0,0,0));
}
}
break;
case 4: //chasers + statics
d = (color / MAX_COLORS) % MAX_STRIPES + 1; //chaser
c = GetColor(color % MAX_COLORS); //color
j = tick % (strip.numPixels()/d);
for(i=0; i < strip.numPixels(); i++) {
x = i % (strip.numPixels()/d);
if((x == j) || (x == 0)) {
strip.setPixelColor(i, c);
}
else {
strip.setPixelColor(i, strip.Color(0,0,0));
}
}
break;
case 5: //fuckin' rainbows
j = tick % 384;
for(i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 384 / strip.numPixels() * (color%MAX_COLORS)) + j) % 384));
}
break;
case 6: //carrot POV
j = tick % 158;
d = carrot[j];
//green //orange
c = (j < 30)?GetColor((color+2)%MAX_COLORS):GetColor((color+3)%MAX_COLORS);
for(i=0;i<32;i++) {
//adding 32 to the index makes it appear on the side opposite the controller
if(d & 0x00000001) {
strip.setPixelColor(i+32, c);
}
else {
strip.setPixelColor(i+32, strip.Color(0,0,0));
}
d >>= 1;
}
break;
}
strip.setPixelColor(strip.numPixels()-1, strip.Color(0,0,0)); //set that last LED off because it overlaps
strip.show();
}
void setup() {
// Start up the LED strip
strip.begin();
pinMode(powerPin, INPUT); // declare pushbutton as input
pinMode(upModePin, INPUT); // declare pushbutton as input
pinMode(upColorPin, INPUT); // declare pushbutton as input
triggerSleep();
}
void loop() {
tick++;
handleStrip();
handleButtons();
}
// fill the dots one after the other with said color
// good for testing purposes
void colorWipe(uint32_t c, uint8_t wait) {
int i;
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
// Chase a dot down the strip
// good for testing purposes
void colorChase(uint32_t c, uint8_t wait) {
int i;
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 0); // turn all pixels off
}
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c); // set one pixel
strip.show(); // refresh strip display
delay(wait); // hold image for a moment
strip.setPixelColor(i, 0); // erase pixel (but don't refresh yet)
}
strip.show(); // for last erased pixel
}
// An "ordered dither" fills every pixel in a sequence that looks
// sparkly and almost random, but actually follows a specific order.
void dither(uint32_t c, uint8_t wait) {
// Determine highest bit needed to represent pixel index
int hiBit = 0;
int n = strip.numPixels() - 1;
for(int bit=1; bit < 0x8000; bit <<= 1) {
if(n & bit) hiBit = bit;
}
int bit, reverse;
for(int i=0; i<(hiBit << 1); i++) {
// Reverse the bits in i to create ordered dither:
reverse = 0;
for(bit=1; bit <= hiBit; bit <<= 1) {
reverse <<= 1;
if(i & bit) reverse |= 1;
}
strip.setPixelColor(reverse, c);
strip.show();
delay(wait);
}
delay(250); // Hold image for 1/4 sec
}
// "Larson scanner" = Cylon/KITT bouncing light effect
void scanner(uint8_t r, uint8_t g, uint8_t b, uint8_t wait) {
int i, j, pos, dir;
pos = 0;
dir = 1;
for(i=0; i<((strip.numPixels()-1) * 8); i++) {
// Draw 5 pixels centered on pos. setPixelColor() will clip
// any pixels off the ends of the strip, no worries there.
// we'll make the colors dimmer at the edges for a nice pulse
// look
strip.setPixelColor(pos - 2, strip.Color(r/4, g/4, b/4));
strip.setPixelColor(pos - 1, strip.Color(r/2, g/2, b/2));
strip.setPixelColor(pos, strip.Color(r, g, b));
strip.setPixelColor(pos + 1, strip.Color(r/2, g/2, b/2));
strip.setPixelColor(pos + 2, strip.Color(r/4, g/4, b/4));
strip.show();
delay(wait);
// If we wanted to be sneaky we could erase just the tail end
// pixel, but it's much easier just to erase the whole thing
// and draw a new one next time.
for(j=-2; j<= 2; j++)
strip.setPixelColor(pos+j, strip.Color(0,0,0));
// Bounce off ends of strip
pos += dir;
if(pos < 0) {
pos = 1;
dir = -dir;
} else if(pos >= strip.numPixels()) {
pos = strip.numPixels() - 2;
dir = -dir;
}
}
}
// Sine wave effect
#define PI 3.14159265
void wave(uint32_t c, int cycles, uint8_t wait) {
float y;
byte r, g, b, r2, g2, b2;
// Need to decompose color into its r, g, b elements
g = (c >> 16) & 0x7f;
r = (c >> 8) & 0x7f;
b = c & 0x7f;
for(int x=0; x<(strip.numPixels()*5); x++)
{
for(int i=0; i<strip.numPixels(); i++) {
y = sin(PI * (float)cycles * (float)(x + i) / (float)strip.numPixels());
if(y >= 0.0) {
// Peaks of sine wave are white
y = 1.0 - y; // Translate Y to 0.0 (top) to 1.0 (center)
r2 = 127 - (byte)((float)(127 - r) * y);
g2 = 127 - (byte)((float)(127 - g) * y);
b2 = 127 - (byte)((float)(127 - b) * y);
} else {
// Troughs of sine wave are black
y += 1.0; // Translate Y to 0.0 (bottom) to 1.0 (center)
r2 = (byte)((float)r * y);
g2 = (byte)((float)g * y);
b2 = (byte)((float)b * y);
}
strip.setPixelColor(i, r2, g2, b2);
}
strip.show();
delay(wait);
}
}
/* Helper functions */
//Input a value 0 to 384 to get a color value.
//The colours are a transition r - g - b - back to r
uint32_t Wheel(uint16_t WheelPos)
{
byte r, g, b;
switch(WheelPos / 128)
{
case 0:
r = 127 - WheelPos % 128; // red down
g = WheelPos % 128; // green up
b = 0; // blue off
break;
case 1:
g = 127 - WheelPos % 128; // green down
b = WheelPos % 128; // blue up
r = 0; // red off
break;
case 2:
b = 127 - WheelPos % 128; // blue down
r = WheelPos % 128; // red up
g = 0; // green off
break;
}
return(strip.Color(r,g,b));
}
uint32_t GetColor(int c)
{
switch(c) {
case 0:
return strip.Color(127,0,0);
case 1:
return strip.Color(0,0,127);
case 2:
return strip.Color(0,127,0);
case 3:
return strip.Color(127,31,0);
case 4:
return strip.Color(127,127,0);
case 5:
return strip.Color(0,127,127);
case 6:
return strip.Color(127,0,127);
case 7:
return strip.Color(127,127,127);
default:
return strip.Color(0,0,0);
}
}