Skip to content

Commit d9ca706

Browse files
added example for proper usage of RTC reset
1 parent 19d07a2 commit d9ca706

File tree

1 file changed

+176
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)