-
Notifications
You must be signed in to change notification settings - Fork 0
/
real-time-clock.ino
130 lines (117 loc) · 3.92 KB
/
real-time-clock.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
/**
* Arduino DS3232RTC Library
* https://github.com/JChristensen/DS3232RTC
* Copyright (C) 2018 by Jack Christensen and licensed under
* GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
*/
#include <DS3232RTC.h>
DS3232RTC RTC;
/**
* Copied from https://github.com/JChristensen/DS3232RTC/blob/master/examples/SetSerial/SetSerial.ino
* Physical modifications including removing the 201 resistor instructions from https://forum.arduino.cc/t/zs-042-ds3231-rtc-module/268862/70
*
* Example sketch to display the date and time from a DS3231
* or DS3232 RTC every second. Display the temperature once per
* minute. (The DS3231 does a temperature conversion once every
* 64 seconds. This is also the default for the DS3232.)
*
* Set the date and time by entering the following on the Arduino
* serial monitor:
* year,month,day,hour,minute,second,
*
* Where
* year can be two or four digits,
* month is 1-12,
* day is 1-31,
* hour is 0-23, and
* minute and second are 0-59.
*
* Entering the final comma delimiter (after "second") will avoid a
* one-second timeout and will allow the RTC to be set more accurately.
*
* No validity checking is done, invalid values or incomplete syntax
* in the input will result in an incorrect RTC setting.
*
* Jack Christensen 08Aug2013
*/
#include <Streaming.h> // http://arduiniana.org/libraries/streaming/
void setup() {
Serial.begin(115200);
// setSyncProvider() causes the Time library to synchronize with the
// external RTC by calling RTC.get() every five minutes by default.
setSyncProvider(RTC.get);
Serial << F("RTC Sync");
if (timeStatus() != timeSet) Serial << F(" FAIL!");
Serial << endl;
}
void loop() {
static time_t tLast;
time_t t;
tmElements_t tm;
// check for input to set the RTC, minimum length is 12, i.e. yy,m,d,h,m,s
if (Serial.available() >= 12) {
// note that the tmElements_t Year member is an offset from 1970,
// but the RTC wants the last two digits of the calendar year.
// use convenience macros from the Time Library to do the conversions.
int y = Serial.parseInt();
if (y >= 100 && y < 1000) {
Serial << F("Error: Year must be two or four digits!") << endl;
} else {
if (y >= 1000) {
tm.Year = CalendarYrToTm(y);
} else { // (y < 100)
tm.Year = y2kYearToTm(y);
}
tm.Month = Serial.parseInt();
tm.Day = Serial.parseInt();
tm.Hour = Serial.parseInt();
tm.Minute = Serial.parseInt();
tm.Second = Serial.parseInt();
t = makeTime(tm);
RTC.set(t); // use time_t value to ensure correct weekday is set
setTime(t);
Serial << F("RTC set to: ");
printDateTime(t);
Serial << endl;
// dump any extraneous input
while (Serial.available() > 0) Serial.read();
}
}
t = now();
if (t != tLast) {
tLast = t;
printDateTime(t);
if (second(t) == 0) {
float c = RTC.temperature() / 4.;
float f = c * 9. / 5. + 32.;
Serial << F(" ") << c << F(" C ") << f << F(" F");
}
Serial << endl;
}
}
// print date and time to Serial
void printDateTime(time_t t) {
printDate(t);
Serial << ' ';
printTime(t);
}
// print time to Serial
void printTime(time_t t) {
printI00(hour(t), ':');
printI00(minute(t), ':');
printI00(second(t), ' ');
}
// print date to Serial
void printDate(time_t t) {
printI00(day(t), 0);
Serial << monthShortStr(month(t)) << _DEC(year(t));
}
// Print an integer in "00" format (with leading zero),
// followed by a delimiter character to Serial.
// Input value assumed to be between 0 and 99.
void printI00(int val, char delim) {
if (val < 10) Serial << '0';
Serial << _DEC(val);
if (delim > 0) Serial << delim;
return;
}