Skip to content

Commit 6c22c8b

Browse files
added example for proper usage of RTC reset
1 parent 3d0317b commit 6c22c8b

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
* RTC_Reset
3+
*
4+
* This example sets the RTC with __TIMESTAMP__ macro and can be used to test whether the settings of
5+
* RTC persists after a reset or when VRTC is powered. If VRTC is powered I can disconnect the board
6+
* from the usb cable and the RTC value will persist
7+
*
8+
* Find the full UNO R4 WiFi RTC documentation here:
9+
* https://docs.arduino.cc/tutorials/uno-r4-wifi/rtc
10+
*/
11+
12+
// Include the RTC library
13+
#include "RTC.h"
14+
15+
bool alarmFlag = false;
16+
17+
DayOfWeek convertDayOfWeek(String s)
18+
{
19+
if (s == String("Mon"))
20+
{
21+
return DayOfWeek::MONDAY;
22+
}
23+
if (s == String("Tue"))
24+
{
25+
return DayOfWeek::TUESDAY;
26+
}
27+
if (s == String("Wed"))
28+
{
29+
return DayOfWeek::WEDNESDAY;
30+
}
31+
if (s == String("Thu"))
32+
{
33+
return DayOfWeek::THURSDAY;
34+
}
35+
if (s == String("Fri"))
36+
{
37+
return DayOfWeek::FRIDAY;
38+
}
39+
if (s == String("Sat"))
40+
{
41+
return DayOfWeek::SATURDAY;
42+
}
43+
if (s == String("Sun"))
44+
{
45+
return DayOfWeek::SUNDAY;
46+
}
47+
}
48+
49+
Month convertMonth(String s)
50+
{
51+
if (s == String("Jan"))
52+
{
53+
return Month::JANUARY;
54+
}
55+
if (s == String("Feb"))
56+
{
57+
return Month::FEBRUARY;
58+
}
59+
if (s == String("Mar"))
60+
{
61+
return Month::MARCH;
62+
}
63+
if (s == String("Apr"))
64+
{
65+
return Month::APRIL;
66+
}
67+
if (s == String("May"))
68+
{
69+
return Month::MAY;
70+
}
71+
if (s == String("Jun"))
72+
{
73+
return Month::JUNE;
74+
}
75+
if (s == String("Jul"))
76+
{
77+
return Month::JULY;
78+
}
79+
if (s == String("Aug"))
80+
{
81+
return Month::AUGUST;
82+
}
83+
if (s == String("Sep"))
84+
{
85+
return Month::SEPTEMBER;
86+
}
87+
if (s == String("Oct"))
88+
{
89+
return Month::OCTOBER;
90+
}
91+
if (s == String("Nov"))
92+
{
93+
return Month::NOVEMBER;
94+
}
95+
if (s == String("Dec"))
96+
{
97+
return Month::DECEMBER;
98+
}
99+
}
100+
101+
RTCTime currentRTCTime()
102+
{
103+
// Get a compilation timestamp of the format: Wed May 10 08:54:31 2023
104+
// __TIMESTAMP__ is a GNU C extension macro
105+
// We can't use the standard macros __DATE__ and __TIME__ because they don't provide the day of the week
106+
String timeStamp = __TIMESTAMP__;
107+
// Extract the day of the week
108+
int pos1 = timeStamp.indexOf(" ");
109+
DayOfWeek dayOfWeek = convertDayOfWeek(timeStamp.substring(0, pos1));
110+
// Extract the month
111+
++pos1;
112+
int pos2 = timeStamp.indexOf(" ", pos1);
113+
Month month = convertMonth(timeStamp.substring(pos1, pos2));
114+
// Extract the day
115+
pos1 = ++pos2;
116+
pos2 = timeStamp.indexOf(" ", pos1);
117+
int day = timeStamp.substring(pos1, pos2).toInt();
118+
// Extract the hour
119+
pos1 = ++pos2;
120+
pos2 = timeStamp.indexOf(":", pos1);
121+
int hour = timeStamp.substring(pos1, pos2).toInt();
122+
// Extract the minute
123+
pos1 = ++pos2;
124+
pos2 = timeStamp.indexOf(":", pos1);
125+
int minute = timeStamp.substring(pos1, pos2).toInt();
126+
// Extract the second
127+
pos1 = ++pos2;
128+
pos2 = timeStamp.indexOf(" ", pos1);
129+
int second = timeStamp.substring(pos1, pos2).toInt();
130+
// Extract the year
131+
pos1 = ++pos2;
132+
pos2 = timeStamp.indexOf(" ", pos1);
133+
int year = timeStamp.substring(pos1, pos2).toInt();
134+
135+
return RTCTime(day, month, year, hour, minute, second, dayOfWeek, SaveLight::SAVING_TIME_INACTIVE);
136+
}
137+
138+
void setup()
139+
{
140+
Serial.begin(9600);
141+
while (!Serial) ;
142+
143+
// Initialize the RTC
144+
RTC.begin();
145+
146+
// if the RTC is not running set its value to the compile time __TIMESTAMP__ macro
147+
if(!RTC.isRunning()) {
148+
RTCTime timeToSet = currentRTCTime();
149+
RTC.setTime(timeToSet);
150+
}
151+
}
152+
153+
void loop()
154+
{
155+
Serial.println("The RTC time is: ");
156+
RTCTime currentTime;
157+
RTC.getTime(currentTime);
158+
Serial.print(currentTime.getYear());
159+
Serial.print("-");
160+
Serial.print(Month2int(currentTime.getMonth()));
161+
Serial.print("-");
162+
Serial.print(currentTime.getDayOfMonth());
163+
Serial.print(" ");
164+
Serial.print(currentTime.getHour());
165+
Serial.print(":");
166+
Serial.print(currentTime.getMinutes());
167+
Serial.print(":");
168+
Serial.println(currentTime.getSeconds());
169+
170+
delay(1000);
171+
}

0 commit comments

Comments
 (0)