forked from arduino/ArduinoCore-renesas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest_RTC.ino
150 lines (115 loc) · 3.43 KB
/
Test_RTC.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
Test RTC
A test sketch showcasing all RTC showcasing various functionalities related to the RTC module,
including setting the time, handling interrupts, and reading time values.
Find the full UNO R4 WiFi RTC documentation here:
https://docs.arduino.cc/tutorials/uno-r4-wifi/rtc
*/
// Include the RTC library
#include "RTC.h"
// 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() {
periodicFlag = true;
}
// Callback function for alarm interrupt
void alarm_cbk() {
alarmFlag = true;
}
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Wait for serial connection
while (!Serial) {
}
// Set LED pins as outputs
pinMode(LED_BUILTIN, OUTPUT);
pinMode(LED_ON_INTERRUPT, OUTPUT);
// Initialize the RTC
RTC.begin();
// Set a specific initial time (August 25, 2022, 14:37:00 Thursday)
RTCTime mytime(25, Month::AUGUST, 2022, 14, 37, 00, DayOfWeek::THURSDAY, SaveLight::SAVING_TIME_ACTIVE);
RTCTime savedTime;
RTC.getTime(savedTime);
// Set the initial time if RTC is not running
if (!RTC.isRunning()) {
if (savedTime.getYear() != 2000) {
RTC.setTime(mytime);
} else {
RTC.setTime(savedTime);
}
}
// Create an alarm time set to 35 seconds
RTCTime alarmtime;
alarmtime.setSecond(35);
// Create an AlarmMatch object to match seconds
AlarmMatch am;
am.addMatchSecond();
// Set the periodic callback function to run once every 2 seconds
if (!RTC.setPeriodicCallback(periodic_cbk, Period::ONCE_EVERY_2_SEC)) {
Serial.println("ERROR: periodic callback not set");
}
// Set the alarm callback function with the alarm time and matching condition
if (!RTC.setAlarmCallback(alarm_cbk, alarmtime, am)) {
Serial.println("ERROR: alarm callback not set");
}
}
void loop() {
static bool status = false;
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) {
// Toggle LED and display RTC status if 'status' is true
if (RTC.isRunning()) {
Serial.println("RTC is running");
}
else {
Serial.println("RTC is not running");
}
/* GET CURRENT TIME FROM RTC */
RTC.getTime(currenttime);
/* PRINT CURRENT TIME on Serial */
Serial.print("Current time: ");
/* DATE */
Serial.print(currenttime.getDayOfMonth());
Serial.print("/");
Serial.print(Month2int(currenttime.getMonth()));
Serial.print("/");
Serial.print(currenttime.getYear());
Serial.print(" - ");
/* ORE:MINUTI:SECONDI */
Serial.print(currenttime.getHour());
Serial.print(":");
Serial.print(currenttime.getMinutes());
Serial.print(":");
Serial.println(currenttime.getSeconds());
digitalWrite(LED_BUILTIN, HIGH);
}
else {
digitalWrite(LED_BUILTIN, LOW);
}
status = !status;
delay(1000);
}