-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.arduino
477 lines (417 loc) · 9.95 KB
/
project.arduino
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//Elmas Başak Gören
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//first I set these false. It will be changed by some condition.
bool ClockSet = false;
bool ChooseClockPart = false;
//create new variables fot the alarm
int hoursForAlarm;
int minutesForAlarm;
//these pins were created
const byte pin0 = 0; // it is for the clock part change. 12 and 24.
const byte pin1 = 1; //it is for the alarm pm am mode
const byte pin10 = 10; // it is for the alrm on/off
//I decided the clock start with 12.00.00 and we can see the differences between 12 and 24 clock part.
int hr = 12, min = 00, sec = 00;
int newHours = 0; // I set the new hours 0. So it is initialized.
bool TwentyFourHours = true; //Now it is true and when it 12 hours starts it must be false.
//I will use it the print the actual AM PM text.
String PrintAmPm = "";
//These part is for the teaching the program how to be the clock.
ISR(TIMER1_COMPA_vect)
{
if(sec < 59)// The basic clock rules. It not going to be 60.
{
sec++; //will continue.
return;
}
if(min < 59)
{
// The basic clock rules. It not going to be 60.
sec = 0; ; //new start.
min++;//will continue.
return;
}
if(hr < 23)
{
// The basic clock rules. It not going to be 24 any time.
sec = 0; //new start.
min = 0; //new start.
hr++; //will continue.
return;
}
//set them 0.
sec = 0;
min = 0;
hr = 0;
}
//Timer Settings
//Actually Timer has a clock inherently but it not the same as our clock time.
void TimerSettings(){
cli(); //cli clears the global interrupt flag to prevent any form of interrupt occurring.
TCCR1A = 0;// set for initiliazing
TCCR1B = 0;// set for initiliazing
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 1hz increments
OCR1A = 15624;// OCR1A have to be lower than 65536.
TCCR1B |= (1 << WGM12);
// Setting the needed bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
TIMSK1 |= (1 << OCIE1A); //need to compare
sei(); //sei sets the bit and switches interrupts on.
}
//This function purpose set the clock am pm time
int turnTwentyFourHour()
{
//set the new hours 's'
int s = hr;
//print AM part
PrintAmPm = "AM";
//clockset is seted FALSE
ClockSet = false;
if( s > 11)//so hours bigger then 11 is PM now.
{
s = hr - 12;
PrintAmPm = "PM";
//process'll continue so we need to set it true.
ClockSet=true;
}
//process is done after that, so set it false.
if(s == 0){
s = 12;
ClockSet=false;
}
//end up function.
return s;
}
//Actual show 24 clock time function.
void Show_Time24()
{
//new variable for hours.
int newHours = hr;
//if the twentyFourHours True it means 24 mode on.
if(TwentyFourHours = true)
{
//so new hours will be 24 mode.
newHours = turnTwentyFourHour();
}
//some edit for printing. if it is lower tan 10 so 0 will add it the led.
if(newHours < 10)
{
lcd.setCursor(0,0);lcd.print("0");
//and print hours.
lcd.setCursor(1,0);lcd.print(newHours);
}
else {
lcd.setCursor(0,0);lcd.print(newHours);
}
if(min < 10)//some operation for minutes.
{
lcd.setCursor(3,0);
//print 0 to the led.
lcd.print("0"); //print 0.
lcd.setCursor(4,0);
//and print minutes.
lcd.print(min);
}
else
{
lcd.setCursor(3,0);
lcd.print(min);
}
if(sec < 10)//if sec lower than 10, it will print 0 on the lcd..
{
lcd.setCursor(6,0);
lcd.print("0"); //print 0.
lcd.setCursor(7,0);
lcd.print(sec);
}
else
{
lcd.setCursor(6,0);
lcd.print(sec);
}
lcd.setCursor(2,0);
lcd.print(":");
lcd.setCursor(5,0);
lcd.print(":");
lcd.setCursor(8,0);
lcd.print("");
}
void Show_Time12()//Function for show 12 hours clock.
{
int newHours = hr; //new variable for hours.
if(TwentyFourHours != true)//when 24 not true it will be happen
{
newHours = turnTwentyFourHour();
//we need some hours mode operation and so on.
newHours = newHours % 12;
}
if(newHours < 10 && newHours >= 1 )
{
//same operations for hours
lcd.setCursor(0,0);
lcd.print("0");
lcd.setCursor(1,0);
lcd.print(newHours);
}
else if(newHours < 1){
lcd.setCursor(0, 0);
//It will never be 00 so print 12.
lcd.print("12");
}
else
{
//print on the lcd.
lcd.setCursor(0,0);
lcd.print(newHours);
}
if(min < 10)
{
//same operations for minutes. It will 0 when it lower than 10.
lcd.setCursor(3,0);
lcd.print("0");
lcd.setCursor(4,0);
lcd.print(min);
}
else {
lcd.setCursor(3,0);
lcd.print(min);
}
if(sec < 10){
lcd.setCursor(6,0);
lcd.print("0");
lcd.setCursor(7,0);
lcd.print(sec);
} else {
lcd.setCursor(6,0);
lcd.print(sec);
}
//some lcd print operations
lcd.setCursor(2,0);
lcd.print(":");
lcd.setCursor(5,0);
lcd.print(":");
lcd.setCursor(8,0);
lcd.print("");
lcd.setCursor(12,1);
//So 12 clock time part need to print AM PM time.
lcd.print(PrintAmPm);
}
//This function actually get the alarm.
void takeAlarm() {
if (minutesForAlarm == min)
{ //operation checks minutes.
if (hoursForAlarm == newHours)
{ //operation checks hours.
if (digitalRead(pin0) == HIGH)
{
//when It meets all needs clear lcd and print.
lcd.clear();
lcd.setCursor(5, 0);
//alarm activated so print alarm.
lcd.print("ALARM!");
//we need piezo for the alarm. so I activated in there.
tone(7,200,100);
//end up the operation.
delay(100);
}
else {
//Same operation different clock part.
if (ChooseClockPart == ClockSet)
{//clear lcd and print data.
lcd.clear();
lcd.setCursor(5, 0);
//print alarm text.
lcd.print("ALARM!");
//we need piezo for the alarm. so I activated in there.
tone(7,200,100);
delay(100);
}
else {
}
}
}
else {}
}
else {
}
}
//It gets data from pin and activates alarm.
void ShowAlarm() {
if (digitalRead(pin0) == HIGH)
//means it is pushed.
{
if (digitalRead(A3) == HIGH)//means it is time to start
{
hoursForAlarm++;
//for 24 clock part
hoursForAlarm = hoursForAlarm % 24;
}
else {
}
}
else {
if (digitalRead(A3) == HIGH) //means it is time to start
{
hoursForAlarm++;
//for 12 clock part
hoursForAlarm = hoursForAlarm % 12;
}
else {
}
}
if (hoursForAlarm < 10) {
lcd.setCursor(0, 1);
lcd.print("0");
lcd.print(hoursForAlarm);
}
else {
lcd.setCursor(0, 1);
lcd.print(hoursForAlarm);
}
if (digitalRead(A4) == HIGH) {
minutesForAlarm++;
minutesForAlarm = minutesForAlarm % 60;
}
else {
}
//operations for minutes
if (minutesForAlarm < 10) {
lcd.setCursor(2, 1);
lcd.print(":");
lcd.print("0");
lcd.print(minutesForAlarm);
}
else {
lcd.setCursor(2, 1);
lcd.print(":");
//print minutes for alarm
lcd.print(minutesForAlarm);
}
if (digitalRead(pin0) == HIGH)
//means it is activated.
{
}
else
{
if (digitalRead(pin1) == HIGH)//means it is activated
{
lcd.setCursor(5, 1);
lcd.print(":");
lcd.print("PM");
//knows it is Pm side and set it false.
ChooseClockPart = false;
}
else//same cursor as PM
{
lcd.setCursor(5, 1);
lcd.print(":");
lcd.print("AM");
//knows it is Am sie and set it true.
ChooseClockPart = true;
}
}
if (digitalRead(A2) == HIGH)//this will snooze alarm by 5 minutes (as requareted.)
{
//postponed by 5 minutes.
minutesForAlarm = minutesForAlarm + 5;
if (minutesForAlarm >= 60) //but it never pass 60 so, we restrict.
{
hoursForAlarm++;
minutesForAlarm = minutesForAlarm % 60;
}
else {
}
} else {
}
//get alarm from it.
takeAlarm();
}
#define pinTemp A0
//I will recieve it from A0 pin so I have to define it. It will use for the computing temprature.
void setup()
{
// in the set up all input and output are described.
Serial.begin(9600);
TimerSettings(); //essential setting for timer must be define at the set up side.
//All pinmodes are below.
pinMode(A0, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
pinMode(7, OUTPUT);
pinMode( pinTemp, INPUT ); //special.
lcd.begin(16, 2);
lcd.setCursor(0,0);
}
void ForCelcius()//This function is for computing celcius
{
//the formul for computing celcius. pintemp is a value from the circuit.
float temp = ((analogRead(pinTemp) * (5.0/1024))-0.5 )/0.01;
lcd.setCursor(10, 0);
lcd.print(" ");
//right side.
lcd.setCursor(10, 0);
lcd.print( temp );
lcd.print("C");//celcius symbol.
}
void ForFahrenheit()//This function is for computing fahrenheit
{
//It actually use celcius math and then * 9/5 +32
float temp = (((analogRead(pinTemp) * (5.0/1024))-0.5 )/0.01) * 9/5 +32;
//print it on the lcd.
lcd.setCursor(10, 0);
lcd.print(" ");
//right side.
lcd.setCursor(10, 0);
lcd.print( temp );
lcd.print("F"); //fahrenheit symbol.
}
void loop() {
//need to describe
byte number = digitalRead(pin0);//it will read the pin0 for 12 and 24 hours clock change.
//and print it.
Serial.println(number);
//it will read pin10 for on/off the alarm.
byte SetA = digitalRead(pin10);
//and print it.
Serial.println(SetA);
if (number == HIGH)
{
//clear the lcd
lcd.clear();
//print 24 hours clock.
Show_Time24();
// it check alarm is opened.
if (SetA == HIGH)
{
//condition is okay, so show alarm.
ShowAlarm();
}
//else dont do anything.
else {}
}
else
{
TwentyFourHours = false; //set it false now.
lcd.clear();
//clear the lcd and print 12 hours clock part.
Show_Time12();
if (SetA == HIGH)
{
//condition is okay, so show alarm.
ShowAlarm();
}
//else dont do anything.
else {}
}
//button for temprature.
if (digitalRead(A5) == HIGH) { //button for the print celcius value.
ForCelcius(); //the function is above.
}
else{//otherwise it will print fahrenheit value.
ForFahrenheit(); //the function is above.
}
delay ( 1000 );
}