Skip to content

Commit 9d403bc

Browse files
committed
Simplify RTC example
1 parent b0d2bc5 commit 9d403bc

File tree

1 file changed

+33
-50
lines changed

1 file changed

+33
-50
lines changed

examples/WakeFromRTC/WakeFromRTC.ino

+33-50
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
/*
22
********************************************************************************
3-
* WakeFromRTC.ino
43
*
54
* This example demonstrates how to use the RTC to wake up the
65
* Portenta C33 from deep sleep. The device will go to sleep for 1 second and
76
* then wake up. The built-in LED will blink every second.
87
*
98
* The example also demonstrates how to use the PF1550 PMIC to turn off the peripherals
109
* before going to sleep and turn them back on after waking up.
11-
* uncomment #define TURN_PERIPHERALS_OFF on line 24 to enable this feature.
10+
* uncomment #define TURN_PERIPHERALS_OFF on line 23 to enable this feature.
1211
*
1312
* INSTRUCTIONS:
1413
* - Make sure you are running the latest version of the Renesas Core
@@ -50,70 +49,54 @@ RTCTime initialTime(1, Month::JANUARY, 2000, 12, 10, 00, DayOfWeek::TUESDAY, Sav
5049
}
5150
#endif
5251

53-
bool sleepFor(int hours, int minutes, int seconds)
54-
{
55-
if(RTC.isRunning()){
56-
// Get the current time from the RTC
57-
RTCTime currentTime;
58-
if (!RTC.getTime(currentTime)) {
59-
return false; // Failed to get current time
60-
}
61-
62-
// Convert current time to UNIX timestamp and add the desired interval
63-
time_t currentTimestamp = currentTime.getUnixTime();
64-
currentTimestamp += hours * 3600 + minutes * 60 + seconds;
65-
66-
// Convert back to RTCTime
67-
RTCTime alarmTime(currentTimestamp);
68-
69-
70-
// Configure the alarm match criteria
71-
AlarmMatch match;
72-
match.addMatchSecond(); // Trigger the alarm when the seconds match
73-
match.addMatchMinute(); // Trigger the alarm when the minutes match
74-
match.addMatchHour(); // Trigger the alarm when the hours match
75-
76-
// Set the alarm
77-
if (!RTC.setAlarm(alarmTime, match)) {
78-
return false; // Failed to set the alarm
79-
}
52+
/**
53+
* Initializes the Real-Time Clock (RTC) and sets the initial time if the RTC is not running.
54+
* This initial time is a dummy time. You can obtain the current time from an NTP server and set it here.
55+
*/
56+
void initializeRealTimeClock(){
57+
RTC.begin();
8058

81-
// Enable the alarm
82-
return true;
59+
if (!RTC.isRunning()) {
60+
RTC.setTime(initialTime);
8361
}
8462
}
8563

8664
void setup(){
8765
lowPower = LowPower();
88-
lowPower.enableWakeupFromRTC();
8966

9067
#ifdef TURN_PERIPHERALS_OFF
9168
PMIC.begin();
9269
turnPeripheralsOn();
9370
#endif
9471

95-
// Initialize the RTC
96-
RTC.begin();
97-
98-
// Set the initial time if the RTC is not running
99-
if (!RTC.isRunning()) {
100-
RTC.setTime(initialTime);
101-
}
72+
initializeRealTimeClock();
10273

103-
pinMode(LED_BUILTIN, OUTPUT); // Set the LED pin as an output
74+
pinMode(LED_BUILTIN, OUTPUT); // Set the LED pin as an output
75+
pinMode(LEDR, OUTPUT); // Use the red LED to indicate errors
76+
digitalWrite(LEDR, HIGH); // Turn off the red LED
10477
digitalWrite(LED_BUILTIN, LOW); // Turn on the LED
105-
delay(1000); // lets the user see the led for 1 second
78+
delay(5000); // lets the user see the led for 5 seconds
10679

107-
// The device will wake up every second and blink the LED
80+
// The device will go to sleep every 5 seconds and wake up after 5 seconds to blink the LED
10881
// effectivelly creating the same efect as the blink sketch
109-
if(sleepFor(0, 0, 1)){
110-
// turn peripherals off before going to sleep
111-
#ifdef TURN_PERIPHERALS_OFF
112-
turnPeripheralsOff();
113-
#endif
114-
lowPower.deepSleep();
82+
if(!lowPower.setWakeUpAlarm(0, 0, 5)){
83+
// Blink the red LED indefinitely to indicate an error
84+
while(true){
85+
digitalWrite(LEDR, HIGH);
86+
delay(500);
87+
digitalWrite(LEDR, LOW);
88+
delay(500);
89+
}
11590
}
116-
}
11791

118-
void loop(){
92+
#ifdef TURN_PERIPHERALS_OFF
93+
// turn peripherals off before going to sleep
94+
turnPeripheralsOff();
95+
#else
96+
// Turn off the LED to indicate the device is going to sleep
97+
digitalWrite(LED_BUILTIN, HIGH);
98+
#endif
99+
lowPower.deepSleep();
119100
}
101+
102+
void loop(){}

0 commit comments

Comments
 (0)