-
Notifications
You must be signed in to change notification settings - Fork 2
/
dancing_lights2.ino
426 lines (369 loc) · 9.37 KB
/
dancing_lights2.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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#include <rotary.h>
#define HEART 1
//#define BRAIN 1
#include <WriteLight.h>
#include <Metro.h>
#include <SoftwareSerial.h>
#ifdef BRAIN
#define MODE (LIGHT_MODE_LOCAL_WRITE | LIGHT_MODE_SERIAL_DEBUG | LIGHT_MODE_SERIAL_READ)
#define FAKE false
#define BUTTON 31
// #define SERIAL Serial1
#else //
#define MODE (LIGHT_MODE_SERIAL_DEBUG| LIGHT_MODE_SERIAL_WRITE | LIGHT_MODE_ANIMATE)
#define FAKE false
#define BUTTON A0
// #define SERIAL Serial2
#endif
WriteLight light1(LIGHT_UNIT_1);
WriteLight light2(LIGHT_UNIT_2);
WriteLight light3(LIGHT_UNIT_3);
WriteLight light4(LIGHT_UNIT_4);
WriteLight lights(LIGHT_UNIT_ALL);
// LED Intensities
//int rInt = 255;
//int gInt = 255;
//int bInt = 255;
const int INPUT_MAX(500);
const int RANGE_MAX(255);
// Rotary Input
// the order selects the pin assignment
#ifdef HEART
Rotary redRotary(INPUT_MAX, RANGE_MAX, FAKE); // pins 2,3 (A,B) A is yellow, B is purple
Rotary greenRotary(INPUT_MAX, RANGE_MAX, FAKE); // pins 21,20 (A,B)
Rotary blueRotary(INPUT_MAX, RANGE_MAX, FAKE); // pins 19,18 (A,B)
#else
class rotary {
public:
int value(void){};
void loop(){};
void setup(rotary&){};
};
rotary redRotary;
rotary greenRotary;
rotary blueRotary;
#endif
// black ground
// gray channel A
// red vcc
// blue channel B
// Pot Inputs
//int rInp(A0);
//int gInp(A1);
//int bInp(A2);
// Mic Inputs
//int rMicInp(A0);
//int gMicInp(A1);
//int bMicInp(A2);
// Array Address Constants
// Color:
//const int red = 0;
//const int green = 1;
//const int blue = 2;
enum ANIMATION_STATE {
ANIMATION_START=0,
ANIMATION_GLOW=0,
ANIMATION_PULSE,
ANIMATION_ON,
ANIMATION_BLINK,
ANIMATION_POP,
// ANIMATION_LISTEN,
// ANIMATION_CYCLE,
ANIMATION_RANDOM,
ANIMATION_COP,
ANIMATION_OFF,
ANIMATION_END
} g_state(ANIMATION_GLOW);
// animation functions
void glow();
void blink();
void pulse();
void pulse();
void pop_lights();
void listen();
void cycle();
void random_lights();
void lights_off();
void cop();
void lights_on();
// Button Variables (including debouncing)
int button_pin(BUTTON);
bool button_pressed(false);
bool debouncing(false);
unsigned long debounce_time;
const unsigned long debounce_timeout(50);
void debounce();
void handleButton();
void reportState(void);
void debounce() {
int button = analogRead(button_pin);
// Serial.println(button,DEC);
bool current_state(button > 500);
if(debouncing) {
if(millis() > debounce_time + debounce_timeout) {
debouncing = false;
// Serial.println(current_state);
if(current_state != button_pressed) {
button_pressed = current_state;
// Serial.print("DEBOUNCED: ");
if(button_pressed) {
// Serial.println("PRESSED");
handleButton();
} else {
// Serial.println("RELEASED");
//handleButton();
}
}
}
} else {
if(current_state != button_pressed) {
// Serial.println("Debouncing");
debouncing = true;
debounce_time = millis();
}
}
}
void handleButton(void) {
int state(g_state);
int end_state(ANIMATION_END);
state++;
state = state % end_state;
g_state = (ANIMATION_STATE)state;
dumpState();
}
Metro random_period(3000);
//int redMic() {
// analogReference(INTERNAL);
// analogRead(rMicInp);
// int retval(map(analogRead(rMicInp), 0, 1023, 0, 254));
// analogReference(DEFAULT);
// #ifdef UNO
// Serial.print(" r");
// Serial.print(retval);
// Serial.print(" ");
// #endif
// return retval;
//}
//int greenMic() {
// analogReference(INTERNAL);
// analogRead(gMicInp);
// int retval(map(analogRead(gMicInp), 0, 1023, 0, 254));
// analogReference(DEFAULT);
// #ifdef UNO
// Serial.print(" g");
// Serial.print(retval);
// Serial.print(" ");
// #endif
// return retval;
//}
//int blueMic() {
//
// analogReference(INTERNAL);
// analogRead(bMicInp);
// int retval(map(analogRead(bMicInp), 0, 1023, 0, 254));
// analogReference(DEFAULT);
// #ifdef UNO
// Serial.print(" b");
// Serial.print(retval);
// Serial.print(" ");
// #endif
// return retval;
//}
//Metro glowTime(50);
void glow() {
// if(!glowTime.check()) return;
// Serial.println("glowing");
int r;
lights.write(redRotary.value(),greenRotary.value(),blueRotary.value());
}
Metro blinkTime(100);
bool blinkon(false);
void blink() {
if(!blinkTime.check()) return;
// Serial.println("blinking");
blinkon = !blinkon;
if(blinkon) {
lights.write(0,0,0);
} else {
lights.write(redRotary.value(),
greenRotary.value(),
blueRotary.value());
}
}
//Metro pulseTime(1);
//float pulseCount(1);
int pulseDir(1);
const float pulse_period(5000);
//unsigned long pulse_start(0);
void pulse() {
// if(!pulseTime.check()) return;
// Serial.println("pulsing");
unsigned long now=millis();
float delta(now % (int)pulse_period);
float factor;
if(delta < pulse_period/2) {
factor = 2.0/pulse_period*delta;
} else {
factor = 2.0-(2.0/(pulse_period*delta));
}
//lights.write(factor*redNob(),
// factor*greenNob(),
// factor*blueNob()
lights.write(factor*redRotary.value(),
factor*greenRotary.value(),
factor*blueRotary.value());
}
//float popCount(0);
float pop_period(5000);
//Metro popTime(10);
void pop_lights() {
unsigned long now=millis();
float delta(now % (int)pop_period);
float factor;
factor = delta/pop_period;
lights.write(factor*redRotary.value(),
factor*greenRotary.value(),
factor*blueRotary.value());
}
void listen() {
//
// lights.write(redMic(),greenMic(),blueMic());
// endColor();
}
//void randomWash () {
// // every period, choose a new color at random and transition to it
// // the period has a duty cycle where "on" is transitioning and off is stable at the current color
//
// // for the transition, change the lead light
// // toward the target color by the percentage of the duty cycle
//
// // change the following leads by the percentage as if it were X seconds in the past
// int period(2000);
// int duty(333);
// int following(50);
//
// int cycle_start;
//
//
//}
int period(500);
void random_lights() {
if(!random_period.check()) {
return;
}
Serial.println("random lighting");
period++;
int r,g,b;
int target;
// Serial.println(period);
target = (int)round(random(3));
r = (int)round(random(255));
g = (int)round(random(255));
b = (int)round(random(255));
//char buffer[20];
//sprintf(buffer,"rnd %d %d %d %d\n",target,r,g,b);
//Serial.print(buffer);
switch(target) {
case 0: light1.write(r,g,b); break;
case 1: light2.write(r,g,b); break;
case 2: light3.write(r,g,b); break;
case 3: light4.write(r,g,b); break;
}
}
Metro animation_timer(1000/10); // 100 frames/second
void animate() {
if(!animation_timer.check())
return;
switch(g_state) {
case ANIMATION_ON : lights_on(); break;
case ANIMATION_GLOW: glow(); break;
case ANIMATION_BLINK: blink(); break;
case ANIMATION_PULSE: pulse(); break;
case ANIMATION_POP: pop_lights(); break;
// case ANIMATION_LISTEN: listen(); break;
// case ANIMATION_CYCLE: listen(); break;
case ANIMATION_RANDOM: random_lights(); break;
case ANIMATION_OFF: lights_off();break;
case ANIMATION_COP: cop();break;
default:
Serial.print("ERROR: unknown state in animate");
Serial.println(g_state);
}
return;
}
Metro light_time(100);
void lights_on(void) {
if(!light_time.check()) {
return;
}
lights.write(255,255,255);
}
void lights_off(void) {
if(!light_time.check()) {
return;
}
lights.write(0,0,0);
}
Metro cop_timer(300); // alernate about every half second
bool cop_state(false);
void cop(void) {
if(!cop_timer.check())
return;
if(cop_state) {
light1.write(255,0,0); // 1/3 alternate red
light2.write(0,0,255); // 2/4 alternate blue
light3.write(0,0,0);
light4.write(0,0,0);
} else {
light1.write(0,0,0);
light2.write(0,0,0);
light3.write(255,0,0); // 1/3 alternate red
light4.write(0,0,255); // 2/4 alternate blue
cop_state=!cop_state;
}
}
void setup() {
delay(100);
Serial.begin(115200); // &Serial
Serial1.begin(9600);
Serial2.begin(9600);
WriteLight::setup(MODE); // sets up the output pins
redRotary.setup(redRotary);
greenRotary.setup(greenRotary);
blueRotary.setup(blueRotary);
Serial.print("SETUP:");
Serial.println(MODE);
// and set up the button
pinMode(button_pin, INPUT);
}
void loop() {
debounce();
if(WriteLight::isAnimated()) {
// if(MODE & LIGHT_MODE_SERIAL_DEBUG)
dumpState();
animate();
}
WriteLight::loop(); // call library to read the serial if necessary
redRotary.loop();
greenRotary.loop();
blueRotary.loop();
}
void dumpState(void) {
Serial.print("\t\t\t");
switch(g_state) {
case ANIMATION_ON : Serial.println("lights_on()"); break;
case ANIMATION_GLOW: Serial.println("glow()"); break;
case ANIMATION_BLINK: Serial.println("blink()"); break;
case ANIMATION_PULSE: Serial.println("pulse()"); break;
case ANIMATION_POP: Serial.println("pop_lights()"); break;
// case ANIMATION_LISTEN: Serial.println("listen()"); break;
// case ANIMATION_CYCLE: listen(); break;
case ANIMATION_COP: Serial.println("cop!");break;
case ANIMATION_RANDOM: Serial.println("random_lights()"); break;
case ANIMATION_OFF: Serial.println("lights_off()");break;
default:
Serial.print("ERROR: unknown state in dumpstate: ");
Serial.println(g_state);
}
return;
}