Skip to content

Commit 4cd43f1

Browse files
committed
Example sketch bug fix.
1 parent edc6ce4 commit 4cd43f1

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

Diff for: examples/rtcSetSerial/rtcSetSerial.ino

+17-15
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <MCP79412RTC.h> // https://github.com/JChristensen/MCP79412RTC
1111
#include <TimeLib.h> // https://github.com/PaulStoffregen/Time
1212

13-
const uint32_t PRINT_INTERVAL(1000); // ms between printing the time
13+
const uint32_t PRINT_INTERVAL(10000); // ms between printing the time
1414
uint32_t ms, msLast;
1515

1616
void setup()
@@ -40,36 +40,38 @@ void loop()
4040
void readCommand()
4141
{
4242
char cmd[24] = "Set yyyy-mm-dd hh:mm:ss";
43-
static int i;
44-
tmElements_t tmSet;
45-
time_t tSet;
4643

47-
if (Serial.available() >= 23) { // enough characters for the whole command?
48-
i = 0; // yes, read the available characters
44+
// serial terminal will send 23 char command plus line terminator (0x0A)
45+
if (Serial.available() >= 24) { // enough characters for the whole command?
46+
unsigned int i = 0; // yes, read the available characters
4947
while (Serial.available() > 0) {
50-
if (i >= sizeof(cmd) - 1) { // more than we can enjoy
51-
flushInput(); // clear out the input buffer
48+
if (i >= sizeof(cmd)) { // more than we can enjoy
49+
flushInput(); // clear out the input buffer
50+
cmd[sizeof(cmd) - 1] = 0; // string terminator
5251
Serial.print("Too long: ");
5352
Serial.println(cmd);
5453
return;
5554
}
56-
delay(2); //let the next character trickle in
57-
cmd[i++] = char(Serial.read());
55+
delay(2); // let the next character trickle in
56+
char c = Serial.read();
57+
if (c >= ' ') cmd[i++] = c; // printable characters and spaces only
5858
}
59-
cmd[i] = 0; //put in string terminator
59+
cmd[i] = 0; // put in string terminator
6060

61+
tmElements_t tmSet;
6162
if (strncmp(cmd, "Set ", 4) == 0) {
6263
tmSet.Year = 1000 * (cmd[4] - '0') + 100 * (cmd[5] - '0') + 10 * (cmd[6] - '0') + cmd[7] - '0' - 1970;
6364
tmSet.Month = 10 * (cmd[9] - '0') + cmd[10] - '0';
6465
tmSet.Day = 10 * (cmd[12] - '0') + cmd[13] - '0';
6566
tmSet.Hour = 10 * (cmd[15] - '0') + cmd[16] - '0';
6667
tmSet.Minute = 10 * (cmd[18] - '0') + cmd[19] - '0';
6768
tmSet.Second = 10 * (cmd[21] - '0') + cmd[22] - '0';
68-
tSet = makeTime(tmSet); // convert to time_t
69-
setTime(tSet); // set the system time
70-
RTC.set(now()); // set the rtc
69+
time_t tSet = makeTime(tmSet); // convert to time_t
70+
setTime(tSet); // set the system time
71+
RTC.set(now()); // set the rtc
7172
Serial.println("RTC set!");
72-
flushInput(); // discard any extraneous trailing characters
73+
printTime(RTC.get());
74+
flushInput(); // discard any extraneous trailing characters
7375
}
7476
else {
7577
Serial.print("Unknown: ");

0 commit comments

Comments
 (0)