From b3437b66d1dcf842bd07e6b13c122ee7256be138 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl=20S=C3=B6derby?= <35461661+karlsoderby@users.noreply.github.com> Date: Tue, 4 Jul 2023 11:57:41 +0200 Subject: [PATCH] Update RTC_Alarm.ino Update RTC_Alarm to work with UNO R4 Minima & WiFi boards --- .../RTC/examples/RTC_Alarm/RTC_Alarm.ino | 59 ++++++++++++------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/libraries/RTC/examples/RTC_Alarm/RTC_Alarm.ino b/libraries/RTC/examples/RTC_Alarm/RTC_Alarm.ino index 7b17d7c73..03ebc05df 100644 --- a/libraries/RTC/examples/RTC_Alarm/RTC_Alarm.ino +++ b/libraries/RTC/examples/RTC_Alarm/RTC_Alarm.ino @@ -1,33 +1,28 @@ /* * This example demonstrates how to use the alarm functionality of the RTC - * (Real Time Clock) on the Portenta C33. + * (Real Time Clock) on the Portenta C33 and UNO R4 Minima / WiFi. * - * It switches the built-in LED between blue and red each time the alarm + * It turns on the built-in LED when the alarm * is triggered, which is once every minute in this example. + * In addition, inside the loop, we print the state of the alarm + * continuously, which is either 0 (LOW) or 1 (HIGH). * + * Note that the Portenta C33's LED is inverted and will be lit when + * the state is 0 (LOW). */ -#include "RTC.h" +unsigned long previousMillis = 0; +const long interval = 1000; +bool ledState = false; -void alarmCallback() -{ - static bool ledState = false; - if (!ledState) { - digitalWrite(LEDB, HIGH); - digitalWrite(LEDR, LOW); - } - else { - digitalWrite(LEDR, HIGH); - digitalWrite(LEDB, LOW); - } - ledState = !ledState; -} +#include "RTC.h" void setup() { - pinMode(LEDR, OUTPUT); - pinMode(LEDB, OUTPUT); - digitalWrite(LEDR, HIGH); - digitalWrite(LEDB, LOW); + //initialize Serial Communication + Serial.begin(9600); + + //define LED as output + pinMode(LED_BUILTIN, OUTPUT); // Initialize the RTC RTC.begin(); @@ -44,9 +39,31 @@ void setup() { // Make sure to only match on the seconds in this example - not on any other parts of the date/time AlarmMatch matchTime; matchTime.addMatchSecond(); - + + //sets the alarm callback RTC.setAlarmCallback(alarmCallback, alarmTime, matchTime); } void loop() { + + // in the loop, we continuously print the alarm's current state + // this is for debugging only and has no effect on the alarm whatsoever + unsigned long currentMillis = millis(); + if (currentMillis - previousMillis >= interval) { + // save the last time you blinked the LED + previousMillis = currentMillis; + Serial.print("Alarm state: "); + Serial.println(ledState); + } +} + +// this function activates every minute +// and changes the ledState boolean +void alarmCallback() { + if (!ledState) { + digitalWrite(LED_BUILTIN, HIGH); + } else { + digitalWrite(LED_BUILTIN, LOW); + } + ledState = !ledState; }