Skip to content

Commit

Permalink
RTC: remove Serial.print()s from callback functions (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobhylen authored Nov 13, 2023
1 parent 8d0a28d commit b6987b0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
// Include the RTC library
#include "RTC.h"

bool alarmFlag = false;

DayOfWeek convertDayOfWeek(String s)
{
if (s == String("Mon"))
Expand Down Expand Up @@ -139,20 +141,7 @@ RTCTime currentRTCTime()
}

void alarmCallback() {
Serial.println("An alarm was triggered at:");
RTCTime currentTime;
RTC.getTime(currentTime);
Serial.print(currentTime.getYear());
Serial.print("-");
Serial.print(Month2int(currentTime.getMonth()));
Serial.print("-");
Serial.print(currentTime.getDayOfMonth());
Serial.print(" ");
Serial.print(currentTime.getHour());
Serial.print(":");
Serial.print(currentTime.getMinutes());
Serial.print(":");
Serial.println(currentTime.getSeconds());
alarmFlag = true;
}

void setup()
Expand Down Expand Up @@ -195,4 +184,21 @@ void setup()

void loop()
{
if(alarmFlag){
Serial.println("An alarm was triggered at:");
RTCTime currentTime;
RTC.getTime(currentTime);
Serial.print(currentTime.getYear());
Serial.print("-");
Serial.print(Month2int(currentTime.getMonth()));
Serial.print("-");
Serial.print(currentTime.getDayOfMonth());
Serial.print(" ");
Serial.print(currentTime.getHour());
Serial.print(":");
Serial.print(currentTime.getMinutes());
Serial.print(":");
Serial.println(currentTime.getSeconds());
alarmFlag = false;
}
}
49 changes: 33 additions & 16 deletions libraries/RTC/examples/Test_RTC/Test_RTC.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,19 @@
// Define the interrupt pin for LED control during interrupts
const int LED_ON_INTERRUPT = 22;

bool periodicFlag = false;
bool alarmFlag = false;

// Callback function for periodic interrupt
void periodic_cbk() {
static bool clb_st = false;

// Toggle the LED based on callback state
if (clb_st) {
digitalWrite(LED_ON_INTERRUPT, HIGH);
}
else {
digitalWrite(LED_ON_INTERRUPT, LOW);
}

clb_st = !clb_st; // Toggle callback state

// Print message indicating periodic interrupt
Serial.println("PERIODIC INTERRUPT");
periodicFlag = true;
}

// Callback function for alarm interrupt
void alarm_cbk() {
Serial.println("ALARM INTERRUPT");
alarmFlag = true;
}

// Callback function for alarm interrupt
void setup() {
// Initialize serial communication
Serial.begin(9600);
Expand Down Expand Up @@ -91,6 +81,33 @@ void loop() {

RTCTime currenttime;

if(periodicFlag){
// Print message indicating periodic interrupt
Serial.println("PERIODIC INTERRUPT");

static bool clb_st = false;

// Toggle the LED based on callback state
if (clb_st) {
digitalWrite(LED_ON_INTERRUPT, HIGH);
}
else {
digitalWrite(LED_ON_INTERRUPT, LOW);
}

clb_st = !clb_st; // Toggle callback state

periodicFlag = false;
}

if(alarmFlag){
// Print message indicating alarm interrupt
Serial.println("ALARM INTERRUPT");

alarmFlag = false;
}


// Check if RTC is running and print status
if (status) {

Expand Down

0 comments on commit b6987b0

Please sign in to comment.