-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalarm_project.ino
178 lines (144 loc) · 3.6 KB
/
alarm_project.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <RTClib.h>
#include <DS3232RTC.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
RTC_DS3231 rtc;
int clk_reset_button = 8;
int alarm_reset_button = 9;
int increment_button = 10;
int decrement_button = 11;
int confirm_alarm_button = 3;
int alarm_mode_button = 4;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(alarm_reset_button, INPUT);
pinMode(clk_reset_button, INPUT);
pinMode(increment_button, INPUT);
pinMode(decrement_button, INPUT);
pinMode(confirm_alarm_button, INPUT);
pinMode(alarm_mode_button, INPUT);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, setting the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
DateTime currTime = rtc.now();
lcd.setCursor(0, 0);
lcd.print("Time: ");
regular_clock_mode(currTime);
if (digitalRead(clk_reset_button) == 1) {
delay(1000);
reset_normal_clock();
}
else if (digitalRead(alarm_mode_button) == 1) {
delay(2000);
lcd.setCursor(0, 0);
lcd.print("Set alarm: ");
set_alarm();
}
}
//if reset is high, set back the alarm to 00:00
//if set_alarm is high, set clock to 00:00:00 and then make use of increment and decrement operations
//if alarm_start is high, return to regular clock and countdown
void reset_normal_clock() {
lcd.clear();
DateTime currTime = rtc.now();
rtc.adjust(DateTime(currTime.year(), currTime.month(), currTime.day(), currTime.hour(), currTime.minute(), currTime.second()));
}
void set_alarm() {
DateTime currTime = rtc.now();
lcd.clear();
lcd.setCursor(1, 2);
bool flag = true;
int hrs = 0, mins = 0;
while (flag == true) {
// Update the display only when hours or minutes change
lcd.setCursor(1, 2);
lcd.print("Set Alarm: ");
if (hrs < 10) {
lcd.print("0");
}
lcd.print(hrs);
lcd.print(" : ");
if (mins < 10) {
lcd.print("0");
}
lcd.print(mins);
// Increment minutes
if (digitalRead(increment_button) == HIGH) {
mins++;
delay(200); // debounce
}
// Decrement minutes
if (digitalRead(decrement_button) == HIGH) {
mins--;
delay(200); // debounce
}
// Handle rollover for minutes and hours
if (mins == 60) {
mins = 0;
hrs++;
} else if (mins < 0) {
mins = 59;
hrs--;
}
// Handle rollover for hours
if (hrs == 24) {
hrs = 0;
} else if (hrs < 0) {
hrs = 23;
}
// Confirm the alarm setting
if (digitalRead(confirm_alarm_button) == HIGH) {
flag = false;
}
}
// Reset alarm time if the reset button is pressed
if (digitalRead(alarm_reset_button) == HIGH) {
hrs = 0;
mins = 0;
}
// Display the set alarm
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Alarm set to ");
if (hrs < 10) {
lcd.print("0");
}
lcd.print(hrs);
lcd.print(" : ");
if (mins < 10) {
lcd.print("0");
}
lcd.print(mins);
// Adjust the alarm time on the RTC module
rtc.setAlarm1(DateTime(currTime.year(), currTime.month(), currTime.day(), hrs, mins, 0), DS3231_A1_Hour);
}
void regular_clock_mode(DateTime currTime) {
lcd.setCursor(2, 1);
currTime = rtc.now();
int hrs = currTime.hour(), mins = currTime.minute(), secs = currTime.second();
if (hrs < 10) {
lcd.print("0");
}
lcd.print(hrs);
lcd.print(" : ");
if (mins < 10) {
lcd.print("0");
}
lcd.print(mins);
lcd.print(" : ");
if (secs < 10) {
lcd.print("0");
}
lcd.print(secs);
delay(1000);
}