1
1
/*
2
2
Test RTC
3
-
4
- A test sketch showcasing all RTC showcasing various functionalities related to the RTC module,
3
+
4
+ A test sketch showcasing all RTC showcasing various functionalities related to the RTC module,
5
5
including setting the time, handling interrupts, and reading time values.
6
6
7
7
Find the full UNO R4 WiFi RTC documentation here:
@@ -19,15 +19,15 @@ void periodic_cbk() {
19
19
static bool clb_st = false ;
20
20
21
21
// Toggle the LED based on callback state
22
- if (clb_st) {
23
- digitalWrite (LED_ON_INTERRUPT,HIGH);
22
+ if (clb_st) {
23
+ digitalWrite (LED_ON_INTERRUPT, HIGH);
24
24
}
25
25
else {
26
- digitalWrite (LED_ON_INTERRUPT,LOW);
26
+ digitalWrite (LED_ON_INTERRUPT, LOW);
27
27
}
28
28
29
29
clb_st = !clb_st; // Toggle callback state
30
-
30
+
31
31
// Print message indicating periodic interrupt
32
32
Serial.println (" PERIODIC INTERRUPT" );
33
33
}
@@ -41,23 +41,30 @@ void setup() {
41
41
// Initialize serial communication
42
42
Serial.begin (9600 );
43
43
// Wait for serial connection
44
- while (!Serial) {
45
-
44
+ while (!Serial) {
45
+
46
46
}
47
-
47
+
48
48
// Set LED pins as outputs
49
49
pinMode (LED_BUILTIN, OUTPUT);
50
50
pinMode (LED_ON_INTERRUPT, OUTPUT);
51
-
51
+
52
52
// Initialize the RTC
53
53
RTC.begin ();
54
54
55
55
// Set a specific initial time (August 25, 2022, 14:37:00 Thursday)
56
56
RTCTime mytime (25 , Month::AUGUST, 2022 , 14 , 37 , 00 , DayOfWeek::THURSDAY, SaveLight::SAVING_TIME_ACTIVE);
57
57
58
+ RTCTime savedTime;
59
+ RTC.getTime (savedTime);
60
+
58
61
// Set the initial time if RTC is not running
59
- if (!RTC.isRunning ()) {
60
- RTC.setTime (mytime);
62
+ if (!RTC.isRunning ()) {
63
+ if (savedTime.getYear () != 2000 ) {
64
+ RTC.setTime (mytime);
65
+ } else {
66
+ RTC.setTime (savedTime);
67
+ }
61
68
}
62
69
63
70
// Create an alarm time set to 35 seconds
@@ -69,36 +76,35 @@ void setup() {
69
76
am.addMatchSecond ();
70
77
71
78
// Set the periodic callback function to run once every 2 seconds
72
- if (!RTC.setPeriodicCallback (periodic_cbk, Period::ONCE_EVERY_2_SEC)) {
79
+ if (!RTC.setPeriodicCallback (periodic_cbk, Period::ONCE_EVERY_2_SEC)) {
73
80
Serial.println (" ERROR: periodic callback not set" );
74
81
}
75
-
82
+
76
83
// Set the alarm callback function with the alarm time and matching condition
77
- if (!RTC.setAlarmCallback (alarm_cbk, alarmtime, am)) {
84
+ if (!RTC.setAlarmCallback (alarm_cbk, alarmtime, am)) {
78
85
Serial.println (" ERROR: alarm callback not set" );
79
86
}
80
-
81
87
}
82
88
83
89
void loop () {
84
90
static bool status = false ;
85
-
91
+
86
92
RTCTime currenttime;
87
-
93
+
88
94
// Check if RTC is running and print status
89
- if (status) {
95
+ if (status) {
90
96
91
97
// Toggle LED and display RTC status if 'status' is true
92
- if (RTC.isRunning ()) {
98
+ if (RTC.isRunning ()) {
93
99
Serial.println (" RTC is running" );
94
100
}
95
101
else {
96
102
Serial.println (" RTC is not running" );
97
103
}
98
-
104
+
99
105
/* GET CURRENT TIME FROM RTC */
100
106
RTC.getTime (currenttime);
101
-
107
+
102
108
/* PRINT CURRENT TIME on Serial */
103
109
Serial.print (" Current time: " );
104
110
/* DATE */
@@ -108,20 +114,20 @@ void loop() {
108
114
Serial.print (" /" );
109
115
Serial.print (currenttime.getYear ());
110
116
Serial.print (" - " );
111
-
117
+
112
118
/* ORE:MINUTI:SECONDI */
113
119
Serial.print (currenttime.getHour ());
114
120
Serial.print (" :" );
115
121
Serial.print (currenttime.getMinutes ());
116
122
Serial.print (" :" );
117
123
Serial.println (currenttime.getSeconds ());
118
-
119
- digitalWrite (LED_BUILTIN, HIGH);
124
+
125
+ digitalWrite (LED_BUILTIN, HIGH);
120
126
}
121
127
else {
122
- digitalWrite (LED_BUILTIN, LOW);
128
+ digitalWrite (LED_BUILTIN, LOW);
123
129
}
124
130
125
131
status = !status;
126
132
delay (1000 );
127
- }
133
+ }
0 commit comments