-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathword_clock.ino
More file actions
713 lines (612 loc) · 17.7 KB
/
Copy pathword_clock.ino
File metadata and controls
713 lines (612 loc) · 17.7 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
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
// Pin assignments for LED rows
// Rows are counted top to bottom
#define ROW_1 13
#define ROW_2 12
#define ROW_3 11
#define ROW_4 10
#define ROW_5 9
#define ROW_6 8
#define ROW_7 7
#define ROW_8 6
// Pin assignments for controls
#define MODE_BUTTON 5
#define HOUR_BUTTON 4
#define MINUTE_BUTTON 3
// Number of LEDs in a strip
#define NUM_LED 13
// Brightness constant - should be in the range [0, 255]
// Define based on power availiability
#define BRIGHTNESS 32
// How frequently we should query the RTC for the time
#define RTC_QUERY_PERIOD_MILLIS 5000UL
// How often to check the switch state
#define SWITCH_QUERY_PERIOD_MILLIS 1000UL
// TIME MODE constants
#define TIME_AUTO_OFF 22 // 11pm
#define TIME_AUTO_ON 8 // 8am
// Special Day constants
#define SPECIAL_DAY 28
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip_1 = Adafruit_NeoPixel(NUM_LED, ROW_1);
Adafruit_NeoPixel strip_2 = Adafruit_NeoPixel(NUM_LED, ROW_2);
Adafruit_NeoPixel strip_3 = Adafruit_NeoPixel(NUM_LED, ROW_3);
Adafruit_NeoPixel strip_4 = Adafruit_NeoPixel(NUM_LED, ROW_4);
Adafruit_NeoPixel strip_5 = Adafruit_NeoPixel(NUM_LED, ROW_5);
Adafruit_NeoPixel strip_6 = Adafruit_NeoPixel(NUM_LED, ROW_6);
Adafruit_NeoPixel strip_7 = Adafruit_NeoPixel(NUM_LED, ROW_7);
Adafruit_NeoPixel strip_8 = Adafruit_NeoPixel(NUM_LED, ROW_8);
// Colors
uint32_t white = strip_1.Color(255, 255, 255);
uint32_t off = strip_1.Color(0, 0, 0);
uint32_t red = strip_1.Color(255, 0, 0);
uint32_t green = strip_1.Color(0, 255, 0);
uint32_t blue = strip_1.Color(0, 0, 255);
uint32_t lightBlue = strip_1.Color(8, 94, 154);
uint32_t pink = strip_1.Color(255, 0, 198);
// Global variables
static unsigned long lastRTCQueryTime = 0; // the last time we queried the clock for the time
static unsigned int currentHour = 0; // the hour that will be displayed on the clock
static unsigned int currentMinute = 0; // the minute that will be used to calculate what to show on the clock
static unsigned int currentHour24 = 0; // the hour (in 24 hour time) used for TIME MODE
static unsigned int currentDayOfMonth = 0; // the day of the month, used for lighting dead letters on a specifc day
// Globals for mode button
int modeButtonState = LOW;
int lastModeButtonState = LOW;
static unsigned int displayMode = 1; // 0 = off, 1 = on, 2 = time; default to on
unsigned long lastModeDebounceTime = 0;
unsigned long modeDebounceDelay = 100;
bool shouldDisplayTimeBool = false;
// Globals for hour button
static unsigned int hourOffset = 0; // This gets added to the hour taken from the clock, used for daylight savings and such. Taken mod 12
int hourButtonState = LOW;
int lastHourButtonState = LOW;
unsigned long lastHourDebounceTime = 0;
unsigned long hourDebounceDelay = 100;
// Globals for minute button
static unsigned int minuteOffset = 0; // Added to the minute taken from the clock, mod 60
int minuteButtonState = LOW;
int lastMinuteButtonState = LOW;
unsigned long lastMinuteDebounceTime = 0;
unsigned long minuteDebounceDelay = 100;
void setup() {
// Init all 8 strands of LEDs
// Use half brightness due to power concerns
strip_1.begin();
strip_1.setBrightness(BRIGHTNESS);
strip_1.show();
strip_2.begin();
strip_2.setBrightness(BRIGHTNESS);
strip_2.show();
strip_3.begin();
strip_3.setBrightness(BRIGHTNESS);
strip_3.show();
strip_4.begin();
strip_4.setBrightness(BRIGHTNESS);
strip_4.show();
strip_5.begin();
strip_5.setBrightness(BRIGHTNESS);
strip_5.show();
strip_6.begin();
strip_6.setBrightness(BRIGHTNESS);
strip_6.show();
strip_7.begin();
strip_7.setBrightness(BRIGHTNESS);
strip_7.show();
strip_8.begin();
strip_8.setBrightness(BRIGHTNESS);
strip_8.show();
// Clock Setup
Serial.begin(9600);
delay(3000); // wait for console opening
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
// Control Setup
pinMode(MODE_BUTTON, INPUT);
clearAll();
showAll();
}
void loop() {
handleHourAdjust();
handleMinuteAdjust();
handleModeButton();
unsigned long timeNow = millis();
if ((timeNow - lastRTCQueryTime) > RTC_QUERY_PERIOD_MILLIS) {
lastRTCQueryTime = timeNow;
readTime();
}
if (shouldDisplayTimeBool) {
// the user wants the time to be displayed
clearAll(); // start with a clean slate
showTime((currentHour + hourOffset) % 12, (currentMinute + minuteOffset) % 60, lightBlue);
if (currentDayOfMonth == SPECIAL_DAY) {
lightDeadLetters(pink);
}
showAll();
} else {
clearAll();
showAll();
}
// testMatrix();
delay(1); // don't want to burn out the chip
}
// Handle input from the buttons to adjust the time
void handleHourAdjust() {
// will update the hourOffset global based on user button press
int reading = digitalRead(HOUR_BUTTON);
if (reading != lastHourButtonState) {
lastHourDebounceTime = millis();
}
if ((millis() - lastHourDebounceTime) > hourDebounceDelay) {
if (reading != hourButtonState) {
hourButtonState = reading;
if (hourButtonState == HIGH) {
hourOffset++;
hourOffset = hourOffset % 12;
}
}
}
//Serial.println(currentHour + hourOffset);
lastHourButtonState = reading;
}
void handleMinuteAdjust() {
// will update the minuteOffset global based on user button press
int reading = digitalRead(MINUTE_BUTTON);
if (reading != lastMinuteButtonState) {
lastMinuteDebounceTime = millis();
}
if ((millis() - lastMinuteDebounceTime) > minuteDebounceDelay) {
if (reading != minuteButtonState) {
minuteButtonState = reading;
if (minuteButtonState == HIGH) {
minuteOffset++;
minuteOffset = minuteOffset % 60;
}
}
}
Serial.println((currentMinute + minuteOffset) % 60);
lastMinuteButtonState = reading;
}
// Handle input from the light on/off/time button
bool handleModeButton() {
int reading = digitalRead(MODE_BUTTON);
// if (reading == HIGH) {
// Serial.println("high");
// }
if (reading != lastModeButtonState) {
lastModeDebounceTime = millis();
}
if ((millis() - lastModeDebounceTime) > modeDebounceDelay) {
if (reading != modeButtonState) {
modeButtonState = reading;
if (modeButtonState == HIGH) {
displayMode++;
displayMode = displayMode % 3;
Serial.println(displayMode);
}
}
}
lastModeButtonState = reading;
switch (displayMode) {
case 0:
shouldDisplayTimeBool = false;
break;
case 1:
shouldDisplayTimeBool = true;
break;
case 2:
if (currentHour24 > TIME_AUTO_OFF || currentHour24 < TIME_AUTO_ON) {
shouldDisplayTimeBool = false;
} else {
shouldDisplayTimeBool = true;
}
break;
default:
break;
}
}
void turnOffStrip(Adafruit_NeoPixel strip) {
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, off);
}
strip.show();
}
// Test the entire matrx by flashing all LEDs green, then red, then blue
void testMatrix() {
for (int j = 0; j < 3; j++) {
for (int i = 0; i < NUM_LED; i++) {
switch (j) {
case 0:
strip_1.setPixelColor(i, green);
strip_2.setPixelColor(i, green);
strip_3.setPixelColor(i, green);
strip_4.setPixelColor(i, green);
strip_5.setPixelColor(i, green);
strip_6.setPixelColor(i, green);
strip_7.setPixelColor(i, green);
strip_8.setPixelColor(i, green);
break;
case 1:
strip_1.setPixelColor(i, red);
strip_2.setPixelColor(i, red);
strip_3.setPixelColor(i, red);
strip_4.setPixelColor(i, red);
strip_5.setPixelColor(i, red);
strip_6.setPixelColor(i, red);
strip_7.setPixelColor(i, red);
strip_8.setPixelColor(i, red);
break;
case 2:
strip_1.setPixelColor(i, blue);
strip_2.setPixelColor(i, blue);
strip_3.setPixelColor(i, blue);
strip_4.setPixelColor(i, blue);
strip_5.setPixelColor(i, blue);
strip_6.setPixelColor(i, blue);
strip_7.setPixelColor(i, blue);
strip_8.setPixelColor(i, blue);
break;
default:
break;
}
}
strip_1.show();
strip_2.show();
strip_3.show();
strip_4.show();
strip_5.show();
strip_6.show();
strip_7.show();
strip_8.show();
delay(1000);
}
}
void showTime(int hour, int minute, uint32_t col) {
lightItIs(col);
if (minute < 5) {
lightOClock(col);
} else if (minute >= 5 && minute < 10) {
lightFiveTop(col);
lightMinutes(col);
} else if (minute >= 10 && minute < 15) {
lightMinutes(col);
lightTenTop(col);
} else if (minute >= 15 && minute < 20) {
lightQuarter(col);
} else if (minute >= 20 && minute < 25) {
lightTwenty(col);
lightMinutes(red);
} else if (minute >= 25 && minute < 30) {
lightTwenty(col);
lightFiveTop(col);
lightMinutes(col);
} else if (minute >= 30 && minute < 35) {
lightHalf(col);
} else if (minute >= 35 && minute < 40) {
lightTwenty(col);
lightFiveTop(col);
lightMinutes(col);
} else if (minute >= 40 && minute < 45) {
lightTwenty(col);
lightMinutes(col);
} else if (minute >= 45 && minute < 50) {
lightQuarter(col);
} else if (minute >= 50 && minute < 55) {
lightTenTop(col);
lightMinutes(col);
} else if (minute >= 55) {
lightFiveTop(col);
lightMinutes(col);
}
// Handle the "hour" part of the time
if (minute < 35) {
if (minute >= 5) {
lightPast(col);
}
switch (hour) {
case 1:
lightOne(col);
break;
case 2:
lightTwo(col);
break;
case 3:
lightThree(col);
break;
case 4:
lightFour(col);
break;
case 5:
lightFiveBottom(col);
break;
case 6:
lightSix(col);
break;
case 7:
lightSeven(col);
break;
case 8:
lightEight(col);
break;
case 9:
lightNine(col);
break;
case 10:
lightTenBottom(col);
break;
case 11:
lightEleven(col);
break;
case 12:
case 0:
lightTwelve(col);
break;
}
} else {
lightTo(col);
switch ((hour + 1) % 12) {
case 1:
lightOne(col);
break;
case 2:
lightTwo(col);
break;
case 3:
lightThree(col);
break;
case 4:
lightFour(col);
break;
case 5:
lightFiveBottom(col);
break;
case 6:
lightSix(col);
break;
case 7:
lightSeven(col);
break;
case 8:
lightEight(col);
break;
case 9:
lightNine(col);
break;
case 10:
lightTenBottom(col);
break;
case 11:
lightEleven(col);
break;
case 12:
case 0:
lightTwelve(col);
break;
}
}
}
/* Light the various words on the clock
The grid used:
IT_IS_TENHALF
QUARTERTWENTY
FIVE_MINUTES_
PASTTO_ONETWO
THREEFOURFIVE
SIXSEVENEIGHT
NINETENELEVEN
TWELVE_OCLOCK
_ = doesn't matter what the letter is, we're never going to light it
*/
// In one clock, I hid a message in the dead letters, so I wanted a way to light them
void lightDeadLetters(uint32_t color) {
strip_1.setPixelColor(2, color);
strip_1.setPixelColor(5, color);
strip_3.setPixelColor(4, color);
strip_3.setPixelColor(12, color);
strip_4.setPixelColor(6, color);
strip_8.setPixelColor(6, color);
}
void lightItIs(uint32_t color) {
strip_1.setPixelColor(0, color);
strip_1.setPixelColor(1, color);
strip_1.setPixelColor(3, color);
strip_1.setPixelColor(4, color);
}
void lightTenTop(uint32_t color) {
strip_1.setPixelColor(6, color);
strip_1.setPixelColor(7, color);
strip_1.setPixelColor(8, color);
}
void lightHalf(uint32_t color) {
strip_1.setPixelColor(9, color);
strip_1.setPixelColor(10, color);
strip_1.setPixelColor(11, color);
strip_1.setPixelColor(12, color);
}
void lightQuarter(uint32_t color) {
strip_2.setPixelColor(0, color);
strip_2.setPixelColor(1, color);
strip_2.setPixelColor(2, color);
strip_2.setPixelColor(3, color);
strip_2.setPixelColor(4, color);
strip_2.setPixelColor(5, color);
strip_2.setPixelColor(6, color);
}
void lightTwenty(uint32_t color) {
strip_2.setPixelColor(7, color);
strip_2.setPixelColor(8, color);
strip_2.setPixelColor(9, color);
strip_2.setPixelColor(10, color);
strip_2.setPixelColor(11, color);
strip_2.setPixelColor(12, color);
}
void lightFiveTop(uint32_t color) {
strip_3.setPixelColor(0, color);
strip_3.setPixelColor(1, color);
strip_3.setPixelColor(2, color);
strip_3.setPixelColor(3, color);
}
void lightMinutes(uint32_t color) {
strip_3.setPixelColor(5, color);
strip_3.setPixelColor(6, color);
strip_3.setPixelColor(7, color);
strip_3.setPixelColor(8, color);
strip_3.setPixelColor(9, color);
strip_3.setPixelColor(10, color);
strip_3.setPixelColor(11, color);
}
void lightPast(uint32_t color) {
strip_4.setPixelColor(0, color);
strip_4.setPixelColor(1, color);
strip_4.setPixelColor(2, color);
strip_4.setPixelColor(3, color);
}
void lightTo(uint32_t color) {
strip_4.setPixelColor(4, color);
strip_4.setPixelColor(5, color);
}
void lightOne(uint32_t color) {
strip_4.setPixelColor(7, color);
strip_4.setPixelColor(8, color);
strip_4.setPixelColor(9, color);
}
void lightTwo(uint32_t color) {
strip_4.setPixelColor(10, color);
strip_4.setPixelColor(11, color);
strip_4.setPixelColor(12, color);
}
void lightThree(uint32_t color) {
strip_5.setPixelColor(0, color);
strip_5.setPixelColor(1, color);
strip_5.setPixelColor(2, color);
strip_5.setPixelColor(3, color);
strip_5.setPixelColor(4, color);
}
void lightFour(uint32_t color) {
strip_5.setPixelColor(5, color);
strip_5.setPixelColor(6, color);
strip_5.setPixelColor(7, color);
strip_5.setPixelColor(8, color);
}
void lightFiveBottom(uint32_t color) {
strip_5.setPixelColor(9, color);
strip_5.setPixelColor(10, color);
strip_5.setPixelColor(11, color);
strip_5.setPixelColor(12, color);
}
void lightSix(uint32_t color) {
strip_6.setPixelColor(0, color);
strip_6.setPixelColor(1, color);
strip_6.setPixelColor(2, color);
}
void lightSeven(uint32_t color) {
strip_6.setPixelColor(3, color);
strip_6.setPixelColor(4, color);
strip_6.setPixelColor(5, color);
strip_6.setPixelColor(6, color);
strip_6.setPixelColor(7, color);
}
void lightEight(uint32_t color) {
strip_6.setPixelColor(8, color);
strip_6.setPixelColor(9, color);
strip_6.setPixelColor(10, color);
strip_6.setPixelColor(11, color);
strip_6.setPixelColor(12, color);
}
void lightNine(uint32_t color) {
strip_7.setPixelColor(0, color);
strip_7.setPixelColor(1, color);
strip_7.setPixelColor(2, color);
strip_7.setPixelColor(3, color);
}
void lightTenBottom(uint32_t color) {
strip_7.setPixelColor(4, color);
strip_7.setPixelColor(5, color);
strip_7.setPixelColor(6, color);
}
void lightEleven(uint32_t color) {
strip_7.setPixelColor(7, color);
strip_7.setPixelColor(8, color);
strip_7.setPixelColor(9, color);
strip_7.setPixelColor(10, color);
strip_7.setPixelColor(11, color);
strip_7.setPixelColor(12, color);
}
void lightTwelve(uint32_t color) {
strip_8.setPixelColor(0, color);
strip_8.setPixelColor(1, color);
strip_8.setPixelColor(2, color);
strip_8.setPixelColor(3, color);
strip_8.setPixelColor(4, color);
strip_8.setPixelColor(5, color);
}
void lightOClock(uint32_t color) {
strip_8.setPixelColor(7, color);
strip_8.setPixelColor(8, color);
strip_8.setPixelColor(9, color);
strip_8.setPixelColor(10, color);
strip_8.setPixelColor(11, color);
strip_8.setPixelColor(12, color);
}
// Tell all strips to display what we've told them to display
void showAll() {
strip_1.show();
strip_2.show();
strip_3.show();
strip_4.show();
strip_5.show();
strip_6.show();
strip_7.show();
strip_8.show();
}
// turn off all the LEDs
void clearAll() {
for (int i = 0; i < NUM_LED; i++){
strip_1.setPixelColor(i, off);
strip_2.setPixelColor(i, off);
strip_3.setPixelColor(i, off);
strip_4.setPixelColor(i, off);
strip_5.setPixelColor(i, off);
strip_6.setPixelColor(i, off);
strip_7.setPixelColor(i, off);
strip_8.setPixelColor(i, off);
}
}
void readTime() {
DateTime now = rtc.now();
currentHour = now.hour();
currentHour24 = currentHour % 24; // for the TIME_MODE
currentHour = currentHour % 12; // want 12 hour time
currentMinute = now.minute();
if (currentMinute >= 60) {
// need to advance the hour
currentHour++;
currentHour = currentHour % 12;
currentMinute = currentMinute % 60; // we've accounted for the hour now, so we need any remaining minutes
}
currentDayOfMonth = now.day(); // for turning on dead letters
}