-
Notifications
You must be signed in to change notification settings - Fork 3
/
clock.ino
493 lines (442 loc) · 7.99 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
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#include "Wire.h"
#include "math.h"
#include "RTClib.h"
#include "LPD8806.h"
#include "SPI.h" // lpd8806 dependency
// RTC SCL (yellow): Analog in 5
// RTC SDA (blue): Analog in 4
int ledDataPin = 6; // LED DI (blue), digital
int ledClockPin = 7; // LED CI (yellow), digital
int lightPin = 1; // Photoresistor, Analog
int buttonPin = 4; // Push button, digital
DateTime now;
int intensity = 100;
int min_intensity = 10;
int max_intensity = intensity;
int nLEDs = 118;
int reset = 1;
RTC_DS1307 rtc;
LPD8806 leds = LPD8806(nLEDs, ledDataPin, ledClockPin);
void (*words_hour[24])();
void (*words_minute[12])();
void setup() {
Serial.begin(57600);
pinMode(buttonPin, INPUT);
init_leds();
init_rtc();
check_reset();
setup_words();
test_all();
}
void loop() {
now = rtc.now();
adapt_intensity();
reset_leds();
if(digitalRead(buttonPin) == HIGH){
test_words();
}else{
display_time();
}
delay(1000);
}
void print_time(int hour, int minute, int second){
Serial.print(hour, DEC);
Serial.print(':');
Serial.print(minute, DEC);
Serial.print(':');
Serial.print(second, DEC);
Serial.println();
}
void init_leds(){
leds.begin();
leds.show();
}
void init_rtc(){
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
rtc.begin();
if (!rtc.isrunning()) {
// Time is not set. Upload sketch, hold button to set the time.
error();
Serial.println("RTC is NOT running!");
}
}
void error(){
leds.setPixelColor(0, 255, 0, 0);
leds.show();
}
void check_reset(){
if(reset == 1 && digitalRead(buttonPin) == HIGH){
Serial.println(F("Resetting time to..."));
Serial.println(__TIME__);
Serial.println(__DATE__);
rtc.adjust(DateTime(__DATE__, __TIME__)); // Reset time to the compilation time
}
reset = 0;
}
void adapt_intensity(){
intensity = map(analogRead(lightPin), 0, 1023, min_intensity, max_intensity);
}
void display_time(){
int hour = now.hour();
int minute = now.minute();
int second = now.second();
if(minute >= 35){
hour = hour+1; // adjusts the hour. at 7:35, the french actually say "8 moins 25"
}
int rest_minute = (minute % 5);
int rounded_minute = minute-rest_minute;
_il_est();
if(hour == 1 || hour == 13){
_heure();
} else if (hour % 12 != 0){
_heures(); // show the word "hours" unless it's 00:xx or 12:xx
}
show_hour(hour);
show_minute(rounded_minute / 5);
show_minute_edges(rest_minute);
leds.show();
print_time(hour, minute, second);
}
void setup_words(){
words_hour[0] = _minuit;
words_hour[1] = _une;
words_hour[2] = _deux;
words_hour[3] = _trois;
words_hour[4] = _quatre;
words_hour[5] = _cinq;
words_hour[6] = _six;
words_hour[7] = _sept;
words_hour[8] = _huit;
words_hour[9] = _neuf;
words_hour[10] = _dix;
words_hour[11] = _onze;
words_hour[12] = _midi;
words_hour[13] = _une;
words_hour[14] = _deux;
words_hour[15] = _trois;
words_hour[16] = _quatre;
words_hour[17] = _cinq;
words_hour[18] = _six;
words_hour[19] = _sept;
words_hour[20] = _huit;
words_hour[21] = _neuf;
words_hour[22] = _dix;
words_hour[23] = _onze;
words_minute[0] = _nul;
words_minute[1] = _cinq2;
words_minute[2] = _dix2;
words_minute[3] = _et_quart;
words_minute[4] = _vingt;
words_minute[5] = _vingt_cinq;
words_minute[6] = _et_demie;
words_minute[7] = _moins_vingt_cinq;
words_minute[8] = _moins_vingt;
words_minute[9] = _moins_le_quart;
words_minute[10] = _moins_dix;
words_minute[11] = _moins_cinq;
}
void test_all(){
for(int i=0; i < nLEDs; i++) {
light_on(i+1);
leds.show();
delay(50);
}
delay(3000);
reset_leds();
}
void test_words(){
test_hours();
test_minutes();
test_edges();
}
void test_hours(){
for(int i=0; i < 24; i++){
show_hour(i);
leds.show();
delay(1500);
reset_leds();
}
}
void test_minutes(){
for(int i=1; i < 12; i++){
show_minute(i);
leds.show();
delay(1500);
reset_leds();
}
}
void test_edges(){
for(int i=1; i <= 4; i++) {
show_minute_edges(i);
leds.show();
delay(1500);
reset_leds();
}
}
void reset_leds(){
for(int i=0; i < leds.numPixels(); i++) {
light_off(i+1);
}
}
void light_on(int i){
leds.setPixelColor(i-1, color());
}
void light_off(int i){
leds.setPixelColor(i-1, 0);
}
uint32_t color(){
int month = now.month();
int day = now.day();
int second = now.second();
if((day == 8 && month == 4) || (day == 6 && month == 10) || (day == 14 && month == 9)){
return Wheel(map(second, 0, 60, 0, 384));
} else {
return leds.Color(intensity*0.8, intensity*0.9, intensity);
}
}
void show_hour(int i){
(*words_hour[i])();
}
void show_minute(int i){
(*words_minute[i])();
}
void show_minute_edges(int i){
switch (i) {
case 1:
_m();
break;
case 2:
_mm();
break;
case 3:
_mmm();
break;
case 4:
_mmmm();
break;
}
}
/* times */
void _m(){
light_on(115);
}
void _mm(){
_m();
light_on(118);
}
void _mmm(){
_mm();
light_on(111);
}
void _mmmm(){
_mmm();
light_on(114);
}
void _heure(){
light_on(55);
light_on(66);
light_on(75);
light_on(86);
light_on(95);
}
void _heures(){
_heure();
light_on(106);
}
void _il_est(){
light_on(1);
light_on(20);
light_on(40);
light_on(41);
light_on(60);
}
void _une(){
light_on(43);
light_on(58);
light_on(63);
}
void _deux(){
light_on(80);
light_on(81);
light_on(100);
light_on(101);
}
void _trois(){
light_on(62);
light_on(79);
light_on(82);
light_on(99);
light_on(102);
}
void _quatre(){
light_on(2);
light_on(19);
light_on(22);
light_on(39);
light_on(42);
light_on(59);
}
void _cinq(){
light_on(77);
light_on(84);
light_on(97);
light_on(104);
}
void _six(){
light_on(44);
light_on(57);
light_on(64);
}
void _sept(){
light_on(78);
light_on(83);
light_on(98);
light_on(103);
}
void _huit(){
light_on(4);
light_on(17);
light_on(24);
light_on(37);
}
void _neuf(){
light_on(3);
light_on(18);
light_on(23);
light_on(38);
}
void _dix(){
light_on(25);
light_on(36);
light_on(45);
}
void _onze(){
light_on(6);
light_on(15);
light_on(26);
light_on(35);
}
void _midi(){
light_on(5);
light_on(16);
light_on(25);
light_on(36);
}
void _minuit(){
light_on(56);
light_on(65);
light_on(76);
light_on(85);
light_on(96);
light_on(105);
}
void _nul(){
}
void _cinq2(){
light_on(69);
light_on(72);
light_on(89);
light_on(92);
}
void _dix2(){
light_on(87);
light_on(94);
light_on(107);
}
void _et(){
light_on(8);
light_on(13);
}
void _le(){
light_on(67);
light_on(74);
}
void _quart(){
light_on(33);
light_on(48);
light_on(53);
light_on(68);
light_on(73);
}
void _et_quart(){
_et();
_quart();
}
void _vingt(){
light_on(9);
light_on(12);
light_on(29);
light_on(32);
light_on(49);
}
void _vingt_cinq(){
_vingt();
light_on(52);
_cinq2();
}
void _et_demie(){
light_on(10);
light_on(11);
light_on(31);
light_on(50);
light_on(51);
light_on(70);
light_on(71);
}
void _moins(){
light_on(7);
light_on(14);
light_on(27);
light_on(34);
light_on(47);
}
void _moins_vingt_cinq(){
_moins();
_vingt_cinq();
}
void _moins_vingt(){
_moins();
_vingt();
}
void _moins_le_quart(){
_moins();
_le();
_quart();
}
void _moins_dix(){
_moins();
_dix2();
}
void _moins_cinq(){
_moins();
_cinq2();
}
// Input a value 0 to 384 to get a color value.
// The colours are a transition r - g -b - back to r
uint32_t Wheel(int WheelPos)
{
byte r, g, b;
switch(WheelPos / 128)
{
case 0:
r = 127 - WheelPos % 128; //Red down
g = WheelPos % 128; // Green up
b = 0; //blue off
break;
case 1:
g = 127 - WheelPos % 128; //green down
b = WheelPos % 128; //blue up
r = 0; //red off
break;
case 2:
b = 127 - WheelPos % 128; //blue down
r = WheelPos % 128; //red up
g = 0; //green off
break;
}
return(leds.Color(r,g,b));
}