Skip to content

Update RTC_Alarm.ino #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 38 additions & 21 deletions libraries/RTC/examples/RTC_Alarm/RTC_Alarm.ino
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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;
}