Skip to content

Commit b6987b0

Browse files
authored
RTC: remove Serial.print()s from callback functions (#178)
1 parent 8d0a28d commit b6987b0

File tree

2 files changed

+53
-30
lines changed

2 files changed

+53
-30
lines changed

Diff for: libraries/RTC/examples/RTC_AutomaticExample/RTC_AutomaticExample.ino

+20-14
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
// Include the RTC library
1818
#include "RTC.h"
1919

20+
bool alarmFlag = false;
21+
2022
DayOfWeek convertDayOfWeek(String s)
2123
{
2224
if (s == String("Mon"))
@@ -139,20 +141,7 @@ RTCTime currentRTCTime()
139141
}
140142

141143
void alarmCallback() {
142-
Serial.println("An alarm was triggered at:");
143-
RTCTime currentTime;
144-
RTC.getTime(currentTime);
145-
Serial.print(currentTime.getYear());
146-
Serial.print("-");
147-
Serial.print(Month2int(currentTime.getMonth()));
148-
Serial.print("-");
149-
Serial.print(currentTime.getDayOfMonth());
150-
Serial.print(" ");
151-
Serial.print(currentTime.getHour());
152-
Serial.print(":");
153-
Serial.print(currentTime.getMinutes());
154-
Serial.print(":");
155-
Serial.println(currentTime.getSeconds());
144+
alarmFlag = true;
156145
}
157146

158147
void setup()
@@ -195,4 +184,21 @@ void setup()
195184

196185
void loop()
197186
{
187+
if(alarmFlag){
188+
Serial.println("An alarm was triggered at:");
189+
RTCTime currentTime;
190+
RTC.getTime(currentTime);
191+
Serial.print(currentTime.getYear());
192+
Serial.print("-");
193+
Serial.print(Month2int(currentTime.getMonth()));
194+
Serial.print("-");
195+
Serial.print(currentTime.getDayOfMonth());
196+
Serial.print(" ");
197+
Serial.print(currentTime.getHour());
198+
Serial.print(":");
199+
Serial.print(currentTime.getMinutes());
200+
Serial.print(":");
201+
Serial.println(currentTime.getSeconds());
202+
alarmFlag = false;
203+
}
198204
}

Diff for: libraries/RTC/examples/Test_RTC/Test_RTC.ino

+33-16
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,19 @@
1414
// Define the interrupt pin for LED control during interrupts
1515
const int LED_ON_INTERRUPT = 22;
1616

17+
bool periodicFlag = false;
18+
bool alarmFlag = false;
19+
1720
// Callback function for periodic interrupt
1821
void periodic_cbk() {
19-
static bool clb_st = false;
20-
21-
// Toggle the LED based on callback state
22-
if (clb_st) {
23-
digitalWrite(LED_ON_INTERRUPT, HIGH);
24-
}
25-
else {
26-
digitalWrite(LED_ON_INTERRUPT, LOW);
27-
}
28-
29-
clb_st = !clb_st; // Toggle callback state
30-
31-
// Print message indicating periodic interrupt
32-
Serial.println("PERIODIC INTERRUPT");
22+
periodicFlag = true;
3323
}
3424

25+
// Callback function for alarm interrupt
3526
void alarm_cbk() {
36-
Serial.println("ALARM INTERRUPT");
27+
alarmFlag = true;
3728
}
3829

39-
// Callback function for alarm interrupt
4030
void setup() {
4131
// Initialize serial communication
4232
Serial.begin(9600);
@@ -91,6 +81,33 @@ void loop() {
9181

9282
RTCTime currenttime;
9383

84+
if(periodicFlag){
85+
// Print message indicating periodic interrupt
86+
Serial.println("PERIODIC INTERRUPT");
87+
88+
static bool clb_st = false;
89+
90+
// Toggle the LED based on callback state
91+
if (clb_st) {
92+
digitalWrite(LED_ON_INTERRUPT, HIGH);
93+
}
94+
else {
95+
digitalWrite(LED_ON_INTERRUPT, LOW);
96+
}
97+
98+
clb_st = !clb_st; // Toggle callback state
99+
100+
periodicFlag = false;
101+
}
102+
103+
if(alarmFlag){
104+
// Print message indicating alarm interrupt
105+
Serial.println("ALARM INTERRUPT");
106+
107+
alarmFlag = false;
108+
}
109+
110+
94111
// Check if RTC is running and print status
95112
if (status) {
96113

0 commit comments

Comments
 (0)