11/*
22 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,
55 including setting the time, handling interrupts, and reading time values.
66
77 Find the full UNO R4 WiFi RTC documentation here:
@@ -19,15 +19,15 @@ void periodic_cbk() {
1919 static bool clb_st = false ;
2020
2121 // 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);
2424 }
2525 else {
26- digitalWrite (LED_ON_INTERRUPT,LOW);
26+ digitalWrite (LED_ON_INTERRUPT, LOW);
2727 }
2828
2929 clb_st = !clb_st; // Toggle callback state
30-
30+
3131 // Print message indicating periodic interrupt
3232 Serial.println (" PERIODIC INTERRUPT" );
3333}
@@ -41,23 +41,30 @@ void setup() {
4141 // Initialize serial communication
4242 Serial.begin (9600 );
4343 // Wait for serial connection
44- while (!Serial) {
45-
44+ while (!Serial) {
45+
4646 }
47-
47+
4848 // Set LED pins as outputs
4949 pinMode (LED_BUILTIN, OUTPUT);
5050 pinMode (LED_ON_INTERRUPT, OUTPUT);
51-
51+
5252 // Initialize the RTC
5353 RTC.begin ();
5454
5555 // Set a specific initial time (August 25, 2022, 14:37:00 Thursday)
5656 RTCTime mytime (25 , Month::AUGUST, 2022 , 14 , 37 , 00 , DayOfWeek::THURSDAY, SaveLight::SAVING_TIME_ACTIVE);
5757
58+ RTCTime savedTime;
59+ RTC.getTime (savedTime);
60+
5861 // 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+ }
6168 }
6269
6370 // Create an alarm time set to 35 seconds
@@ -69,36 +76,35 @@ void setup() {
6976 am.addMatchSecond ();
7077
7178 // 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)) {
7380 Serial.println (" ERROR: periodic callback not set" );
7481 }
75-
82+
7683 // 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)) {
7885 Serial.println (" ERROR: alarm callback not set" );
7986 }
80-
8187}
8288
8389void loop () {
8490 static bool status = false ;
85-
91+
8692 RTCTime currenttime;
87-
93+
8894 // Check if RTC is running and print status
89- if (status) {
95+ if (status) {
9096
9197 // Toggle LED and display RTC status if 'status' is true
92- if (RTC.isRunning ()) {
98+ if (RTC.isRunning ()) {
9399 Serial.println (" RTC is running" );
94100 }
95101 else {
96102 Serial.println (" RTC is not running" );
97103 }
98-
104+
99105 /* GET CURRENT TIME FROM RTC */
100106 RTC.getTime (currenttime);
101-
107+
102108 /* PRINT CURRENT TIME on Serial */
103109 Serial.print (" Current time: " );
104110 /* DATE */
@@ -108,20 +114,20 @@ void loop() {
108114 Serial.print (" /" );
109115 Serial.print (currenttime.getYear ());
110116 Serial.print (" - " );
111-
117+
112118 /* ORE:MINUTI:SECONDI */
113119 Serial.print (currenttime.getHour ());
114120 Serial.print (" :" );
115121 Serial.print (currenttime.getMinutes ());
116122 Serial.print (" :" );
117123 Serial.println (currenttime.getSeconds ());
118-
119- digitalWrite (LED_BUILTIN, HIGH);
124+
125+ digitalWrite (LED_BUILTIN, HIGH);
120126 }
121127 else {
122- digitalWrite (LED_BUILTIN, LOW);
128+ digitalWrite (LED_BUILTIN, LOW);
123129 }
124130
125131 status = !status;
126132 delay (1000 );
127- }
133+ }
0 commit comments