-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorldClock.ino
826 lines (725 loc) · 17.8 KB
/
WorldClock.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
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
#include <stdio.h>
#include <string.h>
#include <Wire.h>
#include <RTClib.h>
// DAY Brightness setting 0 = off 20 = full
#define DAYBRIGHTNESS 20
// NIGHT Brightness setting 0 = off 20 = full
#define NIGHTBRIGHTNESS 20
// Display output pin assignments
#define MINUTES Display1=Display1 | (1<<0)
#define MTEN Display1=Display1 | (1<<1)
#define HALF Display1=Display1 | (1<<2)
#define PAST Display1=Display1 | (1<<3)
#define THREE Display1=Display1 | (1<<4)
#define ITIS Display1=Display1 | (1<<5)
#define TWENTY Display1=Display1 | (1<<6)
#define TO Display1=Display1 | (1<<7)
#define TWO Display2=Display2 | (1<<0)
#define SIX Display2=Display2 | (1<<1)
#define TWELVE Display2=Display2 | (1<<2)
#define HFIVE Display2=Display2 | (1<<3)
#define SEVEN Display2=Display2 | (1<<4)
#define OCLOCK Display2=Display2 | (1<<5)
#define ONE Display2=Display2 | (1<<6)
#define QUARTER Display2=Display2 | (1<<7)
#define EIGHT Display3=Display3 | (1<<0)
#define MFIVE Display3=Display3 | (1<<1)
#define ARDUINO Display3=Display3 | (1<<2)
#define ELEVEN Display3=Display3 | (1<<3)
#define HTEN Display3=Display3 | (1<<4)
#define NINE Display3=Display3 | (1<<5)
#define FOUR Display3=Display3 | (1<<6)
#define DOUG Display3=Display3 | (1<<7)
#define LED1 Led1=1
#define LED2 Led2=1
#define LED3 Led3=1
#define LED4 Led4=1
#define INIT_TIMER_COUNT 6
#define RESET_TIMER2 TCNT2 = INIT_TIMER_COUNT
int hour = 11, minute = 29, second = 00;
static unsigned long msTick = 0; // the number of Millisecond Ticks since we last incremented the second counter
int count;
int selftestmode; // 1 = in self test - flash display
int DS1307Present = 0; // flag to indicate that the 1307 is there.. 1 = present
char Display1 = 0, Display2 = 0, Display3 = 0, Led1 = 0, Led2 = 0, Led3 = 0, Led4 = 0;
int OldHardware = 0; // 1 = we are running on old hardwrae
int BTNActive = 1; // the sense of the button inputs (Changes based on hardware type)
int timercount = 10; // used for interrupt counting to determine the brightnes of the display
// hardware constants
int LEDClockPin = 5; // Arduino Pin#11 - 4094 Pin 3 clock ERA 11
int LEDDataPin = 3; // Arduino Pin#5 - 4094 pin 2 Data ERA 5
int LEDStrobePin = 4; // Arduino Pin#6 - 4094 pin 1 Strobe ERA 6
// buttons
int FWDButtonPin = 6;
int REVButtonPin = 7;
// Minute LED Pins
int LED1PIN = 14; // Arduino analog 5
int LED2PIN = 15; // Arduino analog 4
int LED3PIN = 16; // Arduino analog 3
int LED4PIN = 17; // Arduino analog 2
int current_brightnes = 0;
/* Create buffers */
char buf[50]; // time output string for debugging
// create an object that talks to the RTC
RTC_DS1307 RTC;
void print_DS1307time()
{
/* Get the current time and date from the chip */
//Time t = rtc.time();
DateTime t = RTC.now();
/* Format the time and date and insert into the temporary buffer */
snprintf(buf, sizeof(buf), "DS1307 time: %02d:%02d:%02d",
t.hour(), t.minute(), t.second());
/* Print the formatted string to serial so we can see the time */
Serial.println(buf);
}
void setup()
{
// initialise the hardware
// initialize the appropriate pins as outputs:
pinMode(LEDClockPin, OUTPUT);
pinMode(LEDDataPin, OUTPUT);
pinMode(LEDStrobePin, OUTPUT);
pinMode(FWDButtonPin, INPUT);
pinMode(REVButtonPin, INPUT);
pinMode(13, OUTPUT);
// Minute LEDS
pinMode(LED1PIN, OUTPUT);
pinMode(LED2PIN, OUTPUT);
pinMode(LED3PIN, OUTPUT);
pinMode(LED4PIN, OUTPUT);
current_brightnes = DAYBRIGHTNESS;
Serial.begin(9600); // setup the serial port to 9600 baud
SWversion(); // Display the version number of the software
Wire.begin();
RTC.begin();
// test whether the DS1307 is there
Serial.print("Verifying DS1307 ");
// start by verifying that the chip has a valid signature RTC.setSqwOutSignal(RTC_DS1307::Frequency_1Hz);
if (RTC.readByteInRam(0x20) == 0xa5) {
// Signature is there - set the present flag and mmove on
DS1307Present = 1;
Serial.println("present - Valid Signature");
}
else
{
// Signature isnt there - may be a new chip -
// do a write to see if it will hold the signature
RTC.writeByteInRam(0x20, 0xa5);
if (RTC.readByteInRam(0x20) == 0xa5) {
// We can store data - assume that it is a new chip that needs initialisation
// Start by turning off write protection and clearing the clock halt flag.
// Make a new time object to set the date and time
DateTime t(2015, 04, 17, hour, minute, second);
// Set the time and date on the chip
RTC.adjust(t);
// set the DS1307 present flag
DS1307Present = 1;
Serial.println("present - new chip initialised.");
}
else Serial.println("absent");
}
// determine whether we are running on old or new hardware
// old hardware tied the push buttons to ground using 4k7 resistors
// and relied on the buttons to pull them high
// new hardware uses internal pullups, and uses the buttons
// to pull the inputs down.
digitalWrite(FWDButtonPin, HIGH); // Turn on weak pullups
digitalWrite(REVButtonPin, HIGH); // Turn on weak pullups
OldHardware = 0;
if ( digitalRead(FWDButtonPin) == 0 && digitalRead(REVButtonPin) == 0)
{
Serial.println("Detected Old Hardware");
OldHardware = 1; // we have old hardware
BTNActive = 1; // True = active for old hardware
digitalWrite(FWDButtonPin, LOW); // Turn off weak pullups
digitalWrite(REVButtonPin, LOW); // Turn off weak pullups
}
else
{
Serial.println("Detected New Hardware");
OldHardware = 0; // we have old hardware
BTNActive = 0; // True = active for old hardware
}
// Initialise Timer2: Timer Prescaler /64,
TCCR2A = 0;
TCCR2B |= (1 << CS22);
TCCR2B &= ~((1 << CS21) | (1 << CS20));
// Use normal mode
TCCR2B &= ~((1 << WGM21) | (1 << WGM20));
// Use internal clock - external clock not used in Arduino
ASSR |= (0 << AS2);
//Timer2 Overflow Interrupt Enable
TIMSK2 |= 1 << TOIE2;
RESET_TIMER2;
sei();
Serial.println("Finished setting up Timer2 Interrupt");
msTick = millis(); // Initialise the msTick counter
selftest(); // validate the hardware for the user
selftestmode = 0;
if (DS1307Present == 1) {
// Get the current time and date from the chip
DateTime t = RTC.now();
second = t.second();
minute = t.minute();
hour = t.hour();
}
displaytime(); // display the current time
}
// Interrupt handler - Arduino runs at 16 Mhz, so we have 1000 Overflows per second...
// 1/ ((16000000 / 64) / 256) = 1 / 1000
ISR(TIMER2_OVF_vect) {
RESET_TIMER2;
if (timercount-- <= current_brightnes) {
// Now we write the actual values to the hardware
shiftOut(LEDDataPin, LEDClockPin, MSBFIRST, Display3);
shiftOut(LEDDataPin, LEDClockPin, MSBFIRST, Display2);
shiftOut(LEDDataPin, LEDClockPin, MSBFIRST, Display1);
digitalWrite(LEDStrobePin, HIGH);
//delay(2);
digitalWrite(LEDStrobePin, LOW);
digitalWrite(LED1PIN, Led1);
digitalWrite(LED2PIN, Led2);
digitalWrite(LED3PIN, Led3);
digitalWrite(LED4PIN, Led4);
}
else
{
shiftOut(LEDDataPin, LEDClockPin, MSBFIRST, 0);
shiftOut(LEDDataPin, LEDClockPin, MSBFIRST, 0);
shiftOut(LEDDataPin, LEDClockPin, MSBFIRST, 0);
digitalWrite(LEDStrobePin, HIGH);
//delay(2);
digitalWrite(LEDStrobePin, LOW);
digitalWrite(LED1PIN, 0);
digitalWrite(LED2PIN, 0);
digitalWrite(LED3PIN, 0);
digitalWrite(LED4PIN, 0);
}
if (timercount == 0) timercount = 20;
}
void ledsoff(void) {
Display1 = 0;
Display2 = 0;
Display3 = 0;
Led1 = 0;
Led2 = 0;
Led3 = 0;
Led4 = 0;
}
void selftest(void) {
// start by clearing the display to a known state
ledsoff();
ITIS;
delay(500);
ledsoff();
MTEN;
delay(500);
ledsoff();
HALF;
delay(500);
ledsoff();
TWENTY;
delay(500);
ledsoff();
QUARTER;
delay(500);
ledsoff();
MFIVE;
delay(500);
ledsoff();
MINUTES;
delay(500);
ledsoff();
PAST;
delay(500);
ledsoff();
TO;
delay(500);
ledsoff();
ONE;
delay(500);
ledsoff();
TWO;
delay(500);
ledsoff();
THREE;
delay(500);
ledsoff();
FOUR;
delay(500);
ledsoff();
HFIVE;
delay(500);
ledsoff();
SIX;
delay(500);
ledsoff();
SEVEN;
delay(500);
ledsoff();
EIGHT;
delay(500);
ledsoff();
NINE;
delay(500);
ledsoff();
HTEN;
delay(500);
ledsoff();
ELEVEN;
delay(500);
ledsoff();
TWELVE;
delay(500);
ledsoff();
OCLOCK;
delay(500);
ledsoff();
DOUG;
delay(500);
ledsoff();
ARDUINO;
delay(500);
ledsoff();
LED1;
delay(500);
ledsoff();
LED2;
delay(500);
ledsoff();
LED3;
delay(500);
ledsoff();
LED4;
delay(500);
for (int i = 0; i < 5; i++)
{
Display1 = 255;
Display2 = 255;
Display3 = 255;
delay(500);
ledsoff();
delay(500);
}
}
void displaytime(void) {
// start by clearing the display to a known state
ledsoff();
// Now, turn on the "It is" leds
ITIS;
Serial.print("It is ");
// now we display the appropriate minute counter
if ((minute > 4) && (minute < 10)) {
MFIVE;
MINUTES;
Serial.print("Five Minutes ");
}
if ((minute > 9) && (minute < 15)) {
MTEN;
MINUTES;
Serial.print("Ten Minutes ");
}
if ((minute > 14) && (minute < 20)) {
QUARTER;
Serial.print("Quarter ");
}
if ((minute > 19) && (minute < 25)) {
TWENTY;
MINUTES;
Serial.print("Twenty Minutes ");
}
if ((minute > 24) && (minute < 30)) {
TWENTY;
MFIVE;
MINUTES;
Serial.print("Twenty Five Minutes ");
}
if ((minute > 29) && (minute < 35)) {
HALF;
Serial.print("Half ");
}
if ((minute > 34) && (minute < 40)) {
TWENTY;
MFIVE;
MINUTES;
Serial.print("Twenty Five Minutes ");
}
if ((minute > 39) && (minute < 45)) {
TWENTY;
MINUTES;
Serial.print("Twenty Minutes ");
}
if ((minute > 44) && (minute < 50)) {
QUARTER;
Serial.print("Quarter ");
}
if ((minute > 49) && (minute < 55)) {
MTEN;
MINUTES;
Serial.print("Ten Minutes ");
}
if (minute > 54) {
MFIVE;
MINUTES;
Serial.print("Five Minutes ");
}
if ((minute < 5))
{
switch (hour) {
case 1:
case 13:
ONE;
Serial.print("One ");
break;
case 2:
case 14:
TWO;
Serial.print("Two ");
break;
case 3:
case 15:
THREE;
Serial.print("Three ");
break;
case 4:
case 16:
FOUR;
Serial.print("Four ");
break;
case 5:
case 17:
HFIVE;
Serial.print("Five ");
break;
case 6:
case 18:
SIX;
Serial.print("Six ");
break;
case 7:
case 19:
SEVEN;
Serial.print("Seven ");
break;
case 8:
case 20:
EIGHT;
Serial.print("Eight ");
break;
case 9:
case 21:
NINE;
Serial.print("Nine ");
break;
case 10:
case 22:
HTEN;
Serial.print("Ten ");
break;
case 11:
case 23:
ELEVEN;
Serial.print("Eleven ");
break;
case 0:
case 12:
TWELVE;
Serial.print("Twelve ");
break;
}
OCLOCK;
Serial.println("O'Clock");
}
else if ((minute < 35) && (minute > 4))
{
PAST;
Serial.print("Past ");
switch (hour) {
case 1:
case 13:
ONE;
Serial.println("One ");
break;
case 2:
case 14:
TWO;
Serial.println("Two ");
break;
case 3:
case 15:
THREE;
Serial.println("Three ");
break;
case 4:
case 16:
FOUR;
Serial.println("Four ");
break;
case 5:
case 17:
HFIVE;
Serial.println("Five ");
break;
case 6:
case 18:
SIX;
Serial.println("Six ");
break;
case 7:
case 19:
SEVEN;
Serial.println("Seven ");
break;
case 8:
case 20:
EIGHT;
Serial.println("Eight ");
break;
case 9:
case 21:
NINE;
Serial.println("Nine ");
break;
case 10:
case 22:
HTEN;
Serial.println("Ten ");
break;
case 11:
case 23:
ELEVEN;
Serial.println("Eleven ");
break;
case 0:
case 12:
TWELVE;
Serial.println("Twelve ");
break;
}
}
else
{
// if we are greater than 34 minutes past the hour then display
// the next hour, as we will be displaying a 'to' sign
TO;
Serial.print("To ");
switch (hour) {
case 1:
case 13:
TWO;
Serial.println("Two ");
break;
case 14:
case 2:
THREE;
Serial.println("Three ");
break;
case 15:
case 3:
FOUR;
Serial.println("Four ");
break;
case 4:
case 16:
HFIVE;
Serial.println("Five ");
break;
case 5:
case 17:
SIX;
Serial.println("Six ");
break;
case 6:
case 18:
SEVEN;
Serial.println("Seven ");
break;
case 7:
case 19:
EIGHT;
Serial.println("Eight ");
break;
case 8:
case 20:
NINE;
Serial.println("Nine ");
break;
case 9:
case 21:
HTEN;
Serial.println("Ten ");
break;
case 10:
case 22:
ELEVEN;
Serial.println("Eleven ");
break;
case 11:
case 23:
TWELVE;
Serial.println("Twelve ");
break;
case 0:
case 12:
ONE;
Serial.println("One ");
break;
}
}
// now we can illuminate the extra minute LEDs
if ((minute - (minute / 5) * 5) == 1) {
LED1;
}
if ((minute - (minute / 5) * 5) == 2) {
LED1;
LED2;
}
if ((minute - (minute / 5) * 5) == 3) {
LED1;
LED2;
LED3;
}
if ((minute - (minute / 5) * 5) == 4) {
LED1;
LED2;
LED3;
LED4;
}
}
void incrementtime(void) {
// increment the time counters keeping care to rollover as required
second = 0;
if (++minute >= 60) {
minute = 0;
if (++hour == 25) {
hour = 1;
}
}
// debug outputs
Serial.println();
if (DS1307Present == 1) print_DS1307time();
else Serial.print("Arduino Time: " );
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(second);
}
void SWversion(void) {
delay(2000);
Serial.println();
Serial.println("Wordclock -Arduino v3.0a - reduced brightnes version");
Serial.println("(c)2009,2010 Doug Jackson");
}
void loop(void)
{
int previous_minute;
if (DS1307Present == 1)
previous_minute = minute; // keep track of the last minute read to spot faulty DS1307 chips
//Serial.println("Loop Started");
// heart of the timer - keep looking at the millisecond timer on the Arduino
// and increment the seconds counter every 1000 ms
if ( millis() - msTick > 999) {
msTick = millis();
second++;
// Flash the onboard Pin13 Led so we know something is hapening!
digitalWrite(13, HIGH);
delay(50);
digitalWrite(13, LOW);
delay(50);
digitalWrite(13, HIGH);
delay(50);
digitalWrite(13, LOW);
if (second % 5 == 0) {
Serial.print(second);
Serial.print("..");
}
}
//test to see if we need to increment the time counters
if (second == 60)
{
incrementtime();
displaytime();
}
if (DS1307Present == 1) {
// Get the current time and date from the chip
DateTime t = RTC.now();
second = t.second();
minute = t.minute();
hour = t.hour();
}
// set the brightnes level based on the current hour - night=7pm - 6.59am
//
if ((hour < 7) | (hour >= 19))
current_brightnes = NIGHTBRIGHTNESS;
else
current_brightnes = DAYBRIGHTNESS;
// test to see if both buttons are being held down
// if so - start a self test till both buttons are held
// down again.
if ( digitalRead(FWDButtonPin) == BTNActive && digitalRead(REVButtonPin) == BTNActive)
{
selftestmode = !selftestmode;
if (selftestmode) Serial.println("Selftest Mode TRUE");
else Serial.println("Selftest mode FALSE");
}
// if (selftestmode) {
// for(int i=0; i<100; i++)
// {
// Display1=255; Display2=255; Display3=255;
// WriteLEDs(); delay(101-i);
// ledsoff(); WriteLEDs();delay(101-i);
// if (digitalRead(FWDButtonPin)==1) selftestmode=!selftestmode;
//
// }
// displaytime();
//
// }
// test to see if a forward button is being held down
// for time setting
if (digitalRead(FWDButtonPin) == BTNActive )
// the forward button is down
// and it has been more than one second since we
// last looked
{
Serial.println("Forward Button Down");
//minute=(((minute/5)*5) +5);
incrementtime();
second++; // Increment the second counter to ensure that the name
// flash doesnt happen when setting time
if (DS1307Present == 1) {
// Make a new time object to set the date and time
DateTime t(2015, 04, 17, hour, minute, second);
// Set the time and date on the chip
RTC.adjust(t);
}
delay(100);
displaytime();
}
// test to see if the back button is being held down
// for time setting
if (digitalRead(REVButtonPin) == BTNActive )
{
Serial.println("Backwards Button Down");
minute--;
minute--;
second = 0; // decrement the minute counter
if (minute < 0) {
minute = 58;
if (--hour < 0) hour = 23;
}
incrementtime();
second++; // Increment the second counter to ensure that the name
// flash doesnt happen when setting time
if (DS1307Present == 1) {
// Make a new time object to set the date and time
DateTime t(2015, 04, 17, hour, minute, second);
// Set the time and date on the chip
RTC.adjust(t);
}
displaytime();
delay(100);
}
}