forked from arduino/ArduinoCore-renesas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRTC_Reset.ino
171 lines (160 loc) · 3.74 KB
/
RTC_Reset.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* RTC_Reset
*
* This example sets the RTC with __TIMESTAMP__ macro and can be used to test whether the settings of
* RTC persists after a reset or when VRTC is powered. If VRTC is powered I can disconnect the board
* from the usb cable and the RTC value will persist
*
* Find the full UNO R4 WiFi RTC documentation here:
* https://docs.arduino.cc/tutorials/uno-r4-wifi/rtc
*/
// Include the RTC library
#include "RTC.h"
bool alarmFlag = false;
DayOfWeek convertDayOfWeek(String s)
{
if (s == String("Mon"))
{
return DayOfWeek::MONDAY;
}
if (s == String("Tue"))
{
return DayOfWeek::TUESDAY;
}
if (s == String("Wed"))
{
return DayOfWeek::WEDNESDAY;
}
if (s == String("Thu"))
{
return DayOfWeek::THURSDAY;
}
if (s == String("Fri"))
{
return DayOfWeek::FRIDAY;
}
if (s == String("Sat"))
{
return DayOfWeek::SATURDAY;
}
if (s == String("Sun"))
{
return DayOfWeek::SUNDAY;
}
}
Month convertMonth(String s)
{
if (s == String("Jan"))
{
return Month::JANUARY;
}
if (s == String("Feb"))
{
return Month::FEBRUARY;
}
if (s == String("Mar"))
{
return Month::MARCH;
}
if (s == String("Apr"))
{
return Month::APRIL;
}
if (s == String("May"))
{
return Month::MAY;
}
if (s == String("Jun"))
{
return Month::JUNE;
}
if (s == String("Jul"))
{
return Month::JULY;
}
if (s == String("Aug"))
{
return Month::AUGUST;
}
if (s == String("Sep"))
{
return Month::SEPTEMBER;
}
if (s == String("Oct"))
{
return Month::OCTOBER;
}
if (s == String("Nov"))
{
return Month::NOVEMBER;
}
if (s == String("Dec"))
{
return Month::DECEMBER;
}
}
RTCTime currentRTCTime()
{
// Get a compilation timestamp of the format: Wed May 10 08:54:31 2023
// __TIMESTAMP__ is a GNU C extension macro
// We can't use the standard macros __DATE__ and __TIME__ because they don't provide the day of the week
String timeStamp = __TIMESTAMP__;
// Extract the day of the week
int pos1 = timeStamp.indexOf(" ");
DayOfWeek dayOfWeek = convertDayOfWeek(timeStamp.substring(0, pos1));
// Extract the month
++pos1;
int pos2 = timeStamp.indexOf(" ", pos1);
Month month = convertMonth(timeStamp.substring(pos1, pos2));
// Extract the day
pos1 = ++pos2;
pos2 = timeStamp.indexOf(" ", pos1);
int day = timeStamp.substring(pos1, pos2).toInt();
// Extract the hour
pos1 = ++pos2;
pos2 = timeStamp.indexOf(":", pos1);
int hour = timeStamp.substring(pos1, pos2).toInt();
// Extract the minute
pos1 = ++pos2;
pos2 = timeStamp.indexOf(":", pos1);
int minute = timeStamp.substring(pos1, pos2).toInt();
// Extract the second
pos1 = ++pos2;
pos2 = timeStamp.indexOf(" ", pos1);
int second = timeStamp.substring(pos1, pos2).toInt();
// Extract the year
pos1 = ++pos2;
pos2 = timeStamp.indexOf(" ", pos1);
int year = timeStamp.substring(pos1, pos2).toInt();
return RTCTime(day, month, year, hour, minute, second, dayOfWeek, SaveLight::SAVING_TIME_INACTIVE);
}
void setup()
{
Serial.begin(9600);
while (!Serial) ;
// Initialize the RTC
RTC.begin();
// if the RTC is not running set its value to the compile time __TIMESTAMP__ macro
if(!RTC.isRunning()) {
RTCTime timeToSet = currentRTCTime();
RTC.setTime(timeToSet);
}
}
void loop()
{
Serial.println("The RTC time is: ");
RTCTime currentTime;
RTC.getTime(currentTime);
Serial.print(currentTime.getYear());
Serial.print("-");
Serial.print(Month2int(currentTime.getMonth()));
Serial.print("-");
Serial.print(currentTime.getDayOfMonth());
Serial.print(" ");
Serial.print(currentTime.getHour());
Serial.print(":");
Serial.print(currentTime.getMinutes());
Serial.print(":");
Serial.println(currentTime.getSeconds());
delay(1000);
}