3434#include < Streaming.h> // http://arduiniana.org/libraries/streaming/
3535#include < TimeLib.h> // https://github.com/PaulStoffregen/Time
3636
37+ MCP79412RTC myRTC;
38+
3739void setup ()
3840{
39- byte rtcID[8 ];
40-
4141 Serial.begin (115200 );
42+ Serial << F ( " \n " __FILE__ " \n " __DATE__ " " __TIME__ " \n " );
43+ myRTC.begin ();
44+ myRTC.dumpRegs ();
4245
4346 // setSyncProvider() causes the Time library to synchronize with the
44- // external RTC by calling RTC .get() every five minutes by default.
45- setSyncProvider (RTC .get );
47+ // external RTC by calling myRTC .get() every five minutes by default.
48+ setSyncProvider (myRTC .get );
4649 Serial << endl << F (" RTC Sync" );
4750 if (timeStatus () != timeSet) Serial << F (" FAIL!" );
4851 Serial << endl;
4952
50- RTC.idRead (rtcID);
53+ uint8_t rtcID[8 ];
54+ myRTC.idRead (rtcID);
5155 Serial << F (" RTC ID = " );
5256 for (int i=0 ; i<8 ; ++i) {
5357 if (rtcID[i] < 16 ) Serial << ' 0' ;
5458 Serial << _HEX (rtcID[i]);
5559 }
5660 Serial << endl;
5761
58- RTC .getEUI64 (rtcID);
62+ myRTC .getEUI64 (rtcID);
5963 Serial << F (" EUI-64 = " );
6064 for (int i=0 ; i<8 ; ++i) {
6165 if (rtcID[i] < 16 ) Serial << ' 0' ;
6266 Serial << _HEX (rtcID[i]);
6367 }
6468 Serial << endl;
6569
66- Serial << F (" Calibration Register = " ) << RTC .calibRead () << endl;
70+ Serial << F (" Calibration Register = " ) << myRTC .calibRead () << endl;
6771}
6872
6973void loop ()
7074{
71- static time_t tLast;
72- time_t t;
7375 tmElements_t tm;
7476 int cmdChar, y, oldCal, newCal;
7577
@@ -96,21 +98,21 @@ void loop()
9698 tm.Hour = Serial.parseInt ();
9799 tm.Minute = Serial.parseInt ();
98100 tm.Second = Serial.parseInt ();
99- t = makeTime (tm);
100- RTC .set (t); // use the time_t value to ensure correct weekday is set
101+ time_t t = makeTime (tm);
102+ myRTC .set (t); // use the time_t value to ensure correct weekday is set
101103 setTime (t);
102104 Serial << F (" RTC set to: " );
103- printDateTime (t);
105+ printTime (t);
104106 Serial << endl;
105107 }
106108 break ;
107109
108110 case ' C' :
109111 case ' c' :
110112 newCal = Serial.parseInt ();
111- oldCal = RTC .calibRead ();
112- RTC .calibWrite (newCal);
113- Serial << F (" Calibration changed from " ) << oldCal << F (" to " ) << RTC .calibRead () << endl;
113+ oldCal = myRTC .calibRead ();
114+ myRTC .calibWrite (newCal);
115+ Serial << F (" Calibration changed from " ) << oldCal << F (" to " ) << myRTC .calibRead () << endl;
114116 break ;
115117
116118 default :
@@ -122,44 +124,22 @@ void loop()
122124 while (Serial.available () > 0 ) Serial.read ();
123125 }
124126
125- t = now ();
127+ // print time and date every second
128+ static time_t tLast;
129+ time_t t { now () };
126130 if (t != tLast) {
127131 tLast = t;
128- printDateTime (t);
129- Serial << endl;
132+ printTime (t);
130133 }
131134}
132135
133- // print date and time to Serial
134- void printDateTime (time_t t)
135- {
136- printDate (t);
137- Serial << ' ' ;
138- printTime (t);
139- }
140-
141- // print time to Serial
136+ // format and print a time_t value
142137void printTime (time_t t)
143138{
144- printI00 (hour (t), ' :' );
145- printI00 (minute (t), ' :' );
146- printI00 (second (t), ' ' );
147- }
148-
149- // print date to Serial
150- void printDate (time_t t)
151- {
152- printI00 (day (t), 0 );
153- Serial << monthShortStr (month (t)) << _DEC (year (t));
154- }
155-
156- // Print an integer in "00" format (with leading zero),
157- // followed by a delimiter character to Serial.
158- // Input value assumed to be between 0 and 99.
159- void printI00 (int val, char delim)
160- {
161- if (val < 10 ) Serial << ' 0' ;
162- Serial << _DEC (val);
163- if (delim > 0 ) Serial << delim;
164- return ;
139+ char buf[25 ];
140+ char m[4 ]; // temporary storage for month string (DateStrings.cpp uses shared buffer)
141+ strcpy (m, monthShortStr (month (t)));
142+ sprintf (buf, " %.2d:%.2d:%.2d %s %.2d %s %d" ,
143+ hour (t), minute (t), second (t), dayShortStr (weekday (t)), day (t), m, year (t));
144+ Serial.println (buf);
165145}
0 commit comments