Skip to content

Commit 5f856c8

Browse files
authored
Add an RTC alarm example
Adds an RTC alarm example that switches the LED between blue and red every second.
1 parent d51a8e3 commit 5f856c8

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* This example demonstrates how to use the alarm functionality of the RTC
3+
* (Real Time Clock) on the Portenta C33.
4+
*
5+
* It switches the built-in LED between blue and red each time the alarm
6+
* is triggered, which is once every minute in this example.
7+
*
8+
*/
9+
10+
#include "RTC.h"
11+
12+
void alarmCallback()
13+
{
14+
static bool ledState = false;
15+
if (!ledState) {
16+
digitalWrite(LEDB, HIGH);
17+
digitalWrite(LEDR, LOW);
18+
}
19+
else {
20+
digitalWrite(LEDR, HIGH);
21+
digitalWrite(LEDB, LOW);
22+
}
23+
ledState = !ledState;
24+
}
25+
26+
void setup() {
27+
pinMode(LEDR, OUTPUT);
28+
pinMode(LEDB, OUTPUT);
29+
digitalWrite(LEDR, HIGH);
30+
digitalWrite(LEDB, LOW);
31+
32+
// Initialize the RTC
33+
RTC.begin();
34+
35+
// RTC.setTime() must be called for RTC.setAlarmCallback to work, but it doesn't matter
36+
// what date and time it's set to in this example
37+
RTCTime initialTime(7, Month::JUNE, 2023, 13, 03, 00, DayOfWeek::WEDNESDAY, SaveLight::SAVING_TIME_ACTIVE);
38+
RTC.setTime(initialTime);
39+
40+
// Trigger the alarm every time the seconds are zero
41+
RTCTime alarmTime;
42+
alarmTime.setSecond(0);
43+
44+
// Make sure to only match on the seconds in this example - not on any other parts of the date/time
45+
AlarmMatch matchTime;
46+
matchTime.addMatchSecond();
47+
48+
RTC.setAlarmCallback(alarmCallback, alarmTime, matchTime);
49+
}
50+
51+
void loop() {
52+
}

0 commit comments

Comments
 (0)