@@ -70,23 +70,41 @@ void setup()
7070
7171void loop ()
7272{
73- tmElements_t tm;
74- int cmdChar, y, oldCal, newCal ;
73+ // check for input, to set rtc time or calibration
74+ if (Serial. available ()) setRTC () ;
7575
76- // check for input, first character is a command, "S" to set date/time, or "C" to set the calibration register
77- if (Serial.available ()) {
78- cmdChar = Serial.read ();
76+ // print time and date every second
77+ static time_t tLast;
78+ time_t t { now () };
79+ if (t != tLast) {
80+ tLast = t;
81+ printTime (t);
82+ }
83+ }
84+
85+ void setRTC ()
86+ {
87+ // first character is a command, "S" to set date/time, or "C" to set the calibration register
88+ int cmdChar = Serial.read ();
7989
80- switch (cmdChar) {
81- case ' S' :
82- case ' s' :
90+ switch (cmdChar) {
91+ case ' S' :
92+ case ' s' :
93+ delay (25 ); // wait for all the input to arrive
94+ // check for input to set the RTC, minimum length is 13, i.e. yy,m,d,h,m,s<nl>
95+ if (Serial.available () < 13 ) {
96+ while (Serial.available ()) Serial.read (); // dump extraneous input
97+ Serial << F (" Input error or timeout, try again.\n " );
98+ }
99+ else {
83100 // note that the tmElements_t Year member is an offset from 1970,
84101 // but the RTC wants the last two digits of the calendar year.
85102 // use the convenience macros from TimeLib.h to do the conversions.
86- y = Serial.parseInt ();
103+ int y = Serial.parseInt ();
87104 if (y >= 100 && y < 1000 )
88- Serial << F (" Error: Year must be two digits or four digits!" ) << endl ;
105+ Serial << F (" Error: Year must be two digits or four digits!\n " ) ;
89106 else {
107+ tmElements_t tm;
90108 if (y >= 1000 )
91109 tm.Year = CalendarYrToTm (y);
92110 else // (y < 100)
@@ -96,39 +114,43 @@ void loop()
96114 tm.Hour = Serial.parseInt ();
97115 tm.Minute = Serial.parseInt ();
98116 tm.Second = Serial.parseInt ();
99- time_t t = makeTime (tm);
100- myRTC.set (t); // use the time_t value to ensure correct weekday is set
101- setTime (t);
102- Serial << F (" RTC set to: " );
103- printTime (t);
104- Serial << endl;
117+ if (tm.Month == 0 || tm.Day == 0 ) {
118+ while (Serial.available ()) Serial.read (); // dump extraneous input
119+ Serial << F (" Input error or timeout, try again.\n " );
120+ }
121+ else {
122+ time_t t = makeTime (tm);
123+ myRTC.set (t); // use the time_t value to ensure correct weekday is set
124+ setTime (t);
125+ Serial << F (" RTC set to: " );
126+ printTime (t);
127+ }
105128 }
106- break ;
129+ }
130+ break ;
107131
108- case ' C' :
109- case ' c' :
110- newCal = Serial.parseInt ();
111- oldCal = myRTC.calibRead ();
132+ case ' C' :
133+ case ' c' :
134+ delay (25 ); // wait for all the input to arrive
135+ if (Serial.available () < 2 ) { // minimum valid input at this point is 2 chars
136+ while (Serial.available ()) Serial.read (); // dump extraneous input
137+ Serial << F (" Input error or timeout, try again.\n " );
138+ }
139+ else {
140+ int newCal = Serial.parseInt ();
141+ int oldCal = myRTC.calibRead ();
112142 myRTC.calibWrite (newCal);
113143 Serial << F (" Calibration changed from " ) << oldCal << F (" to " ) << myRTC.calibRead () << endl;
114- break ;
115-
116- default :
117- Serial << endl << F (" Unrecognized command: " ) << (char )cmdChar << endl;
118- break ;
119- }
144+ }
145+ break ;
120146
121- // dump any extraneous input
122- while (Serial.available () > 0 ) Serial.read ();
147+ default :
148+ Serial << endl << F (" Unrecognized command: " ) << (char )cmdChar << endl;
149+ break ;
123150 }
124151
125- // print time and date every second
126- static time_t tLast;
127- time_t t { now () };
128- if (t != tLast) {
129- tLast = t;
130- printTime (t);
131- }
152+ // dump any extraneous input
153+ while (Serial.available ()) Serial.read ();
132154}
133155
134156// format and print a time_t value
0 commit comments