-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.ino
314 lines (274 loc) · 6.1 KB
/
clock.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
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
/***************************************
* Project : Digital Clock with Arduino Uni Board
* Date : 29 - 6 - 2016
* Programming Language : C
* Kits : LCD 16x2 , Potentiometer ,Bluetooth
* Programmer : Mohamed Nagy Mostafa
***************************************/
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
LiquidCrystal lcd(2,3,4,5,6,7); //RS E D4 D5 D6 D7
SoftwareSerial HC05(8,9); //TX RX
#define HOURS 24
#define SECONDS 60
#define MINUTES 60
#define NUMBER_OF_ITEMS 3
#define NON_FOCUS -1
#define HALF_SECOND 500
/// Buttons
#define LEFT_BUTTON 76
#define RIGHT_BUTTON 82
#define UP_BUTTON 70
#define DOWN_BUTTON 66
short hour,mint,sec;
short index = NON_FOCUS; //Focusing item's index
short command; //The Button that is clicked
bool focusAvaliable = false;
void display();
// To show clock on the screen
void focusToRight(short &index);
// To move The focus to right */
void focusToLeft(short &index);
// To move The focus to left */
void increasing(short &index,short *items[]);
// To increase the value of The selected item
void decreasing(short &index,short *items[]);
// To decrease the value of The selected item
void focusing(short index,short *items[]);
// To show The focus in the screen
void check(short command,short *item[]);
// To see the choice of the user
void checkZero(short number);
// To add zero in the left side of number of hr/min/sec
// When the number is lower than 10
void checkNight(short hour);
// To control of showing Pm/Am
void setup()
{
/// LCD Settings
lcd.begin(16,2);
lcd.clear();
/// Bluetooth Settings
HC05.begin(9600);
/// Testing Settings
Serial.begin(9600);
}
void loop()
{
for(hour = 0 ; hour != HOURS ; hour++)
for(mint = 0 ; mint != MINUTES ; mint++)
for(sec = 0 ; sec != SECONDS ; sec++)
{
short *item[NUMBER_OF_ITEMS] = {&hour,&mint,&sec};
command = HC05.read();
check(command ,item);
lcd.clear();
display();
delay(HALF_SECOND);
focusing(index,item);
delay(HALF_SECOND);
display();
delete item[NUMBER_OF_ITEMS];
}
}
void display()
{
checkZero(hour);
lcd.print(":");
checkZero(mint);
lcd.print(":");
checkZero(sec);
checkNight(hour);
}
// @params command : number of button that clicked by user
// item[] pointer of array that have the current hr/min/sec
void check(short command, short *item[])
{
switch(command)
{
case RIGHT_BUTTON:
focusToRight(index);
break;
case LEFT_BUTTON:
focusToLeft(index);
break;
case UP_BUTTON:
increasing(index,item);
break;
case DOWN_BUTTON:
decreasing(index,item);
break;
default:
return;
}
}
void focusToRight(short &index)
{
if(index < NUMBER_OF_ITEMS - 1)
{
index++;
focusAvaliable= true;
}
else if(index == NUMBER_OF_ITEMS - 1)
{
focusAvaliable= false;
index = NON_FOCUS;
}
else
{
index = 0;
focusAvaliable= true;
}
}
void focusToLeft(short &index)
{
if(index > 0 )
{
index--;
focusAvaliable= true;
}
else if(index == 0)
{
index = NON_FOCUS;
focusAvaliable= false;
}
else
{
index = NUMBER_OF_ITEMS - 1;
focusAvaliable= true;
}
}
void increasing(short &index,short *items[])
{
// Testing ...
Serial.print("Increasing");
Serial.println(index);
if(index == 0)
{
if(*items[index] < HOURS - 1)
{
++*items[index];
}
else
{
*items[index] = 0;
}
}
if(index == 1)
{
if(*items[index] < MINUTES - 1)
{
++*items[index];
}
else
{
*items[index] = 0;
}
}
if(index == 2)
{
if(*items[index] < SECONDS - 1)
{
++*items[index];
}
else
{
*items[index] = 0;
}
}
}
void decreasing(short &index,short *items[])
{
Serial.print("Decreasing");
Serial.println(index);
if(index == 0)
{
if(*items[index] < HOURS && *items[index] > 0)
{
--*items[index];
}
else
{
*items[index] = HOURS - 1;
}
}
if(index == 1)
{
if(*items[index] < MINUTES && *items[index] > 0)
{
--*items[index];
}
else
{
*items[index] = MINUTES - 1;
}
}
if(index == 2)
{
if(*items[index] < SECONDS && *items[index] > 0)
{
--*items[index];
}
else
{
*items[index] = SECONDS - 1;
}
}
}
void focusing(short index,short *items[])
{
if(index == 0 && focusAvaliable == true)
{
lcd.clear();
lcd.setCursor(2,0);
lcd.print(":");
checkZero(mint);
lcd.print(":");
checkZero(sec);
}
else if(index == 1 && focusAvaliable == true)
{
lcd.clear();
checkZero(hour);
lcd.print(":");
lcd.setCursor(5,0);
lcd.print(":");
checkZero(sec);
}
else if(index == 2 && focusAvaliable == true)
{
lcd.clear();
checkZero(hour);
lcd.print(":");
checkZero(mint);
lcd.print(":");
}
else
{
lcd.clear();
checkZero(hour);
lcd.print(":");
checkZero(mint);
lcd.print(":");
checkZero(sec);
}
checkNight(hour);
}
void checkZero(short number)
{
if( number < 10)
{
lcd.print(0);
lcd.print(number);
}
else
lcd.print(number);
}
void checkNight(short hour)
{
lcd.setCursor(9,0);
if(hour < 12)
lcd.print("Am");
else
lcd.print("Pm");
}