-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordClock.ino
611 lines (536 loc) · 17.3 KB
/
WordClock.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
/**
* Word Clock
*
* A clock that displays the time using words rather than numbers.
* Implemented as an LED array mounted inside a shadow box and controlled by
* an ATMega328P using a DS3231, some CD4094s and some ULN2003As.
*
* Software by Ryan Conway.
* Project inspired by the Instructables project by Scott Bezek.
* Depends on Adafruit's fork of RTClib: https://github.com/adafruit/RTClib
*/
#include <Wire.h>
#include "RTClib.h"
// Hardware constants
// CD4094 STROBE pin; shared by all of them
#define REGISTER_STROBE_PIN 8
// CD4094 DATA pin; fed only to the first one
#define REGISTER_DATA_PIN 7
// CD4094 CLOCK pin; shared by all of them
#define REGISTER_CLOCK_PIN 6
// CD4094 OUTPUT ENABLE pin; shared by all of them
#define REGISTER_OUTPUT_ENABLE_PIN 9
// Brightness adjustment input; expected to be the output of a potentiometer between 5V and GND
#define BRIGHTNESS_ADJUST_PIN A2
// Minute advance button input; expected to be a normally open signal, pulled up internally and
// connected to GND when depressed
#define MINUTE_ADVANCE_BUTTON_PIN A1
// Hour advance button input; expected to be a normally open signal, pulled up internally and
// connected to GND when depressed
#define HOUR_ADVANCE_BUTTON_PIN A0
//@debug Debug LED pin; used for debugging
#define DEBUG_LED_PIN 5
// Amount of time to hold the CD4094 STROBE signal high after a write
#define REGISTER_STROBE_DURATION_MS 2
// Tuneable software constants
// How long a button's state must be constant before we treat it as valid
#define BUTTON_DEBOUNCE_MS 50UL
// How long a button must be held before we assume the user wants another action
#define BUTTON_HOLD_ACTION_REPEAT_PERIOD 1000UL
// How long to wait after the last time adjustment before we assume the user is done setting the time
// This should be at least a few milliseconds longer than BUTTON_HOLD_ACTION_REPEAT_PERIOD
#define BUTTON_INACTION_RTC_UPDATE_DELAY 5000UL
// If our ADC read returns a value lower than this, we treat it as minimum
#define POTENTIOMETER_ERROR_MARGIN_LOW 50
// If our ADC read returns a value higher than this, we treat it as maximum
#define POTENTIOMETER_ERROR_MARGIN_HIGH 973
// How often we query the RTC for the current time during normal operation
#define RTC_QUERY_PERIOD_MILLIS 5000UL
// What time we reset the RTC to in the event that it's lost its power
#define RTC_RESET_YEAR 2017
#define RTC_RESET_MONTH 1
#define RTC_RESET_DAY 1
#define RTC_RESET_HOUR 0
#define RTC_RESET_MINUTE 0
#define RTC_RESET_SECOND 0
// Non-tuneable software constants
#define BITS_PER_BYTE 8
#define PWM_DUTY_MAX 255
#define MS_PER_SECOND 1000UL
// Word bit positions
#define WORD_BIT_MAX 23
typedef enum {
IT_IS = 9,
TEN_M = 13,
HALF_M = 4,
QUARTER_M = 11,
TWENTY_M = 2,
FIVE_M = 22,
MINUTES = 14,
PAST = 20,
TO = 21,
ONE_H = 0,
TWO_H = 5,
THREE_H = 19,
FOUR_H = 12,
FIVE_H = 6,
SIX_H = 18,
SEVEN_H = 10,
EIGHT_H = 3,
NINE_H = 17,
TEN_H = 8,
ELEVEN_H = 1,
TWELVE_H = 16,
OCLOCK = 7
} word_bit_map;
// Global state
// The current time, as understood locally
static uint8_t currentHour = 0;
static uint8_t currentMinute = 0;
static uint8_t currentSecond = 0;
// The last time we queried the RTC
static unsigned long lastRTCQueryTime = 0;
// Whether or not the time has been adjusted locally through the use of the time adjustment buttons
static bool timeLocallyUpdated = false;
// The current time, as understood locally, encoded such that each bit indicates the presence of
// a given word in word_bit_map
static uint32_t localWordRegister = 0;
// Shared reference to the RTC chip
static RTC_DS3231 rtc;
/**
* Get a specific byte of a number, where byte 0 is the least significant, byte 1 is one higher, etc.
*/
uint8_t getByte(uint32_t number, uint8_t byteIndex) {
uint32_t shifted = number >> (byteIndex * BITS_PER_BYTE);
uint8_t targetByte = shifted & 0xFF;
return targetByte;
}
/**
* Locally "enable" a given word by raising the corresponding bit to the local word register
* Note that this function will not update the display - to do so call flushWordRegister()
*/
void enableWord(word_bit_map theWord) {
uint32_t mask = ((uint32_t) 0x01) << theWord;
localWordRegister |= mask;
}
/**
* Locally "disable" all words by lowering all bits of the local word register
* Note that this function will not update the display - to do so call flushWordRegister()
*/
void disableAllWords() {
localWordRegister = 0;
}
/**
* Flush the local word register out to the external word register (the cascaded 8-bit latches).
*
* This is done in such an order that the "furthest" 8-bit latch will have the least significant byte
* and the "closest" one will have the most significant byte
*/
void flushWordRegister() {
shiftOut(REGISTER_DATA_PIN, REGISTER_CLOCK_PIN, MSBFIRST, getByte(localWordRegister, 0));
shiftOut(REGISTER_DATA_PIN, REGISTER_CLOCK_PIN, MSBFIRST, getByte(localWordRegister, 1));
shiftOut(REGISTER_DATA_PIN, REGISTER_CLOCK_PIN, MSBFIRST, getByte(localWordRegister, 2));
digitalWrite(REGISTER_STROBE_PIN, HIGH);
delay(REGISTER_STROBE_DURATION_MS);
digitalWrite(REGISTER_STROBE_PIN, LOW);
}
/**
* Update the local word register given the current hour and minute
* (typically as obtained from an RTC or other clock)
*/
void updateLocalWordRegister(uint8_t hour, uint8_t minute) {
// Start from a clean slate
disableAllWords();
// Convert 24-hour time to 12-hour time
if (hour > 12) { hour = hour - 12; }
if (hour == 0) { hour = 12; }
enableWord(IT_IS);
// Handle the "minute" part of the time
if (minute < 5) {
enableWord(OCLOCK);
} else if (minute >= 5 && minute < 10) {
enableWord(FIVE_M);
} else if (minute >= 10 && minute < 15) {
enableWord(TEN_M);
} else if (minute >= 15 && minute < 20) {
enableWord(QUARTER_M);
} else if (minute >= 20 && minute < 25) {
enableWord(TWENTY_M);
enableWord(MINUTES);
} else if (minute >= 25 && minute < 30) {
enableWord(TWENTY_M);
enableWord(FIVE_M);
enableWord(MINUTES);
} else if (minute >= 30 && minute < 35) {
enableWord(HALF_M);
} else if (minute >= 35 && minute < 40) {
enableWord(TWENTY_M);
enableWord(FIVE_M);
enableWord(MINUTES);
} else if (minute >= 40 && minute < 45) {
enableWord(TWENTY_M);
enableWord(MINUTES);
} else if (minute >= 45 && minute < 50) {
enableWord(QUARTER_M);
} else if (minute >= 50 && minute < 55) {
enableWord(TEN_M);
} else if (minute >= 55) {
enableWord(FIVE_M);
}
// Handle the "hour" part of the time
if (minute < 35) {
if (minute >= 5) {
enableWord(PAST);
}
switch (hour) {
case 1:
enableWord(ONE_H);
break;
case 2:
enableWord(TWO_H);
break;
case 3:
enableWord(THREE_H);
break;
case 4:
enableWord(FOUR_H);
break;
case 5:
enableWord(FIVE_H);
break;
case 6:
enableWord(SIX_H);
break;
case 7:
enableWord(SEVEN_H);
break;
case 8:
enableWord(EIGHT_H);
break;
case 9:
enableWord(NINE_H);
break;
case 10:
enableWord(TEN_H);
break;
case 11:
enableWord(ELEVEN_H);
break;
case 12:
enableWord(TWELVE_H);
break;
}
} else {
enableWord(TO);
switch (hour) {
case 1:
enableWord(TWO_H);
break;
case 2:
enableWord(THREE_H);
break;
case 3:
enableWord(FOUR_H);
break;
case 4:
enableWord(FIVE_H);
break;
case 5:
enableWord(SIX_H);
break;
case 6:
enableWord(SEVEN_H);
break;
case 7:
enableWord(EIGHT_H);
break;
case 8:
enableWord(NINE_H);
break;
case 9:
enableWord(TEN_H);
break;
case 10:
enableWord(ELEVEN_H);
break;
case 11:
enableWord(TWELVE_H);
break;
case 12:
enableWord(ONE_H);
break;
}
}
}
/**
* Update the clock display given the current hour and minute
* (typically as obtained from an RTC or other clock)
*/
void updateDisplay(uint8_t hour, uint8_t minute) {
updateLocalWordRegister(hour, minute);
flushWordRegister();
}
/**
* Update the clock display, as well as our local time, using the RTC's reported time
*/
void updateDisplayFromRTC() {
DateTime timeRTC = rtc.now();
currentHour = timeRTC.hour();
currentMinute = timeRTC.minute();
currentSecond = timeRTC.second();
updateDisplay(currentHour, currentMinute);
}
/**
* Set the brightness of the display
* Input range is [0, PWM_DUTY_MAX]
*/
void setBrightness(uint8_t brightness) {
analogWrite(REGISTER_OUTPUT_ENABLE_PIN, brightness);
}
/**
* Test the display by pulsing each word, one at a time
*/
void testDisplay1() {
int order[] = { IT_IS, TEN_M, HALF_M, QUARTER_M, TWENTY_M, FIVE_M, MINUTES, PAST, TO, ONE_H, TWO_H,
THREE_H, FOUR_H, FIVE_H, SIX_H, SEVEN_H, EIGHT_H, NINE_H, TEN_H, ELEVEN_H, TWELVE_H, OCLOCK };
int numWords = sizeof(order)/sizeof(order[0]);
for (uint8_t i = 0; i < numWords; i++) {
disableAllWords();
enableWord(order[i]);
flushWordRegister();
uint8_t a = 0;
for (a = 0; a < PWM_DUTY_MAX; a++) {
setBrightness(a);
delay(3);
}
for (a = PWM_DUTY_MAX; a > 0; a--) {
setBrightness(a);
delay(3);
}
}
setBrightness(0);
}
/**
* Test the display by lighting each word quickly, one at a time
*/
void testDisplay2() {
int order[] = { IT_IS, TEN_M, HALF_M, QUARTER_M, TWENTY_M, FIVE_M, MINUTES, PAST, TO, ONE_H, TWO_H,
THREE_H, FOUR_H, FIVE_H, SIX_H, SEVEN_H, EIGHT_H, NINE_H, TEN_H, ELEVEN_H, TWELVE_H, OCLOCK };
int numWords = sizeof(order)/sizeof(order[0]);
setBrightness(100);
for (uint8_t i = 0; i < numWords; i++) {
disableAllWords();
enableWord(order[i]);
flushWordRegister();
delay(100);
}
setBrightness(0);
}
/**
* Test the display by iterating over all minutes of the day, starting at 00:00 and ending at 23:59
* Do this on a simulation time scale where 1 second in real life = 5 simulated minutes
* i.e., this test takes (24*60/5) = 288 seconds to complete
*/
void testDisplay3() {
uint8_t hour = 0;
uint8_t minute = 0;
setBrightness(100);
while (hour < 24) {
updateDisplay(hour, minute);
delay(1000);
minute += 5;
if (minute >= 60) {
hour += 1;
minute = 0;
}
}
setBrightness(0);
}
/**
* Setup function
* This gets called automatically on boot
*/
void setup() {
// Configure all of our pins
pinMode(REGISTER_STROBE_PIN, OUTPUT);
pinMode(REGISTER_DATA_PIN, OUTPUT);
pinMode(REGISTER_CLOCK_PIN, OUTPUT);
pinMode(REGISTER_OUTPUT_ENABLE_PIN, OUTPUT);
pinMode(DEBUG_LED_PIN, OUTPUT); //@debug
pinMode(BRIGHTNESS_ADJUST_PIN, INPUT);
pinMode(MINUTE_ADVANCE_BUTTON_PIN, INPUT_PULLUP);
pinMode(HOUR_ADVANCE_BUTTON_PIN, INPUT_PULLUP);
// Turn off the display for now. The brightness control function will set this later
setBrightness(0);
//@todo consider doing something on RTC failure
rtc.begin();
// If the RTC's lost power, reset it
if (rtc.lostPower()) {
rtc.adjust(DateTime(RTC_RESET_YEAR, RTC_RESET_MONTH, RTC_RESET_DAY,
RTC_RESET_HOUR, RTC_RESET_MINUTE, RTC_RESET_SECOND));
}
int hourAdvance = digitalRead(HOUR_ADVANCE_BUTTON_PIN);
int minuteAdvance = digitalRead(MINUTE_ADVANCE_BUTTON_PIN);
if (hourAdvance == LOW || minuteAdvance == LOW) {
delay(1000);
if (hourAdvance == HIGH && minuteAdvance == LOW) {
testDisplay1();
} else if (hourAdvance == LOW && minuteAdvance == HIGH) {
testDisplay2();
} else if (hourAdvance == LOW && minuteAdvance == LOW) {
testDisplay3();
}
}
updateDisplayFromRTC();
lastRTCQueryTime = millis();
}
/**
* Handle the external brightness control
*/
void handleBrightnessControl() {
// Read the analog brightness pin
int analogBrightnessValue = analogRead(BRIGHTNESS_ADJUST_PIN);
// Map the read analog value to a PWM duty cycle value
// analogRead returns [0, 1023] for [0, VCC]V. The voltage range is actually less thanks to nonideal potentiometer
// properties and finite pin input impedance, so condense it
// The output PWM duty cycle range is [0, PWM_DUTY_MAX]
int pwmDuty = PWM_DUTY_MAX;
if (analogBrightnessValue < POTENTIOMETER_ERROR_MARGIN_LOW) {
pwmDuty = 0;
} else if (analogBrightnessValue > POTENTIOMETER_ERROR_MARGIN_HIGH) {
pwmDuty = PWM_DUTY_MAX;
} else {
// map [POTENTIOMETER_ERROR_MARGIN_LOW, POTENTIOMETER_ERROR_MARGIN_HIGH] to [0, PWM_DUTY_MAX]
double divideFactor = ((double)(POTENTIOMETER_ERROR_MARGIN_HIGH-POTENTIOMETER_ERROR_MARGIN_LOW)) / ((double)PWM_DUTY_MAX);
double pwmDutyDouble = ((double)(analogBrightnessValue - POTENTIOMETER_ERROR_MARGIN_LOW)) / divideFactor;
pwmDuty = pwmDutyDouble;
}
// For safety, in case of rounding or human errors - limit the PWM duty cycle
if (pwmDuty > PWM_DUTY_MAX) {
pwmDuty = PWM_DUTY_MAX;
}
setBrightness(pwmDuty);
}
/**
* Advance the current hour
*/
static void advanceHour(uint8_t* hour) {
if (*hour < 23) {
*hour = *hour + 1;
} else {
*hour = 0;
}
}
/**
* Advance the current minute, and adjust the current hour if appropriate
*/
static void advanceMinute(uint8_t* hour, uint8_t* minute) {
if (*minute < 59) {
// Typical case
*minute = *minute + 1;
} else {
// Minute 59 -> 0 transition; increment the hour, too
*minute = 0;
*hour = *hour + 1;
}
}
/**
* Handle the external time adjust buttons
* Return whether or not the user is currently in the process of changing the time
* @todo duplicated code
*/
void handleTimeAdjustButtons() {
// Debouncing variables
static unsigned long lastHourButtonChange = 0;
static unsigned long lastMinuteButtonChange = 0;
static int lastHourButtonValue = HIGH;
static int lastMinuteButtonValue = HIGH;
// Variables to enable holding a time adjust button to gradually change the time
static bool hourButtonBeingHeld = false;
static bool minuteButtonBeingHeld = false;
static unsigned long nextHourAdvanceTime = 0;
static unsigned long nextMinuteAdvanceTime = 0;
static unsigned long updateRTCTime = 0;
int hourButtonValue = digitalRead(HOUR_ADVANCE_BUTTON_PIN);
int minuteButtonValue = digitalRead(MINUTE_ADVANCE_BUTTON_PIN);
// Debouncing logic
unsigned long timeNow = millis();
if (hourButtonValue != lastHourButtonValue) {
lastHourButtonChange = timeNow;
}
if (minuteButtonValue != lastMinuteButtonValue) {
lastMinuteButtonChange = timeNow;
}
lastHourButtonValue = hourButtonValue;
lastMinuteButtonValue = minuteButtonValue;
if ((timeNow - lastHourButtonChange) > BUTTON_DEBOUNCE_MS) {
// the button state is settled
// if it's being depressed (connected to ground), then increment the hour once immediately
// and then once every so often until released
if (hourButtonValue == LOW) {
if (hourButtonBeingHeld == false) {
advanceHour(¤tHour);
currentSecond = 0;
timeLocallyUpdated = true;
nextHourAdvanceTime = timeNow + BUTTON_HOLD_ACTION_REPEAT_PERIOD;
updateRTCTime = timeNow + BUTTON_INACTION_RTC_UPDATE_DELAY;
updateDisplay(currentHour, currentMinute);
hourButtonBeingHeld = true;
} else if (timeNow >= nextHourAdvanceTime) {
advanceHour(¤tHour);
currentSecond = 0;
nextHourAdvanceTime = timeNow + BUTTON_HOLD_ACTION_REPEAT_PERIOD;
updateDisplay(currentHour, currentMinute);
updateRTCTime = timeNow + BUTTON_INACTION_RTC_UPDATE_DELAY;
}
} else {
hourButtonBeingHeld = false;
}
}
if ((timeNow - lastMinuteButtonChange) > BUTTON_DEBOUNCE_MS) {
// the button state is settled
// if it's being depressed (connected to ground), then increment the minute once immediately
// and then once every so often until released
if (minuteButtonValue == LOW) {
if (minuteButtonBeingHeld == false) {
advanceMinute(¤tHour, ¤tMinute);
currentSecond = 0;
timeLocallyUpdated = true;
nextMinuteAdvanceTime = timeNow + BUTTON_HOLD_ACTION_REPEAT_PERIOD;
updateRTCTime = timeNow + BUTTON_INACTION_RTC_UPDATE_DELAY;
updateDisplay(currentHour, currentMinute);
minuteButtonBeingHeld = true;
} else if (timeNow >= nextMinuteAdvanceTime) {
advanceMinute(¤tHour, ¤tMinute);
currentSecond = 0;
nextMinuteAdvanceTime = timeNow + BUTTON_HOLD_ACTION_REPEAT_PERIOD;
updateRTCTime = timeNow + BUTTON_INACTION_RTC_UPDATE_DELAY;
updateDisplay(currentHour, currentMinute);
}
} else {
minuteButtonBeingHeld = false;
}
}
if (timeLocallyUpdated == true && timeNow > updateRTCTime && hourButtonBeingHeld == false && minuteButtonBeingHeld == false) {
DateTime timeRTC = rtc.now();
rtc.adjust(DateTime(timeRTC.year(), timeRTC.month(), timeRTC.day(), currentHour, currentMinute, (BUTTON_INACTION_RTC_UPDATE_DELAY / MS_PER_SECOND)));
timeLocallyUpdated = false;
updateRTCTime = 0;
}
}
/**
* Loop function
* This gets called repeatedly and indefinitely after setup() is called
*/
void loop() {
handleBrightnessControl();
handleTimeAdjustButtons();
unsigned long timeNow = millis();
if ((timeNow - lastRTCQueryTime) > RTC_QUERY_PERIOD_MILLIS) {
if (timeLocallyUpdated == false) {
updateDisplayFromRTC();
lastRTCQueryTime = timeNow;
}
}
delay(1);
}