1
1
/*
2
2
* This example demonstrates how to use the alarm functionality of the RTC
3
- * (Real Time Clock) on the Portenta C33.
3
+ * (Real Time Clock) on the Portenta C33 and UNO R4 Minima / WiFi .
4
4
*
5
- * It switches the built-in LED between blue and red each time the alarm
5
+ * It turns on the built-in LED when the alarm
6
6
* is triggered, which is once every minute in this example.
7
+ * In addition, inside the loop, we print the state of the alarm
8
+ * continuously, which is either 0 (LOW) or 1 (HIGH).
7
9
*
10
+ * Note that the Portenta C33's LED is inverted and will be lit when
11
+ * the state is 0 (LOW).
8
12
*/
9
13
10
- #include " RTC.h"
14
+ unsigned long previousMillis = 0 ;
15
+ const long interval = 1000 ;
16
+ bool ledState = false ;
11
17
12
- void alarmCallback ()
13
- {
14
- static bool ledState = false ;
15
- if (!ledState) {
16
- digitalWrite (LEDB, HIGH);
17
- digitalWrite (LEDR, LOW);
18
- }
19
- else {
20
- digitalWrite (LEDR, HIGH);
21
- digitalWrite (LEDB, LOW);
22
- }
23
- ledState = !ledState;
24
- }
18
+ #include " RTC.h"
25
19
26
20
void setup () {
27
- pinMode (LEDR, OUTPUT);
28
- pinMode (LEDB, OUTPUT);
29
- digitalWrite (LEDR, HIGH);
30
- digitalWrite (LEDB, LOW);
21
+ // initialize Serial Communication
22
+ Serial.begin (9600 );
23
+
24
+ // define LED as output
25
+ pinMode (LED_BUILTIN, OUTPUT);
31
26
32
27
// Initialize the RTC
33
28
RTC.begin ();
@@ -44,9 +39,31 @@ void setup() {
44
39
// Make sure to only match on the seconds in this example - not on any other parts of the date/time
45
40
AlarmMatch matchTime;
46
41
matchTime.addMatchSecond ();
47
-
42
+
43
+ // sets the alarm callback
48
44
RTC.setAlarmCallback (alarmCallback, alarmTime, matchTime);
49
45
}
50
46
51
47
void loop () {
48
+
49
+ // in the loop, we continuously print the alarm's current state
50
+ // this is for debugging only and has no effect on the alarm whatsoever
51
+ unsigned long currentMillis = millis ();
52
+ if (currentMillis - previousMillis >= interval) {
53
+ // save the last time you blinked the LED
54
+ previousMillis = currentMillis;
55
+ Serial.print (" Alarm state: " );
56
+ Serial.println (ledState);
57
+ }
58
+ }
59
+
60
+ // this function activates every minute
61
+ // and changes the ledState boolean
62
+ void alarmCallback () {
63
+ if (!ledState) {
64
+ digitalWrite (LED_BUILTIN, HIGH);
65
+ } else {
66
+ digitalWrite (LED_BUILTIN, LOW);
67
+ }
68
+ ledState = !ledState;
52
69
}
0 commit comments