11/*
22 * This example demonstrates how to use the alarm functionality of the RTC
3- * (Real Time Clock) on the Portenta C33.
3+ * (Real Time Clock) on the Portenta C33 and UNO R4 Minima / WiFi .
44 *
5- * It switches the built-in LED between blue and red each time the alarm
5+ * It turns on the built-in LED when the alarm
66 * is triggered, which is once every minute in this example.
7+ * In addition, inside the loop, we print the state of the alarm
8+ * continuously, which is either 0 (LOW) or 1 (HIGH).
79 *
10+ * Note that the Portenta C33's LED is inverted and will be lit when
11+ * the state is 0 (LOW).
812 */
913
10- #include " RTC.h"
14+ unsigned long previousMillis = 0 ;
15+ const long interval = 1000 ;
16+ bool ledState = false ;
1117
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- }
18+ #include " RTC.h"
2519
2620void setup () {
27- pinMode (LEDR, OUTPUT);
28- pinMode (LEDB, OUTPUT);
29- digitalWrite (LEDR, HIGH);
30- digitalWrite (LEDB, LOW);
21+ // initialize Serial Communication
22+ Serial.begin (9600 );
23+
24+ // define LED as output
25+ pinMode (LED_BUILTIN, OUTPUT);
3126
3227 // Initialize the RTC
3328 RTC.begin ();
@@ -44,9 +39,31 @@ void setup() {
4439 // Make sure to only match on the seconds in this example - not on any other parts of the date/time
4540 AlarmMatch matchTime;
4641 matchTime.addMatchSecond ();
47-
42+
43+ // sets the alarm callback
4844 RTC.setAlarmCallback (alarmCallback, alarmTime, matchTime);
4945}
5046
5147void loop () {
48+
49+ // in the loop, we continuously print the alarm's current state
50+ // this is for debugging only and has no effect on the alarm whatsoever
51+ unsigned long currentMillis = millis ();
52+ if (currentMillis - previousMillis >= interval) {
53+ // save the last time you blinked the LED
54+ previousMillis = currentMillis;
55+ Serial.print (" Alarm state: " );
56+ Serial.println (ledState);
57+ }
58+ }
59+
60+ // this function activates every minute
61+ // and changes the ledState boolean
62+ void alarmCallback () {
63+ if (!ledState) {
64+ digitalWrite (LED_BUILTIN, HIGH);
65+ } else {
66+ digitalWrite (LED_BUILTIN, LOW);
67+ }
68+ ledState = !ledState;
5269}
0 commit comments