forked from Here-Be-Dragons/familamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfamilamp.ino
664 lines (584 loc) · 21.7 KB
/
familamp.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
#include "application.h"
#include "captouch.h"
#include "neopixel/neopixel.h"
SYSTEM_MODE(AUTOMATIC);
//PRODUCT_ID and PRODUCT_VERSION required for Particle's "Products" feature
PRODUCT_ID(639);
PRODUCT_VERSION(13);
CapTouch Touch(D3, D4);
#define PIXEL_COUNT 60
#define PIXEL_PIN D2
#define PIXEL_TYPE WS2812
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
////
// User Variables
////
uint32_t decayTime = 5;//2835; // Start dimming light after elapsed seconds
uint32_t decayDelay = 1;//15; // Seconds between decay steps
uint8_t nightHours[2] = {6, 21}; // Night mode starts at nightHours[1], ends at nightHours[0]
uint8_t duskHours[2] = { 7, 19 }; // Dusk mode starts at duskHours[1], ends at duskHours[0]. Needs to be inside nightHours' times.
// Day mode starts at duskHours[0], ends at duskHours[1]
uint16_t maxDayBrightness = 180; // 0 - 255, lamp will not exceed this during the day
uint16_t maxDuskBrightness = 70; // 0 - 255, lamp will not exceed this during dusk
uint16_t maxNightBrightness = 20; // 0 - 255, lamp will not exceed this during the night
float fadeRate = 0.95; // Fireworks Variable: 0.01-0.99, controls decay speed
////
// End User Variables
////
// German
int while_is_not_running = 1; //
const uint16_t TOUCH_SHORT = 100; // minimum short touch duration (ms)
const uint16_t TOUCH_LONG = 1000; // minimum long touch duration (ms)
const uint16_t TOUCH_TOO_LONG = 10000; // maximum long touch duration (ms)
const uint8_t TOUCH_TYPE_TBD = 0;
const uint8_t TOUCH_TYPE_SHORT = 1;
const uint8_t TOUCH_TYPE_LONG = 2;
time_t zero_time = 0;
time_t subtimer = 0;
time_t dt = 0;
time_t t_touching = 0 ;
bool touchSensor = false; // true indicates that touch has been sensed
time_t touchMillis = 0; // time when touch was first sensed
uint8_t touchType = 0; // indicates type of touch that was detected (values = TOUCH_TYPE_ constants)
bool actionPending = false; // true indicates taht an action is pending
uint8_t light_activated = 0;
//
double lastColorUpdate = 0; // Epoch of last color update (local or remote)
String colorFromID; // String, Tracks who sent the color (for debug)
uint16_t colorRecieved; // 0 - 255, Tracks the color received from another lamp
bool lampOn = 0; // Tracks if the lamp is lit
uint8_t activeColor = 0; // 0 - 255, Tracks what color is currently active (start at red)
uint8_t activeR = 255; // 0 - 255, Red component of activeColor;
uint8_t activeG = 0; // 0 - 255, Green component of activeColor;
uint8_t activeB = 0; // 0 - 255, Blue component of activeColor;
double lastDecayDelay = 0; // Time Tracker for decayDelay
uint16_t lampBrightness = 0; // 0 - 255, Tracks current lamp brightness
uint16_t maxBrightness = maxDayBrightness; // Assigned the current max brightness
uint8_t dayTrack = 0; // Track day/dusk/night condition
uint8_t fadeColor = 0; // Track color for special events
byte activePixels = 0; // Tracks number of active pixels, 0 is first pixel
float redStates[PIXEL_COUNT]; // Fireworks Variable
float blueStates[PIXEL_COUNT]; // Fireworks Variable
float greenStates[PIXEL_COUNT]; // Fireworks Variable
uint8_t heartbeatDirector = 0; // Heartbeat Tracking
uint8_t heartbeatColor = 0; // Heartbeat Tracking
CapTouch::Event touchEvent;
CapTouch::Event touchEvent2;
CapTouch::State touchingState;
void setup() {
// german
Serial.begin(9600);
while_is_not_running = 1;
// end
strip.begin();
strip.show();
rainbowFull(5, 0); // 5ms Delay, 0 is fade in
Touch.setup();
Time.zone(-5);
//Listen for other lamps to send a particle.publish()
Particle.subscribe("FamiLamp_Update", gotColorUpdate, MY_DEVICES);
rainbowFull(5, 2); // 5ms Delay, 2 is fade out
}
void loop() {
// begin default
//dayTracking();
//touchEvent = Touch.getEvent();
//if (touchEvent == CapTouch::TouchEvent) {
// whileTouching();
//}
//
//if (Time.now() - lastColorUpdate > decayTime && lampOn == 1) {
// if (Time.now() - lastDecayDelay >= decayDelay) {
// extinguish();
// lastDecayDelay = Time.now();
// }
// }
//end defult
// =========================================================================================================
// being test
dayTracking();
touchEvent2 = Touch.getEvent();
if (touchEvent2 == CapTouch::TouchEvent){
touchSensor = true;
zero_time = millis();
while (touchEvent2 != CapTouch::ReleaseEvent) {
t_touching = millis();
touchEvent2 = Touch.getEvent();
}
}
t_touching = t_touching - zero_time;
//Serial.printlnf("time touching %d",t_touching);
if (t_touching == 0 ){
// do nothing
} else if (t_touching > TOUCH_LONG){
Serial.printlnf("long in");
subtimer = millis();
dt = 0;
Serial.printlnf("time touching %d",t_touching);
Serial.printlnf("while long in %d",dt);
while (dt < t_touching){
touchEvent = CapTouch::TouchEvent;
whileTouching();
dt = millis() - subtimer;
Serial.printlnf("while long in %d",dt);
}
touchEvent = CapTouch::ReleaseEvent;
whileTouching();
lampOn =1;
} else if (t_touching> TOUCH_SHORT) {
uint32_t led_is_on = strip.getPixelColor(1);
Serial.printlnf("short in");
if ((led_is_on != 0)) {
//turning off
strip.clear();
strip.show();
Serial.printlnf("turning lamp off!");
}
//LEDs are off
if ((led_is_on == 0) ) {
//turning on
for(int i = 0; i <= strip.numPixels(); i++) {
strip.setPixelColor(strip.numPixels() - i, 255, 255, 255);
}
strip.show();
Serial.printlnf("turning lamp on!");
}
}
t_touching = 0;
zero_time = 0;
subtimer = 0;
dt = 0;
// To do the decay.
Serial.printlnf("decaying %d",lampOn);
Serial.printlnf("decaying lastcolor %d",lastColorUpdate);
Serial.printlnf("diff %d",(Time.now() - lastColorUpdate > decayTime));
Serial.printlnf("diff2 %d",(Time.now() - lastDecayDelay >= decayDelay));
if ((Time.now() - lastColorUpdate > decayTime) && (lampOn == 1)) {
if (Time.now() - lastDecayDelay >= decayDelay) {
Serial.printlnf("in decay %d",(Time.now() - lastDecayDelay ));
extinguish();
lastDecayDelay = Time.now();
//lampOn = 0;
}
}
// end test
// =========================================================================================================
// Special idle functions
if (lampOn == 0) {
// Christmas Day
if (Time.day() == 25 && Time.month() == 12) {
idleColorFader(0,85);
}
// St. Patricks Day
if (Time.day() == 17 && Time.month() == 3) {
idleColorFlicker(21);
}
// Valentines Day
if (Time.day() == 14 && Time.month() == 2) {
idleColorFlicker(106);
}
// 4th of July
if ( Time.day() == 4 && Time.month() == 7 ) {
idleFireworks(0);
}
// New Years Day
if ( Time.day() == 1 && Time.month() == 1 ) {
idleFireworks(1);
}
// Birthdays
if (
(Time.day() == 22 && Time.month() == 2) ||
(Time.day() == 24 && Time.month() == 2)
)
{
idleDisco();
}
// Unassigned Heartbeat
/*if (Time.day() == 25 && Time.month() == 1) {
idleHeartbeat();
}*/
// Clear any previous day's special idles
if (lampBrightness != 0 && Time.hour() == 0 && Time.minute() == 0 && Time.second() <= 3) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 0, 0, 0);
}
strip.show();
lampBrightness = 0;
}
}
}
void whileTouching() {
byte previousBrightness = lampBrightness; // Store the previous brightness in case we need it later
uint16_t pixelBrightness = lampBrightness; // Tracks the given pixel's brightness. Needs to track > 255, so uint16_t
uint8_t testColor = activeColor; // Start with the current color
activePixels = 0;
while (touchEvent != CapTouch::ReleaseEvent) {
for (byte i = 0; i <= activePixels; i++) {
pixelBrightness = lampBrightness + i; //Fade to full brightness
if (pixelBrightness > maxBrightness) pixelBrightness = maxBrightness; //catch overflow
// "activePixels - i" reverses the direction
strip.setPixelColor(activePixels - i, wheelColor(((i * 60 / strip.numPixels()) + testColor) & 255, pixelBrightness)); // "& 255" AKA bitwise and prevents overflow
}
strip.show();
testColor++; //because testColor is uint8_t, automatically loops at 256
if(activePixels < (strip.numPixels() - 1)) activePixels++; //Add 1 for next iteration, but prevent looping around
if(lampBrightness < maxBrightness) lampBrightness++;
touchEvent = Touch.getEvent();
delay(3);
}
if (activePixels >= (strip.numPixels() - 10)) {
lampOn = 1;
activeColor = testColor;
sendColorUpdate();
lastColorUpdate = Time.now();
activePixels = strip.numPixels();
} else {
lampBrightness = previousBrightness;
setColorFade(activeColor);
}
}
void sendColorUpdate() {
String sColor = String(activeColor);
Particle.publish("FamiLamp_Update", System.deviceID() + "~" + sColor, 60, PRIVATE);
}
void gotColorUpdate(const char *name, const char *data) {
String str = String(data);
char strBuffer[40] = "";
str.toCharArray(strBuffer, 40);
colorFromID = strtok(strBuffer, "~");
colorRecieved = atof(strtok(NULL, "~"));
lampBrightness = maxBrightness;
setColorDither(colorRecieved);
// DEBUG
String sColorRecieved = String(colorRecieved);
Particle.publish("Color_Recieved", System.deviceID() + "~" + sColorRecieved);
// END DEBUG
lampOn = 1;
lastColorUpdate = Time.now();
}
void setColorFade(byte c) { // c is color. This function does a smooth fade to a new color
if (((Time.month() * Time.day()) % 256) == c) { // Semi-random formula to trigger easter egg
rainbowEasterEgg();
} else {
uint8_t currR, currG, currB, endR, endG, endB;
uint32_t color = wheelColor(c, lampBrightness);
endR = (uint8_t)((color >> 16) & 0xff); // Splits out new color into separate R, G, B
endG = (uint8_t)((color >> 8) & 0xff);
endB = (uint8_t)(color & 0xff);
for (uint16_t fade = 0; fade < 255; fade++) {
for (uint16_t j = 0; j < strip.numPixels(); j++) {
long startRGB = strip.getPixelColor(j); // Get pixel's starting color
currR = (uint8_t)((startRGB >> 16) & 0xff); // Splits out current color into separate R, G, B
currG = (uint8_t)((startRGB >> 8) & 0xff);
currB = (uint8_t)(startRGB & 0xff);
endR = currR + (endR - currR) * fade / 255; // Color mixer
endG = currG + (endG - currG) * fade / 255;
endB = currB + (endB - currB) * fade / 255;
strip.setPixelColor(j, endR, endG, endB);
}
strip.show();
delay(10);
}
}
activeColor = c;
}
void setColorDither(byte c) { // c is color. This function does a "random dither" to set the new color
if (((Time.month() * Time.day()) % 256) == c) { // Semi-random formula to trigger easter egg
rainbowEasterEgg();
} else {
// Determine highest bit needed to represent pixel index
uint32_t color = wheelColor(c, lampBrightness);
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, color);
strip.show();
delay(20);
}
}
activeColor = c;
}
void extinguishOld() { //Dims the lamp by one unit until lampBrightness is 0 and lampOn is 0
lampBrightness--;
if (((Time.month() * Time.day()) % 256) == activeColor) { // Semi-random formula to trigger easter egg
rainbowEasterEgg();
} else {
uint32_t color = wheelColor(activeColor, lampBrightness);
for (byte j = 0; j <= strip.numPixels(); j++) {
strip.setPixelColor(j, color);
}
strip.show();
if (lampBrightness <= 0) {
lampOn = 0; //If the lamp is completely off, set lampOn to 0
lampBrightness = 0; // Make sure this number isn't negative somehow
}
}
}
void extinguish() { //Turns off one pixel at a time until they're all off
activePixels--;
for(int i = 0; i <= strip.numPixels() - activePixels; i++) {
strip.setPixelColor(strip.numPixels() - i, 0, 0, 0);
}
strip.show();
}
uint32_t wheelColor(uint16_t WheelPos, uint16_t iBrightness) {
float R, G, B;
float brightness = iBrightness / 255.0;
if (WheelPos < 85) {
R = WheelPos * 3;
G = 255 - WheelPos * 3;
B = 0;
} else if (WheelPos < 170) {
WheelPos -= 85;
R = 255 - WheelPos * 3;
G = 0;
B = WheelPos * 3;
} else {
WheelPos -= 170;
R = 0;
G = WheelPos * 3;
B = 255 - WheelPos * 3;
}
activeR = R * brightness;// + .5;
activeG = G * brightness;// + .5;
activeB = B * brightness;// + .5;
return strip.Color((byte) activeR,(byte) activeG,(byte) activeB);
}
void rainbowFull(byte wait, byte fade) {
uint16_t i, j, k;
if(fade == 0) k = 0;
else k = maxBrightness;
for(j = 0; j <= 255; j++) {
for(i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor((strip.numPixels() - 1) - i, wheelColor(((i * 60 / strip.numPixels()) + j) & 255, k));
}
strip.show();
delay(wait);
if(fade == 0 && k < maxBrightness) {
k++;
}
if(fade == 2 && k > 0) {
k--;
}
}
}
void rainbowEasterEgg() {
for(uint8_t i = 0; i <= strip.numPixels(); i++) {
strip.setPixelColor(i, wheelColor((i * 256 / strip.numPixels()) & 255, lampBrightness));
}
strip.show();
}
void dayTracking() {
if (Time.hour() < nightHours[0] || Time.hour() >= nightHours[1]) { // Night hours
if (dayTrack != 2) {
maxBrightness = maxNightBrightness;
if (lampBrightness > maxBrightness) lampBrightness = maxBrightness;
setColorFade(activeColor);
dayTrack = 2;
}
} else if (Time.hour() < duskHours[0] || Time.hour() >= duskHours[1]) { // Dusk hours
if (dayTrack != 1) {
maxBrightness = maxDuskBrightness;
if (lampBrightness > maxBrightness) lampBrightness = maxBrightness;
setColorFade(activeColor);
dayTrack = 1;
}
} else { // Everything else is day
if (dayTrack != 0) {
maxBrightness = maxDayBrightness;
if (lampBrightness > maxBrightness) lampBrightness = maxBrightness;
setColorFade(activeColor);
dayTrack = 0;
}
}
}
void idleColorFader(uint8_t c1, uint8_t c2) {
lampBrightness = 40;
if ( maxBrightness < lampBrightness ) {
lampBrightness = maxBrightness;
}
uint16_t currR, currG, currB, endR, endG, endB;
uint32_t color = wheelColor(fadeColor, lampBrightness);
endR = (uint16_t)((color >> 16) & 0xff); // Splits out new color into separate R, G, B
endG = (uint16_t)((color >> 8) & 0xff);
endB = (uint16_t)(color & 0xff);
for (uint16_t j = 0; j < activePixels; j++) {
long startRGB = strip.getPixelColor(j); // Get pixel's current color
currR = (uint16_t)((startRGB >> 16) & 0xff); // Splits out current color into separate R, G, B
currG = (uint16_t)((startRGB >> 8) & 0xff);
currB = (uint16_t)(startRGB & 0xff);
if ( currR > endR ) {
currR = currR - 1;
} else if ( currR < endR ) {
currR = currR + 1;
} else {
currR = endR;
}
if ( currG > endG ) {
currG = currG - 1;
} else if ( currG < endG ) {
currG = currG + 1;
} else {
currG = endG;
}
if ( currB > endB ) {
currB = currB - 1;
} else if ( currB < endB ) {
currB = currB + 1;
} else {
currB = endB;
}
//Catch overflows
currR %= 255;
currG %= 255;
currB %= 255;
strip.setPixelColor(j, currR, currG, currB);
if ( j >= strip.numPixels() - 1 && endR == currR && endG == currG && endB == currB) {
if ( fadeColor == c1 ) {
fadeColor = c2;
} else {
fadeColor = c1;
}
activePixels = 0;
}
}
strip.show();
if ( activePixels < strip.numPixels() ) activePixels++;
delay(20);
}
void idleFireworks(uint8_t w) {
// w = 0 for mulitcolor, w = 1 for all white flashes
lampBrightness = 40;
if ( maxBrightness < lampBrightness ) {
lampBrightness = maxBrightness;
}
if (random(20) == 1) {
uint16_t i = random(strip.numPixels());
if (redStates[i] < 1 && greenStates[i] < 1 && blueStates[i] < 1) {
if (w == 0){
redStates[i] = random(lampBrightness);
greenStates[i] = random(lampBrightness);
blueStates[i] = random(lampBrightness);
} else {
redStates[i] = lampBrightness;
greenStates[i] = lampBrightness;
blueStates[i] = lampBrightness;
}
}
}
for(uint16_t l = 0; l < strip.numPixels(); l++) {
if (redStates[l] > 1 || greenStates[l] > 1 || blueStates[l] > 1) {
strip.setPixelColor(l, redStates[l], greenStates[l], blueStates[l]);
if (redStates[l] > 1) {
redStates[l] = redStates[l] * fadeRate;
} else {
redStates[l] = 0;
}
if (greenStates[l] > 1) {
greenStates[l] = greenStates[l] * fadeRate;
} else {
greenStates[l] = 0;
}
if (blueStates[l] > 1) {
blueStates[l] = blueStates[l] * fadeRate;
} else {
blueStates[l] = 0;
}
} else {
strip.setPixelColor(l, 0, 0, 0);
}
}
strip.show();
}
void idleDisco() {
lampBrightness = 20;
if ( maxBrightness < lampBrightness ) {
lampBrightness = maxBrightness;
}
for(int i=0; i<strip.numPixels(); i++) {
int randr = random(0,lampBrightness);
int randg = random(0,lampBrightness);
int randb = random(0,lampBrightness);
int randi = random(0,(strip.numPixels() - 1));
strip.setPixelColor(randi, randr, randg, randb);
strip.show();
delay(5);
}
}
void idleColorFlicker(uint8_t c) {
lampBrightness = 20;
if ( maxBrightness < lampBrightness ) {
lampBrightness = maxBrightness;
}
uint32_t color = wheelColor(c, lampBrightness);
for(uint8_t i=0; i<strip.numPixels(); i++) {
uint8_t flicker = random(0,10);
int flickerR = (uint16_t)((color >> 16) & 0xff) - flicker; // Splits out new color into separate R, G, B
int flickerG = (uint16_t)((color >> 8) & 0xff) - flicker;
int flickerB = (uint16_t)(color & 0xff) - flicker;
if(flickerR<0) flickerR=0;
if(flickerG<0) flickerG=0;
if(flickerB<0) flickerB=0;
strip.setPixelColor(i, flickerR, flickerG, flickerB);
}
strip.show();
delay(30);
}
void idleHeartbeat() {
lampBrightness = 20;
if ( maxBrightness < lampBrightness ) {
lampBrightness = maxBrightness;
}
uint8_t endColor = 0;
if( heartbeatDirector == 0 ) {
endColor = lampBrightness * 0.6;
}else if( heartbeatDirector == 1 ) {
endColor = lampBrightness * 0.2;
}else if( heartbeatDirector == 2 ) {
endColor = lampBrightness;
}else if( heartbeatDirector == 3 ) {
endColor = lampBrightness * 0.12;
} else {
//do nothing, this will delay
}
if( heartbeatColor < endColor ) {
for(int j=heartbeatColor; j<endColor; j+=4) {
for(int i=25; i<35; i++) {
strip.setPixelColor(i, j, 0, 0);
}
strip.show();
delay(15);
}
} else if ( heartbeatColor > endColor ) {
for(int j=heartbeatColor; j>endColor; j--) {
for(int i=25; i<35; i++) {
strip.setPixelColor(i, j, 0, 0);
}
strip.show();
delay(30);
}
} else {
delay(15);
delay(15);
delay(15);
delay(15);
delay(15);
delay(15);
delay(15);
}
heartbeatColor = endColor;
heartbeatDirector++;
heartbeatDirector%=4;
}