Skip to content

Commit 73dfd94

Browse files
committed
Update alarm examples to match core examples
1 parent 5ff1649 commit 73dfd94

File tree

2 files changed

+88
-16
lines changed
  • content/hardware/02.hero/boards
    • uno-r4-minima/tutorials/rtc
    • uno-r4-wifi/tutorials/rtc

2 files changed

+88
-16
lines changed

content/hardware/02.hero/boards/uno-r4-minima/tutorials/rtc/rtc.md

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -243,26 +243,62 @@ The period can be specified using the following enumerations:
243243
- `RTC.setAlarmCallback(alarm_cbk, alarmtime, am)`
244244

245245
```arduino
246+
unsigned long previousMillis = 0;
247+
const long interval = 1000;
248+
bool ledState = false;
249+
250+
// Include the RTC library
246251
#include "RTC.h"
247252
248253
void setup() {
254+
//initialize Serial Communication
249255
Serial.begin(9600);
250256
257+
//define LED as output
258+
pinMode(LED_BUILTIN, OUTPUT);
259+
260+
// Initialize the RTC
251261
RTC.begin();
252262
253-
RTCTime alarmtime;
254-
alarmtime.setSecond(35);
263+
// RTC.setTime() must be called for RTC.setAlarmCallback to work, but it doesn't matter
264+
// what date and time it's set to in this example
265+
RTCTime initialTime(7, Month::JUNE, 2023, 13, 03, 00, DayOfWeek::WEDNESDAY, SaveLight::SAVING_TIME_ACTIVE);
266+
RTC.setTime(initialTime);
255267
256-
AlarmMatch am;
257-
am.addMatchSecond();
268+
// Trigger the alarm every time the seconds are zero
269+
RTCTime alarmTime;
270+
alarmTime.setSecond(0);
258271
259-
if (!RTC.setAlarmCallback(alarm_cbk, alarmtime, am)) {
260-
Serial.println("ERROR: alarm callback not set");
272+
// Make sure to only match on the seconds in this example - not on any other parts of the date/time
273+
AlarmMatch matchTime;
274+
matchTime.addMatchSecond();
275+
276+
//sets the alarm callback
277+
RTC.setAlarmCallback(alarmCallback, alarmTime, matchTime);
278+
}
279+
280+
void loop() {
281+
282+
// in the loop, we continuously print the alarm's current state
283+
// this is for debugging only and has no effect on the alarm whatsoever
284+
unsigned long currentMillis = millis();
285+
if (currentMillis - previousMillis >= interval) {
286+
// save the last time you blinked the LED
287+
previousMillis = currentMillis;
288+
Serial.print("Alarm state: ");
289+
Serial.println(ledState);
261290
}
262291
}
263292
264-
void alarm_cbk() {
265-
Serial.println("ALARM INTERRUPT");
293+
// this function activates every minute
294+
// and changes the ledState boolean
295+
void alarmCallback() {
296+
if (!ledState) {
297+
digitalWrite(LED_BUILTIN, HIGH);
298+
} else {
299+
digitalWrite(LED_BUILTIN, LOW);
300+
}
301+
ledState = !ledState;
266302
}
267303
```
268304

content/hardware/02.hero/boards/uno-r4-wifi/tutorials/rtc/rtc.md

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,26 +247,62 @@ The period can be specified using the following enumerations:
247247
- `RTC.setAlarmCallback(alarm_cbk, alarmtime, am)`
248248

249249
```arduino
250+
unsigned long previousMillis = 0;
251+
const long interval = 1000;
252+
bool ledState = false;
253+
254+
// Include the RTC library
250255
#include "RTC.h"
251256
252257
void setup() {
258+
//initialize Serial Communication
253259
Serial.begin(9600);
254260
261+
//define LED as output
262+
pinMode(LED_BUILTIN, OUTPUT);
263+
264+
// Initialize the RTC
255265
RTC.begin();
256266
257-
RTCTime alarmtime;
258-
alarmtime.setSecond(35);
267+
// RTC.setTime() must be called for RTC.setAlarmCallback to work, but it doesn't matter
268+
// what date and time it's set to in this example
269+
RTCTime initialTime(7, Month::JUNE, 2023, 13, 03, 00, DayOfWeek::WEDNESDAY, SaveLight::SAVING_TIME_ACTIVE);
270+
RTC.setTime(initialTime);
259271
260-
AlarmMatch am;
261-
am.addMatchSecond();
272+
// Trigger the alarm every time the seconds are zero
273+
RTCTime alarmTime;
274+
alarmTime.setSecond(0);
262275
263-
if (!RTC.setAlarmCallback(alarm_cbk, alarmtime, am)) {
264-
Serial.println("ERROR: alarm callback not set");
276+
// Make sure to only match on the seconds in this example - not on any other parts of the date/time
277+
AlarmMatch matchTime;
278+
matchTime.addMatchSecond();
279+
280+
//sets the alarm callback
281+
RTC.setAlarmCallback(alarmCallback, alarmTime, matchTime);
282+
}
283+
284+
void loop() {
285+
286+
// in the loop, we continuously print the alarm's current state
287+
// this is for debugging only and has no effect on the alarm whatsoever
288+
unsigned long currentMillis = millis();
289+
if (currentMillis - previousMillis >= interval) {
290+
// save the last time you blinked the LED
291+
previousMillis = currentMillis;
292+
Serial.print("Alarm state: ");
293+
Serial.println(ledState);
265294
}
266295
}
267296
268-
void alarm_cbk() {
269-
Serial.println("ALARM INTERRUPT");
297+
// this function activates every minute
298+
// and changes the ledState boolean
299+
void alarmCallback() {
300+
if (!ledState) {
301+
digitalWrite(LED_BUILTIN, HIGH);
302+
} else {
303+
digitalWrite(LED_BUILTIN, LOW);
304+
}
305+
ledState = !ledState;
270306
}
271307
```
272308

0 commit comments

Comments
 (0)