-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrixclockrotate.ino
3080 lines (2626 loc) · 85 KB
/
matrixclockrotate.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
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********************************************************************
Mini Clock v1.0, Jul 2014 by Nick Hall
Distributed under the terms of the GPL.
For help on how to build the clock see my blog:
http://123led.wordpress.com/
***********************************************************************
Modified to "Multi-Mode Digi Uhr" by joergeli
http://arduino.joergeli.de
Tested with Arduino-IDE v1.6.7
Modifications and differences to the original:
Using generic MAX72xx Matrixes (therefore chars are rotated 90 degrees!)
Using DS3231 RTC-Module (temperature-compensated = more accurate than DS1307)
Using Arduino-Nano V3.0
Translated daynames, monthnames, etc. to German
Added some "wipe"-effects
Added SmallSlide-Mode
Added Shift-Mode
Added automatic switching approx. every 2 minutes between Small-, Wordclock-, SmallSlide-, Slide- and Shift-mode. (Only when in circle-mode!)
(No automatic-switching in Basic-Mode!)
Added automatic displaying of dayname, date, month-name, year and week of year ( when second is 35 )
Modified buttonB as Toggle-Button, which toggles between "Display Date = On" and "Display Date = Off"
Added automatic Daylight Saving Time (+/- 1 hour)
Reprogrammed code of Wordclock-Mode to look like this: http://arduino.joergeli.de/wordclock/wordclock.php
(shows German-time in steps of 5 minutes, bottom-line shows +1, +2, +3, +4 minutes )
Added DS18B20 Temp-Sensor and displaying it's temperature while in circle-mode when automatic changing of clock-mode occurs (approx. every 2 minutes).
Added Photoresistor (LDR) for automatic changing brightness (therefore brightness-menu removed)
========
Modified in 2017 Radu
Tested on Arduino 1.8.5
Display_temp in C and F
Display_date
screen rotate >> ROTATEDISPLAY
move to ds3231 library, read time and temperature, replace DS18B20 >> DS3231
RTC libraary = RTCLib by NeiroN
light sensor routine
To DO
setup routine
alarm
***********************************************************************/
#define XDS3231 1 // Use DS3231 library for rtc and temp=1, use old RTC and DS sensor=0
#define ROTATEDISPLAY 1 // 0 or 1 - rotate display, olde style, vertical pins 0, new style with horizontal connections 1
const float TempCorrection = -1; // DS3231 temperature correction factor for Celsius reading
const int BrightMin = 100;
const int BrightMax = 1000;
//include libraries:
#include "fontDigiClock.h" // Font library
#include <LedControl.h> // For assigning LED's
#include <Wire.h> // DS1307 clock
#if XDS3231==0
#include <RTClib.h> // DS1307 clock, works also with DS3231 clock
#include <DallasTemperature.h> // For Dallas DS18B20 Temp-Sensor
#endif
#if XDS3231==1
#include <RTClib.h>
#endif
#include <Button.h> // Button library by Alexander Brevig
#include <OneWire.h> // This library allows you to communicate with I2C
//define constants
#define NUM_DISPLAY_MODES 5 // Number of clock-modes (counting zero as the first mode)
#define NUM_SETTINGS_MODES 3 // Number of settings modes = 3 (conting zero as the first mode)
#define SLIDE_DELAY 55 // The time in milliseconds for the slide effect per character in slide mode. Make this higher for a slower effect
#define cls clear_display // Clear display
#define LIGHT A0 // Photoresistor (LDR) for steering brightness LDR5506 to vcc, 10K to ground
#define ONE_WIRE_BUS 4 // Data wire is plugged into pin 4 on the Arduino
#if XDS3231==0
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
#endif
// Setup LED Matrix
// pin 12 is connected to the DataIn (DIN) on the display
// pin 11 is connected to the CLK on the display
// pin 10 is connected to LOAD (CS) on the display
//sets the 3 pins as 12, 11 & 10 and then sets 4 displays (max is 8 displays)
LedControl lc = LedControl(12, 11, 10, 4);
//global variables
bool debug = false; // For debugging only, starts serial output (true/false)
bool show_intro = true; // Show intro at startup ? (true/false)
byte intensity = 0; // Startup intensity/brightness (0-15)
bool ampm = false; // Define 12 or 24 hour time. false = 24 hour. true = 12 hour
bool show_date = true; // Show date? - Display date approx. every 2 minutes (default = true)
bool circle = true; // Define circle mode - changes the clock-mode approx. every 2 minutes. Default = true (on)
byte clock_mode = 1; // Default clock mode.
// clock_mode 0 = basic mode
// clock_mode 1 = small mode
// clock_mode 2 = slide mode
// clock_mode 3 = smallslide mode
// clock_mode 4 = word clock
// clock_mode 5 = shift mode
// clock_mode 6 = setup menu
////________________________________________________________________________________________
//Please don't change the following variables:
byte old_mode = clock_mode; // Stores the previous clock mode, so if we go to date or whatever, we know what mode to go back.
short DN; // Returns the number of day in the year
short WN; // Returns the number of the week in the year
bool date_state = true; // Holds state of displaying date
int devices, dev; // Number of LED Matrix-Displays (dev = devices-1)
int rtc1[7]; // Array that holds complete real time clock output
char tempi[6]; // Holds temperature-chars for displaying temp
char dig[7]; // Holds time-chars for shift-mode
char shiftChar[8]; // Holds chars to display in shift-mode
byte lastsec; // remeber last seconds value
byte lastbright = 0;
////
//big S, small S
byte digits_old[6]; //old values we store time in. Set to somthing that will never match the time initially so all digits get drawn wnen the mode starts
byte digits_new[6]; //new digits time will slide to reveal
///________________________________________________________________________________________
//day array (The DS1307/DS3231 outputs 1-7 values for day of week)
char days[7][4] = {
"Son", "Mon", "Die", "Mit", "Don", "Fre", "Sam"
};
char daysfull[7][9] = {
"Sunday", "Monday", "Tuesday", "Wednesda", "Thursday", "Friday", "Saturday"
//"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerst", "Freitag", "Samstag"
};
char suffix[1] = {'.'}; //date suffix "." , used in slide, basic and jumble modes - e.g. date = 25.
//suffix in German is always "."
#if XDS3231==0
//RTC_DS1307 ds1307; // Create RTC object - works also with DS3231
DS3231 rtc;
#endif
#if XDS3231==1
//DS3231 Clock;
DS3231 rtc;
#endif
Button buttonA = Button(2, BUTTON_PULLUP); // Setup button A (using button library)
Button buttonB = Button(3, BUTTON_PULLUP); // Setup button B (using button library)
////////////////////////////////////////////////////////////////////////////////////////
void setup() {
digitalWrite(2, HIGH); // turn on pullup resistor for button on pin 2
digitalWrite(3, HIGH); // turn on pullup resistor for button on pin 3
pinMode(LIGHT, INPUT); // LDR for brightness
//if (debug) {
Serial.begin(115200); //start serial
Serial.println (F("Debugging activated ... "));
//}
//initialize the 4 matrix panels
//we have already set the number of devices when we created the LedControl
devices = lc.getDeviceCount();
dev = devices - 1;
//we have to init all devices in a loop
for (int address = 0; address < devices; address++) {
/* and clear the display */
lc.clearDisplay(address);
/* Set the brightness to a medium values */
lc.setIntensity(address, intensity);
/*The MAX72XX is in power-saving mode on startup*/
lc.shutdown(address, false);
}
#if XDS3231==0
//Setup DS18B20 Temperature-Sensor
sensors.begin(); // start up Dallas Temperature library
sensors.getAddress(tempDeviceAddress, 0); // get the adress of the first DS18B20 Temp-Sensor
sensors.requestTemperaturesByAddress(tempDeviceAddress); // sends command for one device to perform a temperature by address
//Setup DS1307/DS3231 RTC
#ifdef AVR
Wire.begin(); // start I2C communication
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino
#endif
rtc.begin();
//ds1307.begin(); //start RTC Clock - works also with DS3231
/*
if (! ds1307.isrunning()) {
Serial.println(F("RTC is NOT running!"));
ds1307.adjust(DateTime(__DATE__, __TIME__)); // sets the RTC to the date & time this sketch was compiled
}
*/
#endif
#if XDS3231==1
// Start the I2C interface
Wire.begin();
rtc.begin();
#endif
//Show intro
if (show_intro) {
intro();
}
//Show temperature
display_temp();
//Serial.print(F("Temperature: "));
//Serial.print(rtc.getTemperature());
//Serial.println(F(" C"));
wipeBottom();
// Show state of displaying date. toggleDateState() must! run once at startup, otherwise it shows opposite information.
toggleDateState();
} // end of setup
////////////////////////////////////////////////////////////////////////////////////////
void loop() {
//run the clock with whatever mode is set by clock_mode - the default is set at top of code.
switch (clock_mode) {
case 0:
basic();
break;
case 1:
small();
break;
case 2:
slide();
break;
case 3:
smallslide();
break;
case 4:
word_clock();
break;
case 5:
shift();
break;
case 6:
setup_menu();
break;
}
} // end of loop
////////////////////////////////////////////////////////////////////////////////////////
// plot: plot a dot at positon xy with val 0/1
void plot (byte x, byte y, byte val) {
//select which matrix depending on the x coord
byte address;
y = 7 - y;
if (x >= 0 && x <= 7) {
address = 0;
}
if (x >= 8 && x <= 15) {
address = 1;
x = x - 8;
}
if (x >= 16 && x <= 23) {
address = 2;
x = x - 16;
}
if (x >= 24 && x <= 31) {
address = 3;
x = x - 24;
}
#if ROTATEDISPLAY==0
// vertical pins display
if (val == 1) {
lc.setLed(address, x, y, true);
} else {
lc.setLed(address, x, y, false);
}
#endif
#if ROTATEDISPLAY==1
// horizontal connections display
// invert x
x = 7 - x;
if (val == 1) {
lc.setLed(address, y, x, true);
} else {
lc.setLed(address, y, x, false);
}
#endif
}
////////////////////////////////////////////////////////////////////////////////////////
//clear screen
void clear_display() {
for (byte address = 0; address < 4; address++) {
lc.clearDisplay(address);
}
}
////////////////////////////////////////////////////////////////////////////////////////
// setBright: set the brightness to a value between 0 and 15 (= 16 steps, in dependence of LDR)
int setBright() {
// map LDR-values from 0 to 15 and set the brightness of devices
int rrr;
byte brightness;
if (lastsec != rtc1[0]) {
lastsec = rtc1[0];
rrr = analogRead(LIGHT);
if (rrr<BrightMin) { brightness = 0;}
else if (rrr>BrightMax) { brightness = 15;}
else {brightness = map(rrr, BrightMin, BrightMax, 0, 15);}
// slowly changing lastbright
if (brightness > lastbright) {
if (lastbright < 15) {
lastbright++;
}
}
if (brightness < lastbright) {
if (lastbright > 0) {
lastbright--;
}
}
//we have to init all devices in a loop
for (int address = 0; address < devices; address++) {
lc.setIntensity(address, lastbright);
}
Serial.print(F("Bright="));
Serial.print(brightness);
Serial.print(F(" LastB="));
Serial.print(lastbright);
Serial.print(F(" r="));
Serial.println(rrr);
}
return brightness;
}
////////////////////////////////////////////////////////////////////////////////////////
// fade_high: fade intensity from 0 to brightness (in dependence of LDR)
void fade_high() {
// map LDR-values from 0 to 15
int brightness = map(analogRead(LIGHT), 0, 1023, 0, 15);
//fade from intensity 0 to brightness and set the brightness of devices
for (byte f = 0; f <= brightness; f++) {
for (byte address = 0; address < 4; address++) {
lc.setIntensity(address, f);
}
delay(120); //change this to alter fade-up speed
}
return;
}
////////////////////////////////////////////////////////////////////////////////////////
// fade_low: fade intensity from brightness (in dependence of LDR) to 0
void fade_low() {
// map LDR-values from 0 to 15
int brightness = map(analogRead(LIGHT), 0, 1023, 0, 15);
//fade from brightness to 1 and set the brightness of devices
for (byte f = brightness; f > 0; f--) {
for (byte address = 0; address < 4; address++) {
lc.setIntensity(address, f);
}
delay(120); //change this to alter fade-low speed
}
for (byte address = 0; address < 4; address++) {
lc.setIntensity(address, 0); // set intensity to lowest level
}
return;
}
////////////////////////////////////////////////////////////////////////////////////////
//intro: show intro at startup
void intro() {
for (byte address = 0; address < 4; address++) {
lc.setIntensity(address, 3);
}
for (int i = 0; i < 2; i++) {
wipeBottom();
wipeTop();
}
wipeOutside();
char ver_a[9] = "Matrix";
char ver_b[9] = "Clock";
for (byte address = 0; address < 4; address++) {
lc.setIntensity(address, 0);
}
byte i = 0;
while (ver_a[i]) {
delay(80);
puttinychar((i * 4), 1, ver_a[i]);
i++;
}
fade_high();
delay(200);
fade_low();
delay(500);
wipeOutside();
i = 0;
while (ver_b[i]) {
delay(80);
puttinychar((i * 4), 1, ver_b[i]);
i++;
}
fade_high();
delay(200);
fade_low();
delay(500);
wipeMiddle();
} // end of intro
////////////////////////////////////////////////////////////////////////////////////////
// puttinychar:
// Copy a 3x5 character glyph from the myfont data structure to display memory, with its upper left at the given coordinate
// This is unoptimized and simply uses plot() to draw each dot.
void puttinychar(byte x, byte y, char c) {
byte dots;
if (c >= 'A' && c <= 'Z' || (c >= 'a' && c <= 'z') ) {
c &= 0x1F; // A-Z maps to 1-26
}
else if (c >= '0' && c <= '9') {
c = (c - '0') + 32;
}
else if (c == ' ') {
c = 0; // space
}
else if (c == '.') {
c = 27; // full stop
}
else if (c == ':') {
c = 28; // colon
}
else if (c == '\'') {
c = 29; // single quote mark
}
else if (c == '!') {
c = 30; // exclamation mark
}
else if (c == '?') {
c = 31; // question mark
}
else if (c == '-') {
c = 42; // hyphen
}
else if (c == '#') {
c = 43; // degree-symbol
}
else if (c == '>') {
c = 44; // selector-arrow
}
else if (c == '~') {
c = 45; // Ü
}
else if (c == '*') {
c = 46; // Ö
}
for (byte col = 0; col < 3; col++) {
dots = pgm_read_byte_near(&mytinyfont[c][col]);
for (char row = 0; row < 5; row++) {
if (dots & (16 >> row))
plot(x + col, y + row, 1);
else
plot(x + col, y + row, 0);
}
}
}
////////////////////////////////////////////////////////////////////////////////////////
//putnormalchar:
//Copy a 5x7 character glyph from the myfont data structure to display memory
void putnormalchar(byte x, byte y, char c) {
byte dots;
if (c >= 'A' && c <= 'Z' ) {
c &= 0x1F; // A-Z maps to 1-26
}
else if (c >= 'a' && c <= 'z') {
c = (c - 'a') + 41; // A-Z maps to 41-67
}
else if (c >= '0' && c <= '9') {
c = (c - '0') + 31;
}
else if (c == ' ') {
c = 0; // space
}
else if (c == '.') {
c = 27; // full stop
}
else if (c == '\'') {
c = 28; // single quote mark
}
else if (c == ':') {
c = 29; // colon
}
else if (c == '>') {
c = 30; // clock_mode selector arrow
}
else if (c == '=') {
c = 79; // equal sign
}
else if (c >= -80 && c <= -67) {
c *= -1;
}
for (char col = 0; col < 5; col++) {
dots = pgm_read_byte_near(&myfont[c][col]);
for (char row = 0; row < 7; row++) {
//check coords are on screen before trying to plot
//if ((x >= 0) && (x <= 31) && (y >= 0) && (y <= 7)){
if (dots & (64 >> row)) { // only 7 rows.
plot(x + col, y + row, 1);
} else {
plot(x + col, y + row, 0);
}
//}
}
}
}
////////////////////////////////////////////////////////////////////////////////////////
// small(=mode 1): show the time in small 3x5 characters with seconds-dots at bottom-line
void small() {
char textchar[8]; // the 16 characters on the display
byte mins = 100; //mins
byte secs = rtc1[0]; //seconds
byte old_secs = secs; //holds old seconds value - from last time seconds were updated o display - used to check if seconds have changed
cls();
//run clock main loop as long as run_mode returns true
while (run_mode()) {
get_time();
secs = rtc1[0];
//check for button presses
if (buttonA.uniquePress()) {
switch_mode();
return;
}
if (buttonB.uniquePress()) {
toggleDateState();
delay(1000);
return;
}
// when in circle mode and minute=even and second=14, switch to word_clock (mode 4)
if (circle) {
if (rtc1[1] % 2 == 0 && rtc1[0] == 14) {
wipeInside();
clock_mode = 4; // switch to wordclock mode
return;
}
}
//if secs changed then update them on the display
if (secs != old_secs) {
bottomleds(secs); // plot seconds-dots at bottomline
// display date, when second=40 and date_state = true
if (rtc1[0] == 40 && date_state) {
display_date();
return;
}
char buffer[3];
itoa(secs, buffer, 10);
//fix - as otherwise if num has leading zero, e.g. "03" secs, itoa coverts this to chars with space "3 ".
if (secs < 10) {
buffer[1] = buffer[0];
buffer[0] = '0';
}
puttinychar( 20, 1, ':'); //seconds colon
puttinychar( 24, 1, buffer[0]); //seconds
puttinychar( 28, 1, buffer[1]); //seconds
old_secs = secs;
}
//if minute changes change time
if (mins != rtc1[1]) {
//reset these for comparison next time
mins = rtc1[1];
byte hours = rtc1[2];
if (hours > 12) {
hours = hours - ampm * 12;
}
if (hours < 1) {
hours = hours + ampm * 12;
}
//byte dow = rtc1[3]; // the DS1307/DS3231 outputs 0 - 6 where 0 = Sunday0 - 6 where 0 = Sunday.
//byte date = rtc1[4];
//set characters
char buffer[3];
itoa(hours, buffer, 10);
//fix - as otherwise if num has leading zero, e.g. "03" hours, itoa coverts this to chars with space "3 ".
if (hours < 10) {
buffer[1] = buffer[0];
//if we are in 12 hour mode blank the leading zero.
if (ampm) {
buffer[0] = ' ';
}
else {
buffer[0] = '0';
}
}
//set hours chars
textchar[0] = buffer[0];
textchar[1] = buffer[1];
textchar[2] = ':';
itoa (mins, buffer, 10);
if (mins < 10) {
buffer[1] = buffer[0];
buffer[0] = '0';
}
//set mins characters
textchar[3] = buffer[0];
textchar[4] = buffer[1];
//do seconds
textchar[5] = ':';
buffer[3];
secs = rtc1[0];
itoa(secs, buffer, 10);
//fix - as otherwise if num has leading zero, e.g. "03" secs, itoa coverts this to chars with space "3 ".
if (secs < 10) {
buffer[1] = buffer[0];
buffer[0] = '0';
}
//set seconds
textchar[6] = buffer[0];
textchar[7] = buffer[1];
byte x = 0;
byte y = 0;
//print each char
for (byte x = 0; x < 6 ; x++) {
puttinychar( x * 4, 1, textchar[x]);
}
}
delay(50);
} // end of while run_mode
}
////////////////////////////////////////////////////////////////////////////////////////
// basic(= mode 0): simple mode shows the time in 5x7 characters
void basic() {
cls();
char buffer[3]; //for int to char conversion to turn rtc values into chars we can print on screen
byte offset = 0; //used to offset the x postition of the digits and centre the display when we are in 12 hour mode and the clock shows only 3 digits. e.g. 3:21
byte x, y; //used to draw a clear box over the left hand "1" of the display when we roll from 12:59 -> 1:00am in 12 hour mode.
//do 12/24 hour conversion if ampm set to 1
byte hours = rtc1[2];
if (hours > 12) {
hours = hours - ampm * 12;
}
if (hours < 1) {
hours = hours + ampm * 12;
}
//do offset conversion
if (ampm && hours < 10) {
offset = 2;
}
else {
offset = 0;
}
//set the next minute we show the date at
//set_next_date();
// initially set mins to value 100 - so it wll never equal rtc1[1] on the first loop of the clock, meaning we draw the clock display when we enter the function
byte secs = 100;
byte mins = 100;
int count = 0;
//run clock main loop as long as run_mode returns true
while (run_mode()) {
//get the time from the clock chip
get_time();
//check for button press
if (buttonA.uniquePress()) {
switch_mode();
return;
}
if (buttonB.uniquePress()) {
toggleDateState();
delay(1000);
return;
}
// display temp, when second=40 and minute=even and date_state=true
if (rtc1[0] == 40 && rtc1[1] % 2 == 0 && date_state) {
wipeBottom();
display_temp();
wipeTop();
return;
}
// display date, when second=40 and minute=odd and date_state = true
if (rtc1[0] == 40 && rtc1[1] % 2 == 1 && date_state) {
display_date();
return;
}
//draw the flashing colon on/off if the secs have changed.
if (secs != rtc1[0]) {
secs = rtc1[0]; //update secs with new value
//Blink "::"
if (secs % 2 == 0) {
plot(14 - offset, 4, 1);
plot(14 - offset, 2, 0);
plot(16 - offset, 4, 0);
plot(16 - offset, 2, 1);
}
else {
plot(14 - offset, 4, 0);
plot(14 - offset, 2, 1);
plot(16 - offset, 4, 1);
plot(16 - offset, 2, 0);
}
}
//redraw the display if button pressed or if mins != rtc1[1]
if (mins != rtc1[1]) {
//update mins and hours with the new values
mins = rtc1[1];
hours = rtc1[2];
//adjust hours of ampm set to 12 hour mode
if (hours > 12) {
hours = hours - ampm * 12;
}
if (hours < 1) {
hours = hours + ampm * 12;
}
itoa(hours, buffer, 10);
//if hours < 10 the num e.g. "3" hours, itoa coverts this to chars with space "3 " which we dont want
if (hours < 10) {
buffer[1] = buffer[0];
buffer[0] = '0';
}
//print hours
//if we in 12 hour mode and hours < 10, then don't print the leading zero, and set the offset so we centre the display with 3 digits.
if (ampm && hours < 10) {
offset = 2;
//if the time is 1:00am clear the entire display as the offset changes at this time and we need to blank out the old 12:59
if ((hours == 1 && mins == 0) ) {
cls();
}
}
else {
//else no offset and print hours tens digit
offset = 0;
//if the time is 10:00am clear the entire display as the offset changes at this time and we need to blank out the old 9:59
if (hours == 10 && mins == 0) {
cls();
}
putnormalchar(1, 0, buffer[0]);
}
//print hours ones digit
putnormalchar(7 - offset, 0, buffer[1]);
//print mins
//add leading zero if mins < 10
itoa (mins, buffer, 10);
if (mins < 10) {
buffer[1] = buffer[0];
buffer[0] = '0';
}
//print mins tens and mins ones digits
putnormalchar(19 - offset, 0, buffer[0]);
putnormalchar(25 - offset, 0, buffer[1]);
} // end of if (mins != rtc1[1]
} // end of while run_mode
}
////////////////////////////////////////////////////////////////////////////////////////
//Big-Slide mode (=mode 2): like basic-mode, but with sliding digits top-down
void slide() {
for (byte i = 0; i <= 3; i++) {
digits_old[i] = 99;
}
//byte digits_old[4] = {99, 99, 99, 99}; //old values we store time in. Set to somthing that will never match the time initially so all digits get drawn wnen the mode starts
//byte digits_new[4]; //new digits time will slide to reveal
byte digits_x_pos[4] = {25, 19, 7, 1}; //x pos for which to draw each digit at
char old_char[2]; //used when we use itoa to transpose the current digit (type byte) into a char to pass to the animation function
char new_char[2]; //used when we use itoa to transpose the new digit (type byte) into a char to pass to the animation function
//old_chars - stores the 5 day and date suffix chars on the display. e.g. "mon" and "st". We feed these into the slide animation as the current char when these chars are updated.
//We sent them as A initially, which are used when the clocl enters the mode and no last chars are stored.
//char old_chars[6] = "AAAAA";
cls();
// plot the clock colon on the display
// putnormalchar( 13, 0, ':');
byte old_secs = rtc1[0]; //store seconds in old_secs. We compare secs and old secs. WHen they are different we redraw the display
//run clock main loop as long as run_mode returns true
while (run_mode()) {
get_time();
byte secs = rtc1[0];
// display date, when second=40 and date_state = true
if (rtc1[0] == 40 && date_state) {
display_date();
return;
}
// when in circle mode and minute=even and second=15, switch to shift mode (mode 5)
if (circle) {
if (rtc1[1] % 2 == 0 && rtc1[0] == 15) {
wipeMiddle();
display_temp();
wipeTop();
clock_mode = 5; // switch to shift mode
return;
}
}
//check for button press
if (buttonA.uniquePress()) {
switch_mode();
return;
}
if (buttonB.uniquePress()) {
toggleDateState();
delay(1000);
return;
}
//if secs have changed then update the display
if (rtc1[0] != old_secs) {
//Blink "::"
if (old_secs % 2 == 0) {
plot(14, 4, 1);
plot(14, 2, 1);
plot(16, 4, 0);
plot(16, 2, 0);
}
else {
plot(16, 4, 1);
plot(16, 2, 1);
plot(14, 4, 0);
plot(14, 2, 0);
}
old_secs = rtc1[0];
//do 12/24 hour conversion if ampm set to 1
byte hours = rtc1[2];
if (hours > 12) {
hours = hours - ampm * 12;
}
if (hours < 1) {
hours = hours + ampm * 12;
}
//split all date and time into individual digits - stick in digits_new array
//rtc1[0] = secs //array pos and digit stored
//digits_new[0] = (rtc1[0]%10); //0 - secs ones
//digits_new[1] = ((rtc1[0]/10)%10); //1 - secs tens
//rtc1[1] = mins
digits_new[0] = (rtc1[1] % 10); //2 - mins ones
digits_new[1] = ((rtc1[1] / 10) % 10); //3 - mins tens
//rtc1[2] = hours
digits_new[2] = (hours % 10); //4 - hour ones
digits_new[3] = ((hours / 10) % 10); //5 - hour tens
//rtc1[4] = date
//digits_new[6] = (rtc1[4]%10); //6 - date ones
//digits_new[7] = ((rtc1[4]/10)%10); //7 - date tens
/*
//print the time to the serial port - for debuging
Serial.print(F("BIG S > hh:mm "));
Serial.print(digits_new[3]);
//Serial.print(F(" "));
Serial.print(digits_new[2]);
Serial.print(F(":"));
Serial.print(digits_new[1]);
//erial.print(F(" > "));
Serial.println(digits_new[0]);
//Serial.print(F(":"));
//Serial.println(set_min);
//Serial.print(".");
//Serial.print(rtc1[6]);
*/
//draw initial screen of all chars. After this we just draw the changes.
//compare digits 0 to 3 (mins and hours)
for (byte i = 0; i <= 3; i++) {
/*
//see if digit has changed...
Serial.print(F("BIG S > "));
Serial.print(i);
Serial.print(F(">"));
Serial.print(digits_old[i]);
Serial.print(F(":"));
Serial.println(digits_new[i]);
*/
if (digits_old[i] != digits_new[i]) {
//run 9 step animation sequence for each in turn
for (byte seq = 0; seq <= 8 ; seq++) {