-
Notifications
You must be signed in to change notification settings - Fork 2
/
digitalClock.ino
424 lines (307 loc) · 9.54 KB
/
digitalClock.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
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
/*
Digital clock watch with Arduino (ATmega 328) and 7 segment Display (MPX6 CA)
Sadraque Nunes
sadaquenunesmartiniano@gmail.com
7 SEGMENT DISPLAY 07/06/2020
A
:::::::::::::::: PIN OUT (Display)
:: ::
F :: :: B IO0 -> SEGMENT A
:: :: IO1 -> SEGMENT B
:: G :: IO2 -> SEGMENT C
:::::::::::::::: IO3 -> SEGMENT D
:: :: IO4 -> SEGMENT E
E :: :: C IO5 -> SEGMENT F
:: :: IO6 -> SEGMENT G
:: :: IO7 -> DISPLAY 1
:::::::::::::::: :: IO8 -> DISPLAY 2
D DP IO9 -> DISPLAY 3
IO10 -> DISPLAY 4
IO11 -> DISPLAY 5
IO12 -> DISPLAY 6
*/
//timers
short int const PROGMEM DISPLAY_TIME = 1;
short int const PROGMEM TIME = 160;
//constants
short int const PROGMEM PIN_A = 0;
short int const PROGMEM PIN_B = 1;
short int const PROGMEM PIN_C = 2;
short int const PROGMEM PIN_D = 3;
short int const PROGMEM PIN_E = 4;
short int const PROGMEM PIN_F = 5;
short int const PROGMEM PIN_G = 6;
short int const PROGMEM PIN_1 = 7;
short int const PROGMEM PIN_2 = 8;
short int const PROGMEM PIN_3 = 9;
short int const PROGMEM PIN_4 = 10;
short int const PROGMEM PIN_5 = 11;
short int const PROGMEM PIN_6 = 12;
short int const PROGMEM PIN_STATUS = 13;
short int const PROGMEM PIN_SET = 14;
short int const PROGMEM PIN_MORE = 15;
short int const PROGMEM PIN_LESS = 16;
short int const PROGMEM PIN_LED_AJUST = 17;
short int const PROGMEM PIN_LED_MIN = 18;
short int const PROGMEM PIN_LED_HOUR = 19;
//clock control
short int seconds = 0;
short int minutes = 0;
short int hours = 0;
boolean ajust = false;
short int ajustCount = 0;
//components control
boolean sts = true; //status leds
short int set = false; //SET
short int more = false; //+
short int less = false; //-
//set the seconds
void setSeconds(){
seconds = seconds + 1;
}
//set the minutes
void setMinutes(){
minutes = minutes + 1;
}
//set the hours
void setHours(){
hours = hours + 1;
}
//increments the minutes
void isMinute(){
if(seconds == 60){
setMinutes();
seconds = 0;
}
}
//increments the hours
void isHour(){
if(minutes == 60){
setHours();
minutes = 0;
}
}
//reset hour (23 to 00)
void resetHour(){
if(hours == 24){
hours = 0;
}
}
//show leds status (the four flashing leds)
void showStatus(boolean state){
if(state == true){
digitalWrite(PIN_STATUS, HIGH);
sts = false;
}
else if(state == false){
digitalWrite(PIN_STATUS, LOW);
sts = true;
}
}
//shows the number on the diplay
void showNumber(int number, int disp){
if(number == 0){
digitalWrite(PIN_A, LOW);//SEGMENT A
digitalWrite(PIN_B, LOW);//SEGMENT B
digitalWrite(PIN_C, LOW);//SEGMENT C
digitalWrite(PIN_D, LOW);//SEGMENT D
digitalWrite(PIN_E, LOW);//SEGMENT E
digitalWrite(PIN_F, LOW);//SEGMENT F
digitalWrite(PIN_G, HIGH);//SEGMENT G
digitalWrite((6 + disp), HIGH);//DISPLAY
delay(DISPLAY_TIME);
digitalWrite((6 + disp), LOW);//DISPLAY
}
if(number == 1){
digitalWrite(PIN_A, HIGH);//SEGMENT A
digitalWrite(PIN_B, LOW);//SEGMENT B
digitalWrite(PIN_C, LOW);//SEGMENT C
digitalWrite(PIN_D, HIGH);//SEGMENT D
digitalWrite(PIN_E, HIGH);//SEGMENT E
digitalWrite(PIN_F, HIGH);//SEGMENT F
digitalWrite(PIN_G, HIGH);//SEGMENT G
digitalWrite((6 + disp), HIGH);//DISPLAY
delay(DISPLAY_TIME);
digitalWrite((6 + disp), LOW);//DISPLAY
}
if(number == 2){
digitalWrite(PIN_A, LOW);//SEGMENT A
digitalWrite(PIN_B, LOW);//SEGMENT B
digitalWrite(PIN_C, HIGH);//SEGMENT C
digitalWrite(PIN_D, LOW);//SEGMENT D
digitalWrite(PIN_E, LOW);//SEGMENT E
digitalWrite(PIN_F, HIGH);//SEGMENT F
digitalWrite(PIN_G, LOW);//SEGMENT G
digitalWrite((6 + disp), HIGH);//DISPLAY
delay(DISPLAY_TIME);
digitalWrite((6 + disp), LOW);//DISPLAY
}
if(number == 3){
digitalWrite(PIN_A, LOW);//SEGMENT A
digitalWrite(PIN_B, LOW);//SEGMENT B
digitalWrite(PIN_C, LOW);//SEGMENT C
digitalWrite(PIN_D, LOW);//SEGMENT D
digitalWrite(PIN_E, HIGH);//SEGMENT E
digitalWrite(PIN_F, HIGH);//SEGMENT F
digitalWrite(PIN_G, LOW);//SEGMENT G
digitalWrite((6 + disp), HIGH);//DISPLAY
delay(DISPLAY_TIME);
digitalWrite((6 + disp), LOW);//DISPLAY
}
if(number == 4){
digitalWrite(PIN_A, HIGH);//SEGMENT A
digitalWrite(PIN_B, LOW);//SEGMENT B
digitalWrite(PIN_C, LOW);//SEGMENT C
digitalWrite(PIN_D, HIGH);//SEGMENT D
digitalWrite(PIN_E, HIGH);//SEGMENT E
digitalWrite(PIN_F, LOW);//SEGMENT F
digitalWrite(PIN_G, LOW);//SEGMENT G
digitalWrite((6 + disp), HIGH);//DISPLAY
delay(DISPLAY_TIME);
digitalWrite((6 + disp), LOW);//DISPLAY
}
if(number == 5){
digitalWrite(PIN_A, LOW);//SEGMENT A
digitalWrite(PIN_B, HIGH);//SEGMENT B
digitalWrite(PIN_C, LOW);//SEGMENT C
digitalWrite(PIN_D, LOW);//SEGMENT D
digitalWrite(PIN_E, HIGH);//SEGMENT E
digitalWrite(PIN_F, LOW);//SEGMENT F
digitalWrite(PIN_G, LOW);//SEGMENT G
digitalWrite((6 + disp), HIGH);//DISPLAY
delay(DISPLAY_TIME);
digitalWrite((6 + disp), LOW);//DISPLAY
}
if(number == 6){
digitalWrite(PIN_A, LOW);//SEGMENT A
digitalWrite(PIN_B, HIGH);//SEGMENT B
digitalWrite(PIN_C, LOW);//SEGMENT C
digitalWrite(PIN_D, LOW);//SEGMENT D
digitalWrite(PIN_E, LOW);//SEGMENT E
digitalWrite(PIN_F, LOW);//SEGMENT F
digitalWrite(PIN_G, LOW);//SEGMENT G
digitalWrite((6 + disp), HIGH);//DISPLAY
delay(DISPLAY_TIME);
digitalWrite((6 + disp), LOW);//DISPLAY
}
if(number == 7){
digitalWrite(PIN_A, LOW);//SEGMENT A
digitalWrite(PIN_B, LOW);//SEGMENT B
digitalWrite(PIN_C, LOW);//SEGMENT C
digitalWrite(PIN_D, HIGH);//SEGMENT D
digitalWrite(PIN_E, HIGH);//SEGMENT E
digitalWrite(PIN_F, HIGH);//SEGMENT F
digitalWrite(PIN_G, HIGH);//SEGMENT G
digitalWrite((6 + disp), HIGH);//DISPLAY
delay(DISPLAY_TIME);
digitalWrite((6 + disp), LOW);//DISPLAY
}
if(number == 8){
digitalWrite(PIN_A, LOW);//SEGMENT A
digitalWrite(PIN_B, LOW);//SEGMENT B
digitalWrite(PIN_C, LOW);//SEGMENT C
digitalWrite(PIN_D, LOW);//SEGMENT D
digitalWrite(PIN_E, LOW);//SEGMENT E
digitalWrite(PIN_F, LOW);//SEGMENT F
digitalWrite(PIN_G, LOW);//SEGMENT G
digitalWrite((6 + disp), HIGH);//DISPLAY
delay(DISPLAY_TIME);
digitalWrite((6 + disp), LOW);//DISPLAY
}
if(number == 9){
digitalWrite(PIN_A, LOW);//SEGMENT A
digitalWrite(PIN_B, LOW);//SEGMENT B
digitalWrite(PIN_C, LOW);//SEGMENT C
digitalWrite(PIN_D, LOW);//SEGMENT D
digitalWrite(PIN_E, HIGH);//SEGMENT E
digitalWrite(PIN_F, LOW);//SEGMENT F
digitalWrite(PIN_G, LOW);//SEGMENT G
digitalWrite((6 + disp), HIGH);//DISPLAY
delay(DISPLAY_TIME);
digitalWrite((6 + disp), LOW);//DISPLAY
}
}
//shows the complete time on the display
void showClock(){
showStatus(sts);
for(int j = 0; j <= TIME; j++){
showNumber((seconds % 10), 6);
showNumber((seconds / 10), 5);
showNumber((minutes % 10), 4);
showNumber((minutes / 10), 3);
showNumber((hours % 10), 2);
showNumber((hours / 10), 1);
}
}
//enable the manual clock config
void buttonSet(){
if(analogRead(PIN_SET) >= 200){
ajustCount = ajustCount + 1;
}
if(ajustCount == 1){
digitalWrite(PIN_LED_AJUST, HIGH);
digitalWrite(PIN_LED_MIN, HIGH);
ajust = true;
}
if(ajustCount == 2){
digitalWrite(PIN_LED_MIN, LOW);
digitalWrite(PIN_LED_HOUR, HIGH);
ajust = true;
}
if(ajustCount == 3){
digitalWrite(PIN_LED_AJUST, LOW);
digitalWrite(PIN_LED_MIN, LOW);
digitalWrite(PIN_LED_HOUR, LOW);
ajustCount = 0;
ajust = false;
}
}
//add one more
void buttonMore(){
if(analogRead(PIN_MORE) >= 200){
if(ajust == true and ajustCount == 1){
setMinutes();
isMinute();
}
if(ajust == true and ajustCount == 2){
setHours();
isHour();
}
}
}
//remove one more
void buttonLess(){
if(analogRead(PIN_LESS) >= 200){
if(ajust == true and ajustCount == 1){
minutes = minutes - 1;
if(minutes == -1){
minutes = 59;
}
}
if(ajust == true and ajustCount == 2){
hours = hours - 1;
if(hours == -1){
hours = 23;
}
}
}
}
void setup() {
for(int i = 0; i<=13; i++){pinMode(i, OUTPUT);}
pinMode(PIN_SET, INPUT);
pinMode(PIN_MORE, INPUT);
pinMode(PIN_LESS, INPUT);
pinMode(PIN_LED_AJUST, OUTPUT);
pinMode(PIN_LED_MIN, OUTPUT);
pinMode(PIN_LED_HOUR, OUTPUT);
}
void loop() {
buttonSet();
buttonLess();
buttonMore();
setSeconds();
isMinute();
isHour();
resetHour();
showClock();
}