Skip to content

Commit bb7e981

Browse files
authored
Merge pull request #1370 from arduino/karlsoderby/uno-r4-rtc-mend
[UNO-R4] update rtc docs
2 parents 58a9699 + 798cff7 commit bb7e981

File tree

2 files changed

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

2 files changed

+60
-32
lines changed

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

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -178,36 +178,50 @@ To use this, you will need to initialize the periodic callback, using the `setPe
178178
- `RTC.setPeriodicCallback(periodic_cbk, Period::ONCE_EVERY_2_SEC)`
179179

180180
You will also need to create a function that will be called:
181-
- `void periodic_cbk() { code to be executed }`
181+
- `void periodicCallback() { code to be executed }`
182+
183+
***Note the IRQ has a very fast execution time. Placing a lot of code is not a good practice, so in the example below we are only switching a single flag, `irqFlag`.***
182184

183185
The example below blinks a light every 2 seconds:
184186

185187
```arduino
186188
#include "RTC.h"
187189
188-
const int LED_ON_INTERRUPT = 22;
190+
volatile bool irqFlag = false;
191+
volatile bool ledState = false;
192+
193+
const int led = LED_BUILTIN;
194+
195+
void setup() {
196+
pinMode(led, OUTPUT);
189197
190-
void setup(){
198+
Serial.begin(9600);
199+
200+
// Initialize the RTC
191201
RTC.begin();
192-
if (!RTC.setPeriodicCallback(periodic_cbk, Period::ONCE_EVERY_2_SEC)) {
202+
203+
// RTC.setTime() must be called for RTC.setPeriodicCallback to work, but it doesn't matter
204+
// what date and time it's set to
205+
RTCTime mytime(30, Month::JUNE, 2023, 13, 37, 00, DayOfWeek::WEDNESDAY, SaveLight::SAVING_TIME_ACTIVE);
206+
RTC.setTime(mytime);
207+
208+
if (!RTC.setPeriodicCallback(periodicCallback, Period::ONCE_EVERY_2_SEC)) {
193209
Serial.println("ERROR: periodic callback not set");
194210
}
195211
}
196212
197-
void loop() {
213+
void loop(){
214+
if(irqFlag){
215+
Serial.println("Timed CallBack");
216+
ledState = !ledState;
217+
digitalWrite(LED_BUILTIN, ledState);
218+
irqFlag = false;
219+
}
198220
}
199221
200-
void periodic_cbk() {
201-
static bool clb_st = false;
202-
if(clb_st) {
203-
digitalWrite(LED_ON_INTERRUPT,HIGH);
204-
}
205-
else {
206-
digitalWrite(LED_ON_INTERRUPT,LOW);
207-
}
208-
clb_st = !clb_st;
209-
210-
Serial.println("PERIODIC INTERRUPT");
222+
void periodicCallback()
223+
{
224+
irqFlag = true;
211225
}
212226
```
213227

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

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -182,36 +182,50 @@ To use this, you will need to initialize the periodic callback, using the `setPe
182182
- `RTC.setPeriodicCallback(periodic_cbk, Period::ONCE_EVERY_2_SEC)`
183183

184184
You will also need to create a function that will be called:
185-
- `void periodic_cbk() { code to be executed }`
185+
- `void periodicCallback() { code to be executed }`
186+
187+
***Note the IRQ has a very fast execution time. Placing a lot of code is not a good practice, so in the example below we are only switching a single flag, `irqFlag`.***
186188

187189
The example below blinks a light every 2 seconds:
188190

189191
```arduino
190192
#include "RTC.h"
191193
192-
const int LED_ON_INTERRUPT = 22;
194+
volatile bool irqFlag = false;
195+
volatile bool ledState = false;
196+
197+
const int led = LED_BUILTIN;
198+
199+
void setup() {
200+
pinMode(led, OUTPUT);
193201
194-
void setup(){
202+
Serial.begin(9600);
203+
204+
// Initialize the RTC
195205
RTC.begin();
196-
if (!RTC.setPeriodicCallback(periodic_cbk, Period::ONCE_EVERY_2_SEC)) {
206+
207+
// RTC.setTime() must be called for RTC.setPeriodicCallback to work, but it doesn't matter
208+
// what date and time it's set to
209+
RTCTime mytime(30, Month::JUNE, 2023, 13, 37, 00, DayOfWeek::WEDNESDAY, SaveLight::SAVING_TIME_ACTIVE);
210+
RTC.setTime(mytime);
211+
212+
if (!RTC.setPeriodicCallback(periodicCallback, Period::ONCE_EVERY_2_SEC)) {
197213
Serial.println("ERROR: periodic callback not set");
198214
}
199215
}
200216
201-
void loop() {
217+
void loop(){
218+
if(irqFlag){
219+
Serial.println("Timed CallBack");
220+
ledState = !ledState;
221+
digitalWrite(LED_BUILTIN, ledState);
222+
irqFlag = false;
223+
}
202224
}
203225
204-
void periodic_cbk() {
205-
static bool clb_st = false;
206-
if(clb_st) {
207-
digitalWrite(LED_ON_INTERRUPT,HIGH);
208-
}
209-
else {
210-
digitalWrite(LED_ON_INTERRUPT,LOW);
211-
}
212-
clb_st = !clb_st;
213-
214-
Serial.println("PERIODIC INTERRUPT");
226+
void periodicCallback()
227+
{
228+
irqFlag = true;
215229
}
216230
```
217231

0 commit comments

Comments
 (0)