12-09-2014, 11:54 PM
What is Arduino?
Arduino is the best Microcontrollers designed for Teachers and Students. It became more popular due to its ease of use and programmability. Please read more about
Components Required
Arduino board
Arduino software IDE
Resistor (10KΩ x 3)
LCD 16×2
Buzzer / UM66 with speaker
Push button x 3
Working of Arduino Project
LCD 16×2 is used to display time and alarm, which is connected to digital pin 2 to 7 of Arduino board.
A variable “alm” is declared as Boolean and it will change state between Clock and Alarm when we press the mode button that is connected in pin 9.
Initially “alm” declared as FALSE, which helps to display time on the LCD screen at first time when the system is ON.
Three push buttons are used here for setting up of time and alarm, one for mode selection, remaining for minute and hour.
If mode button is pressed the variable “alm” become TRUE then program control will display alarm.
When the system is running in TIME MODE, we can set time by using minute and hour button directly without entering to any mode.
The device can be switched to ALARM MODE by pressing the Mode button, then the same buttons (minute and hour) are used to set alarm.
How the alarm logic works? It is simple, while system running in any of the mode it compares Clock time with Alarm time with AM/ PM.
If the clock time matches with the alarm time then LCD will display “RINGING” and it will set pin 11 which is connected to buzzer or any other audio device, here we used UM66 with buzzer.
The duration of Alarm sound is set to 1 minute.
There is no external components are used in the Arduino real time clock alarm, that makes our project more flexible.
You can make this as a deviced timer by simple changes in the program and circuit, the delay time that the duration of alarm can be increase and connect a relay at 9th pin with a relay driving transformer. I’m planning to post it in upcoming Arduino series.
Real Time Clock Arduino Program Code
Here I’m adding the Arduino program code for our project.
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//include lcd library
#include <LiquidCrystal.h>
//lcd(RS, E, D4,D5,D6,D7);
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
const int backlight =13;
const int buttonHr = 12;
const int buttonMin = 8;
const int buttonalm = 9;
const int buzzer = 11;
int secs =0;
int secs2 = 0;
int mins = -1;
int hrs = 12;
int amins = 1;
int ahrs = 12;
int atime = amins+1;
boolean isAM = true;
boolean isAAM = true;
boolean alm = false;
int milliDivSecs = 1000;
int milliDivMins = 60000;
int milliDivHrs = 360000;
unsigned long prevmillis=0;
int interval = 1000;
void setup()
{
pinMode(backlight, OUTPUT);
pinMode(buttonHr, INPUT);
pinMode(buttonMin, INPUT);
pinMode(buttonalm, INPUT);
pinMode(buzzer, OUTPUT);
digitalWrite(backlight, HIGH);
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print(" TIME ");
lcd.setCursor(0,1);
lcd.print(" 12:00:00 AM ");
updateAAMPM();
Serial.begin(9600);
}
void loop()
{
unsigned long currmillis = millis();
if(digitalRead(buttonalm) == HIGH)
{
updatealm();
delay(400);
}
//#----ALARM----#//
if(alm)
{
atime = amins+1;
lcd.setCursor(3,0);
lcd.print("ALARM");
lcd.setCursor(5,1);
lcd.print(":");
lcd.setCursor(8,1);
lcd.print(" ");
updateAmin();
updateAhrs();
AA();
if(digitalRead(buttonHr) == HIGH)
{
delay(25);
ahrs = ahrs+1;
updateAhrs();
//update AMPM on button press
if(ahrs==12)
{
updateAAMPM();
}
delay(400);
}
if(digitalRead(buttonMin) == HIGH)
{
delay(25);
amins = amins + 1;
//Serial.println(mins);
updateAmin();
delay(400);
}
}
//
else
{
lcd.setCursor(3,0);
lcd.print(" TIME");
lcd.setCursor(5,1);
lcd.print(":");
lcd.setCursor(8,1);
lcd.print(":");
updateHrs();
updateMin();
TT();
//detect button pressing
if(digitalRead(buttonHr) == HIGH)
{
delay(25);
hrs = hrs+1;
updateHrs();
//update AMPM on button press
if(hrs==12)
{
updateAMPM();
}
delay(400);
}
if(digitalRead(buttonMin) == HIGH)
{
delay(25);
mins = mins + 1;
updateMin();
delay(400);
}
}
if(currmillis-prevmillis > 999)
{
prevmillis =currmillis;
if(secs<10)
{
lcd.setCursor(10,1);
lcd.print(secs);
lcd.setCursor(9,1);
lcd.print(0);
}
else
{
lcd.setCursor(9,1);
lcd.print(secs);
}
if(secs == 0)
{
mins = mins+1;
updateMin();
}
//get new seconds from system time
secs = (millis()/milliDivSecs)%60; // divide by 1000 and mod by 60 gives seconds from milliseconds
}
if(mins == amins && hrs == ahrs && isAAM == isAM)
{
lcd.setCursor(8,0);
lcd.print(" RINGING");
digitalWrite(buzzer, HIGH);
}
if(mins == atime)
{
lcd.setCursor(8,0);
lcd.print(" ");
digitalWrite(buzzer,LOW);
}
}
void updateMin(){
if(mins > 59){
hrs = hrs+1;
updateHrs(); //update hours then
if(hrs==11 && mins >59){
updateAMPM();
}
mins = 0; //reset mins
lcd.setCursor(6,1);
lcd.print("00");
}
if(mins < 10)
{
lcd.setCursor(7,1);
}
else{
lcd.setCursor(6,1);
}
lcd.print(mins);
}
void updateHrs(){
if(hrs> 12)
{
hrs = 1;
}
if(hrs< 10){
lcd.setCursor(3,1);
lcd.print(" ");
lcd.setCursor(4,1);
}
else{
lcd.setCursor(3,1);
}
lcd.print(hrs);
}
void updateAMPM()
{
if(isAM)
{
isAM = false;
lcd.setCursor(12,1);
lcd.print("PM");
}
else
{
isAM = true;
lcd.setCursor(12,1);
lcd.print("AM");
}
}
void updateAmin()
{
if(amins > 59)
{
ahrs = ahrs+1;
updateAhrs(); //update hours then
if(ahrs==11 && amins >59)
{
updateAAMPM();
}
amins = 0; //reset mins
lcd.setCursor(6,1);
lcd.print("00");
}
if(amins < 10)
{
lcd.setCursor(7,1);
}
else
{
lcd.setCursor(6,1);
}
lcd.print(amins);
}
//update hour function
void updateAhrs()
{
//display hours - needs fixing for am pm
if(ahrs> 12){
//reset to 1
ahrs = 1;
}
if(ahrs< 10){
lcd.setCursor(3,1);
lcd.print(" ");
lcd.setCursor(4,1);
}
else{
lcd.setCursor(3,1);
}
lcd.print(ahrs);
}
void updateAAMPM()
{
if(isAAM)
{
isAAM = false;
lcd.setCursor(9,1);
lcd.print("PM ");
}
else
{
isAAM = true;
lcd.setCursor(9,1);
lcd.print("AM ");
}
}
void updatealm()
{
if(alm)
{
alm = false;
}
else
{
alm = true;
}
}
void AA()
{
if(isAAM)
{
lcd.setCursor(9,1);
lcd.print("AM ");
}
else
{
lcd.setCursor(9,1);
lcd.print("PM ");
}
}
void TT()
{
if(isAM)
{
lcd.setCursor(12,1);
lcd.print("AM");
}
else
{
lcd.setCursor(12,1);
lcd.print("PM");
}
}