forked from JChristensen/MCP79412RTC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtcSetSerial.pde
More file actions
120 lines (107 loc) · 4.23 KB
/
Copy pathrtcSetSerial.pde
File metadata and controls
120 lines (107 loc) · 4.23 KB
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
/*----------------------------------------------------------------------*
* Example sketch for Arduino MCP79412 Library by Jack Christensen *
* *
* Set the RTC by entering a "Set" command on the serial monitor. *
* Use a 24-hour clock and enter the command (case sensitive) exactly *
* as follows: Set yyyy-mm-dd hh:mm:ss *
* *
* This work is licensed under the Creative Commons Attribution- *
* ShareAlike 3.0 Unported License. To view a copy of this license, *
* visit http://creativecommons.org/licenses/by-sa/3.0/ or send a *
* letter to Creative Commons, 171 Second Street, Suite 300, *
* San Francisco, California, 94105, USA. *
*----------------------------------------------------------------------*/
#include <MCP79412RTC.h> //http://github.com/JChristensen/MCP79412RTC
#include <Time.h> //http://www.arduino.cc/playground/Code/Time
#include <Wire.h> //http://arduino.cc/en/Reference/Wire (included with Arduino IDE)
#define PRINT_INTERVAL 1000 //ms between printing the time
unsigned long ms, msLast;
void setup()
{
delay(2000);
Serial.begin(9600);
setSyncProvider(RTC.get); //the function to get the time from the RTC
Serial.print("RTC SYNC");
if (timeStatus() != timeSet) Serial.print(" FAIL");
Serial.println();
}
void loop(void)
{
ms = millis();
readCommand();
if (ms - msLast >= PRINT_INTERVAL) {
msLast = ms;
printTime(now());
}
}
//Read command from the Arduino serial monitor to set the RTC.
//Case-sensitive and must be entered exactly as (24-hour clock):
// Set yyyy-mm-dd hh:mm:ss
void readCommand()
{
char cmd[24] = "Set yyyy-mm-dd hh:mm:ss";
static int i;
tmElements_t tmSet;
time_t tSet;
if (Serial.available() >= 23) { //enough characters for the whole command?
i = 0; //yes, read the available characters
while (Serial.available() > 0) {
if (i >= sizeof(cmd) - 1) { //more than we can enjoy
flushInput(); //clear out the input buffer
Serial.print("Too long: ");
Serial.println(cmd);
return;
}
delay(2); //let the next character trickle in
cmd[i++] = char(Serial.read());
}
cmd[i] = 0; //put in string terminator
if (strncmp(cmd, "Set ", 4) == 0) {
tmSet.Year = 1000 * (cmd[4] - '0') + 100 * (cmd[5] - '0') + 10 * (cmd[6] - '0') + cmd[7] - '0' - 1970;
tmSet.Month = 10 * (cmd[9] - '0') + cmd[10] - '0';
tmSet.Day = 10 * (cmd[12] - '0') + cmd[13] - '0';
tmSet.Hour = 10 * (cmd[15] - '0') + cmd[16] - '0';
tmSet.Minute = 10 * (cmd[18] - '0') + cmd[19] - '0';
tmSet.Second = 10 * (cmd[21] - '0') + cmd[22] - '0';
tSet = makeTime(tmSet); //convert to time_t
setTime(tSet); //set the system time
RTC.set(now()); //set the rtc
Serial.println("RTC set!");
flushInput(); //discard any extraneous trailing characters
}
else {
Serial.print("Unknown: ");
Serial.println(cmd);
}
}
}
void flushInput(void)
{
do {
delay(2);
Serial.read();
} while (Serial.available() > 0);
}
//Print time (and date) given a time_t value
void printTime(time_t t)
{
printI00(hour(t), ':');
printI00(minute(t), ':');
printI00(second(t), ' ');
Serial.print(dayShortStr(weekday(t)));
Serial.print(' ');
printI00(day(t), ' ');
Serial.print(monthShortStr(month(t)));
Serial.print(' ');
Serial.println(year(t));
}
//Print an integer in "00" format (with leading zero),
//followed by a delimiter.
//Input value assumed to be between 0 and 99.
void printI00(int val, char delim)
{
if (val < 10) Serial.print('0');
Serial.print(val);
Serial.print(delim);
return;
}