-
Notifications
You must be signed in to change notification settings - Fork 7
/
Timerino.ino
972 lines (919 loc) · 24.9 KB
/
Timerino.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
/*
Darkroom timer with programmable functions
Supported models: DL002A CMG001A
This sketch implements a darkroom timer with various modes.
It's developed after Daniele Lucarelli's version on analogica.it
(http://www.analogica.it/upgrade-timer-con-keypad-t6797.html)
To build this sketch you need, besides core libraries, the Keypad.h
library: http://playground.arduino.cc/code/Keypad
The circuit:
* Arduino UNO/Duemilanove
* 4x4 keypad
* buzzer
* pushbutton
* female 1/4" jack and pedal (optional)
* 220V relay
* 2 toggle switch
* a 10K resistor
* a 330 resistor
Model DL002A specific components:
* 7-Segment 4 number serial LCD display
* Grove LED Bar
Model CMG001A specific components
* 16x2 matrix LCD display
* 1 or 2 potentiometers
The wiring schemes should come with this sketch, if not drop me a line.
### Pin Map Recap - DL002A ###
D0:
D1:
D(2, 3, 4, 5, 6, 7, 8, 9): Keypad
D10: progression switch (linear or f/stop) - maybe superfluous?
D12: Relay
D11: Buzzer
D13: main button (pedal/pushbutton)
A(0, 1): Ledbar
A2: 7 segment display
A3: Lum.Potenz.
### Pin Map Recap - CMG001A ###
D0:
D1:
D(2, 3, 4, 5, 6, 7, 8, 9): Keypad
D10: progression switch (linear or f/stop) - maybe superfluous?
D12: Relay
D11: Buzzer
D13: main button (pedal/pushbutton)
A0-A6: LCD
created 11 Nov 2013
by Daniele Lucarelli
adapted with LCD 16x2 display 5 Dec 2013
by Ciro Mattia Gonano <[email protected]>
TODO: allow setting a buzzer interval for linear countdown (for test strips)
TODO: implement eventListener for keyboard to manage key HOLDing
*/
#include <Keypad.h>
#include <avr/eeprom.h>
#include "Timerino.h" // include personal conf
#if 0 == MYMODEL
// defines for LED Bar. They're set on analog pins 0-1, change accordingly
// if you wired the LED Bar to another location (refer to http://arduino.cc/en/Reference/PortManipulation)
#define LEDBar_DDRData DDRC
#define LEDBar_DDRClk DDRC
#define LEDBar_PORTData PORTC
#define LEDBar_PORTClk PORTC
#define LEDBar_BITData 0x01
#define LEDBar_BITClk 0x02
#define CmdMode 0x0000 //Work on 8-bit mode
#define ON 0x00ff //8-bit 1 data
#define SHUT 0x0000 //8-bit 0 data
// end defines for LCD Bar
#include <SoftwareSerial.h>
SoftwareSerial Serial7Segment(1,A2);
const byte lcdrxpin = A2;
const byte lumpin = A3; //Luminosity control
#elif 1 == MYMODEL
#include <LiquidCrystal.h>
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
#endif
// define wired pins
const byte buzzer = 12; // buzzer pin
const byte mainbtn = 13; // main button (pushbutton/pedal) pin
const byte relay = 11; // relay pin
const byte selector = 10; // progression switch pin
// define tone and delays
const int tone_up = 600;
const int tone_down = 300;
const int scrollTime = 250;
const int waitTime = 800;
/************* YOU SHOULD NOT TOUCH ANYTHING BELOW THIS LINE *************/
// keypad
const byte ROWS = 4; // keypad: 4 rows
const byte COLS = 4; // keypad: 4 cols
// modes
const byte MODLINFREE = 0b00000;
const byte MODLINUP = 0b00010;
const byte MODLINDOWN = 0b00100;
const byte MODLINDDS = 0b00110;
const byte MODFSTFREE = 0b00001;
const byte MODFSTPREC = 0b00011;
const byte MODFSTTEST = 0b00101;
const byte MODFSTDOWN = 0b00111;
// Multiplier formula for f/stop progression (thanks to Gergio)
// mult = 2^(1/precision)
// Variables
int i = 0;
int brightness = 0;
int time = 0;
int time_succ;
int appo_time;
int time_countdown;
int time_dds;
int time_fsttest;
int time_fstdown;
byte timer_mode = MODLINFREE;
long last_time = 0;
long errlet = 0;
int btnstatus = 0; // main button status
int lastbtnstatus = LOW; // last main button status
int selstatus = LOW;
int lastselstatus = LOW;
long lum;
int lastlum;
boolean running = false;
boolean btnhigh = false;
boolean firstpress = true;
boolean mute = false;
float mult = 1.0;
int precis = 1;
char _buffer[17];
/* init matrix keypad */
char keys[ROWS][COLS] = {
{1,2,3,'A' },
{4,5,6,'B' },
{7,8,9,'C' },
{'*','0','#','D' }
};
byte rowPins[ROWS] = {6, 7, 8, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {2, 3, 4, 5}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
/***************** DISPLAY functions *****************/
void beep(int this_tone, int duration) {
if (!mute)
tone(buzzer, this_tone, duration);
}
void metronome() {
if (running && time%10 == 0) {
beep(tone_down, 80);
}
}
#if 0 == MYMODEL // add LED Bar functions
void LEDBar_set_LED_Index(unsigned int index) {
unsigned char i;
LEDBar_send16bitData(CmdMode);
for (i=0;i<12;i++) {
LEDBar_send16bitData(index&0x0001 ? ON : SHUT);
index= index>>1;
}
LEDBar_latchData();
}
void LEDBar_send16bitData(unsigned int data) {
for (unsigned char i=0;i<16;i++) {
data&0x8000 ? LEDBar_PORTData |= LEDBar_BITData : LEDBar_PORTData &=~ LEDBar_BITData;
LEDBar_PORTClk ^= LEDBar_BITClk;
data <<= 1;
}
}
void LEDBar_latchData(void) {
LEDBar_PORTData &=~ LEDBar_BITData;
delayMicroseconds(10);
for(unsigned char i=0;i<8;i++)
LEDBar_PORTData ^= LEDBar_BITData;
}
#endif
void setup_display() {
#if 0 == MYMODEL
LEDBar_set_LED_Index(0b000001111111111);
// Setup 7SegmentDisplay
Serial7Segment.begin(9600); // Connect to 7-Segment display in serial mode
Serial7Segment.write(0x76); // Display reset - brings cursors to the first char
lum = analogRead(lumpin);
brightness = 100*lum/1024;
Serial7Segment.write(0x7A); // Brightness control command
Serial7Segment.write((byte) brightness);
lastlum = lum;
Serial7Segment.write('A');
LEDBar_set_LED_Index(0b000000111111111);
delay(scrollTime);
Serial7Segment.write('N');
LEDBar_set_LED_Index(0b000000011111111);
delay(scrollTime);
Serial7Segment.write('A');
LEDBar_set_LED_Index(0b000000001111111);
delay(scrollTime);
Serial7Segment.write('L');
LEDBar_set_LED_Index(0b000000000111111);
delay(scrollTime);
Serial7Segment.print("NALO");
LEDBar_set_LED_Index(0b000000000011111);
delay(scrollTime);
Serial7Segment.print("ALOG");
LEDBar_set_LED_Index(0b000000000001111);
delay(scrollTime);
Serial7Segment.print("LOGI");
LEDBar_set_LED_Index(0b000000000000111);
delay(scrollTime);
Serial7Segment.print("OGIC");
LEDBar_set_LED_Index(0b000000000000011);
delay(scrollTime);
Serial7Segment.print("GICA");
Serial7Segment.write(0x77);
Serial7Segment.write(0b00001000);
LEDBar_set_LED_Index(0b000000000000001);
delay(scrollTime);
Serial7Segment.print("ICAI");
Serial7Segment.write(0x77);
Serial7Segment.write(0b00000100);
LEDBar_set_LED_Index(0b000000000000000);
delay(scrollTime);
Serial7Segment.print("CAIT");
Serial7Segment.write(0x77);
Serial7Segment.write(0b00000010);
LEDBar_set_LED_Index(0b000001000000000);
delay(scrollTime);
LEDBar_set_LED_Index(0b000000000000000);
delay(scrollTime);
LEDBar_set_LED_Index(0b000001000000000);
delay(scrollTime);
Serial7Segment.print("AIT ");
Serial7Segment.write(0x77);
Serial7Segment.write(0b00000001);
LEDBar_set_LED_Index(0b000000000000000);
delay(scrollTime);
Serial7Segment.print("IT ");
Serial7Segment.write(0x77);
Serial7Segment.write(0b01000000);
LEDBar_set_LED_Index(0b000001000000000);
delay(scrollTime);
Serial7Segment.print("T 0");
LEDBar_set_LED_Index(0b000000000000000);
delay(scrollTime);
Serial7Segment.print(" 00");
LEDBar_set_LED_Index(0b000001000000000);
delay(scrollTime);
Serial7Segment.print(" 000");
LEDBar_set_LED_Index(0b000000000000000);
delay(scrollTime);
reset();
delay(scrollTime);
delay(scrollTime);
LEDBar_set_LED_Index(0b000001000000000);
#elif 1 == MYMODEL
lcd.begin(16, 2);
lcd.clear();
sprintf(_buffer,"%s","ANALOGICA.IT ");
lcd.setCursor(16,1);
lcd.autoscroll();
for (int thisChar=0; thisChar < 16; thisChar++) {
lcd.print(_buffer[thisChar]);
delay(scrollTime);
}
lcd.noAutoscroll();
#endif
}
void say_reset() {
#if 0 == MYMODEL
Serial7Segment.write(0x76);
Serial7Segment.print("0000");
Serial7Segment.write(0x77);
Serial7Segment.write(0b00000100);
#elif 1 == MYMODEL
lcd.setCursor(0,0);
lcd.clear();
#endif
}
void say_clearprecis() {
#if 0 == MYMODEL
LEDBar_set_LED_Index(0b000000000000000);
say_reset();
#elif 1 == MYMODEL
lcd.setCursor(12,1);
lcd.print(" ");
#endif
}
void say_cleartime() {
#if 0 == MYMODEL
say_reset();
#elif 1 == MYMODEL
lcd.setCursor(0,1);
lcd.print(" ");
#endif
}
void say_time() {
//say_cleartime();
#if 0 == MYMODEL
Serial7Segment.write(0x77);
Serial7Segment.write(0b00000100); // light up 1 decimal point
sprintf(_buffer, "%04i", time);
Serial7Segment.write(_buffer);
#elif 1 == MYMODEL
float ftime = time > 0 ? (float)time / 10 : 0.0;
dtostrf(ftime, 3, 1, _buffer);
lcd.setCursor(0,1);
lcd.print(_buffer);
#endif
}
void say_prec() {
say_clearprecis();
#if 0 == MYMODEL
Serial7Segment.write(0x76);
switch (precis) {
case 1:
LEDBar_set_LED_Index(0b000001000000000);
Serial7Segment.print("1-1 ");
break;
case 2:
LEDBar_set_LED_Index(0b000000100000000);
Serial7Segment.print("1-2 ");
break;
case 3:
LEDBar_set_LED_Index(0b000000010000000);
Serial7Segment.print("1-3 ");
break;
case 4:
LEDBar_set_LED_Index(0b000000001000000);
Serial7Segment.print("1-4 ");
break;
case 6:
LEDBar_set_LED_Index(0b000000000100000);
Serial7Segment.print("1-6 ");
break;
case 8:
LEDBar_set_LED_Index(0b000000000010000);
Serial7Segment.print("1-8 ");
break;
case 12:
LEDBar_set_LED_Index(0b000000000001000);
Serial7Segment.print("1-12");
break;
case 24:
LEDBar_set_LED_Index(0b000000000000100);
Serial7Segment.print("1-24");
break;
case 32:
LEDBar_set_LED_Index(0b000000000000010);
Serial7Segment.print("1-32");
break;
case 48:
LEDBar_set_LED_Index(0b000000000000001);
Serial7Segment.print("1-48");
break;
}
#elif 1 == MYMODEL
switch(precis) {
case 1: lcd.setCursor(15,1); lcd.print("1"); break;
case 2: lcd.setCursor(13,1); lcd.print("1/2"); break;
case 3: lcd.setCursor(13,1); lcd.print("1/3"); break;
case 4: lcd.setCursor(13,1); lcd.print("1/4"); break;
case 6: lcd.setCursor(13,1); lcd.print("1/6"); break;
case 8: lcd.setCursor(13,1); lcd.print("1/8"); break;
case 12: lcd.setCursor(12,1); lcd.print("1/12"); break;
case 24: lcd.setCursor(12,1); lcd.print("1/24"); break;
case 32: lcd.setCursor(12,1); lcd.print("1/32"); break;
case 48: lcd.setCursor(12,1); lcd.print("1/48"); break;
}
#endif
}
void say_free() {
#if 0 == MYMODEL
Serial7Segment.write(0x76);
Serial7Segment.print("LINE");
beep(tone_up, 200);
delay(500);
#elif 1 == MYMODEL
lcd.setCursor(0,0);
lcd.print("Free Mode ");
beep(tone_up, 200);
#endif
say_clearprecis();
say_cleartime();
#if 1 == EPR
eeprom_write_byte(0, MODLINFREE);
#endif
}
void say_up(){
#if 0 == MYMODEL
Serial7Segment.write(0x76);
Serial7Segment.print("Up ");
beep(tone_up, 200);
delay(500);
#elif 1 == MYMODEL
lcd.setCursor(0,0);
lcd.print("Stopwatch ");
beep(tone_up, 200);
#endif
say_clearprecis();
say_time();
#if 1 == EPR
eeprom_write_byte(0, MODLINUP);
#endif
}
void say_down(){
#if 0 == MYMODEL
Serial7Segment.write(0x76);
Serial7Segment.print("DouN");
beep(tone_down, 200);
delay(500);
#elif 1 == MYMODEL
lcd.setCursor(0,0);
lcd.print("Countdown ");
beep(tone_down, 200);
#endif
say_clearprecis();
say_time();
#if 1 == EPR
eeprom_write_byte(0, MODLINDOWN);
#endif
}
void say_dds() {
#if 0 == MYMODEL
Serial7Segment.write(0x76);
Serial7Segment.print("DDS ");
beep(tone_down, 200);
delay(500);
#elif 1 == MYMODEL
lcd.setCursor(0,0);
lcd.print("DDS Mode ");
beep(tone_down, 200);
#endif
say_clearprecis();
say_time();
#if 1 == EPR
eeprom_write_byte(0, MODLINDDS);
#endif
}
void say_fstop(){
#if 0 == MYMODEL
Serial7Segment.write(0x76);
Serial7Segment.print("F-ST");
beep(tone_up, 200);
delay(500);
#elif 1 == MYMODEL
lcd.setCursor(0,0);
lcd.print("F/stop Free Mode");
beep(tone_up, 200);
say_cleartime();
#endif
say_prec();
#if 1 == EPR
eeprom_write_byte(0, MODFSTFREE);
#endif
}
void say_precis() {
#if 0 == MYMODEL
Serial7Segment.write(0x76);
Serial7Segment.print("pREC");
beep(tone_down, 200);
delay(500);
#elif 1 == MYMODEL
lcd.setCursor(0,0);
lcd.print("F/Stop Precision");
beep(tone_down, 200);
say_cleartime();
#endif
say_prec();
#if 1 == EPR
eeprom_write_byte(0, MODFSTPREC);
#endif
}
void say_test_strip() {
#if 0 == MYMODEL
Serial7Segment.write(0x76);
Serial7Segment.print("pROS");
beep(tone_down, 200);
delay(500);
#elif 1 == MYMODEL
lcd.setCursor(0,0);
lcd.print("F/stop TestStrip");
beep(tone_down, 200);
#endif
say_prec();
say_time();
#if 1 == EPR
eeprom_write_byte(0, MODFSTTEST);
#endif
}
void say_fstopdown() {
#if 0 == MYMODEL
Serial7Segment.write(0x76);
Serial7Segment.print("CFST");
beep(tone_down, 200);
delay(500);
#elif 1 == MYMODEL
lcd.setCursor(0,0);
lcd.print("F/stop Countdown");
beep(tone_down, 200);
#endif
say_prec();
say_time();
#if 1 == EPR
eeprom_write_byte(0, MODFSTDOWN);
#endif
}
void say_timermode() {
switch (timer_mode) {
case MODLINFREE: say_free(); break;
case MODLINUP: say_up(); break;
case MODLINDOWN: say_down(); break;
case MODLINDDS: say_dds(); break;
case MODFSTFREE: say_fstop(); break;
case MODFSTPREC: say_precis(); break;
case MODFSTTEST: say_test_strip(); break;
case MODFSTDOWN: say_fstopdown(); break;
default: break;
}
}
/************ MODE FUNCTIONS ********/
void off() {
// no action taken
}
void countdown() {
if (time > 0 && (millis() - last_time >= 100)) {
last_time = millis();
time--;
metronome();
say_time();
if (time == 0) {
// if time reached 0, shut down the relay and reset the function
digitalWrite(relay, LOW);
running = false;
time = appo_time;
beep(tone_down, 75);
delay(150);
beep(tone_down, 75);
delay(150);
beep(tone_down, 150);
if (timer_mode == MODLINDDS) {
firstpress = true;
}
say_time();
}
}
}
void stopwatch() {
if (millis() - last_time >= 100) {
last_time = millis();
time++;
metronome();
say_time();
}
}
void test_strip() {
if (millis() - last_time >= 100) {
last_time = millis();
time++;
say_time();
if (time == time_succ) {
beep(tone_up, 150);
mult = pow(2.0,(1.0/precis));
time_succ = int(float(time) * mult);
}
if (time_succ - time <= 3) {
beep(tone_up, 50);
}
}
}
void reset() {
say_reset();
time = 0;
appo_time = 0;
}
#if 1 == EPR
void load_eeprom() {
timer_mode = eeprom_read_byte(0);
// if mode is not valid set the first one and return
if ((timer_mode & 0b11000) > 0) {
timer_mode = MODLINFREE;
return;
}
time_countdown = eeprom_read_word((uint16_t *)1); // 2 byte
time_dds = eeprom_read_word((uint16_t *)3); // 2 byte
time_fsttest = eeprom_read_word((uint16_t *)5); // 2 byte
time_fstdown = eeprom_read_word((uint16_t *)7); // 2 byte
precis = eeprom_read_word((uint16_t *)9); // 2 byte
}
#endif
void init_timermode() {
switch (timer_mode) {
case MODLINDOWN:
time = time_countdown;
break;
case MODLINDDS:
time = time_dds;
firstpress = true;
break;
case MODFSTTEST:
time = time_fsttest;
break;
case MODFSTDOWN:
time = time_fstdown;
break;
case MODLINFREE:
case MODLINUP:
case MODFSTFREE:
case MODFSTPREC:
default:
break;
}
say_timermode();
}
/*** Keypad management functions ***/
//void keypadEvent(KeypadEvent key) {}
void read_key() {
int key = keypad.getKey();
if (key == NO_KEY) return;
switch (key) {
case 'A': // Free mode
reset();
timer_mode = MODLINFREE | (timer_mode & 0b00001);
init_timermode();
break;
case 'B':
reset();
timer_mode = MODLINUP | (timer_mode & 0b00001);
init_timermode();
break;
case 'C':
reset();
timer_mode = MODLINDOWN | (timer_mode & 0b00001);
init_timermode();
break;
case 'D':
reset();
timer_mode = MODLINDDS | (timer_mode & 0b00001);
init_timermode();
break;
case '*': // reset for functions that need that
if (timer_mode == MODFSTDOWN) {
// Step down
mult = pow(2.0, (1.0/precis));
time = int(float(time) / mult);
if (time <= 1)
time = 1;
appo_time = time;
say_time();
} else if (timer_mode == MODLINUP || timer_mode == MODLINDOWN || timer_mode == MODLINDDS || timer_mode == MODFSTTEST) {
reset();
}
break;
case '#':
if (timer_mode == MODFSTDOWN) {
// Step up
mult = pow(2.0, (1.0/precis));
time = int(float(time) * mult);
if (time >= 9999)
time = 9999;
appo_time = time;
say_time();
}
break;
default:
if (timer_mode == MODLINUP) { // stopwatch does not accept numbers input
say_up();
appo_time = time;
say_time();
} else if (timer_mode == MODFSTPREC) { // set precision
switch (key) {
case 1: precis = 1; say_prec();
#if 1 == EPR
eeprom_write_word((uint16_t *)9, precis);
#endif
break;
case 2: precis = 2; say_prec();
#if 1 == EPR
eeprom_write_word((uint16_t *)9, precis);
#endif
break;
case 3: precis = 3; say_prec();
#if 1 == EPR
eeprom_write_word((uint16_t *)9, precis);
#endif
break;
case 4: precis = 4; say_prec();
#if 1 == EPR
eeprom_write_word((uint16_t *)9, precis);
#endif
break;
case 5: precis = 6; say_prec();
#if 1 == EPR
eeprom_write_word((uint16_t *)9, precis);
#endif
break;
case 6: precis = 8; say_prec();
#if 1 == EPR
eeprom_write_word((uint16_t *)9, precis);
#endif
break;
case 7: precis = 12; say_prec();
#if 1 == EPR
eeprom_write_word((uint16_t *)9, precis);
#endif
break;
case 8: precis = 24; say_prec();
#if 1 == EPR
eeprom_write_word((uint16_t *)9, precis);
#endif
break;
case 9: precis = 32; say_prec();
#if 1 == EPR
eeprom_write_word((uint16_t *)9, precis);
#endif
break;
case '0': precis = 48; say_prec();
#if 1 == EPR
eeprom_write_word((uint16_t *)9, precis);
#endif
break;
default: break;
}
} else {
if (time < 1) {
time = key == '0' ? 0 : key;
} else {
time *= 10;
if (time > 9999)
time %= 10000;
if (key != '0')
time += key;
}
appo_time = time;
say_time();
}
}
}
void setup() {
// Serial.begin(9600);
// Serial.println(">>> Debug <<<");
#if 0 == MYMODEL
// init LED Bar
LEDBar_DDRData |= LEDBar_BITData;
LEDBar_DDRClk |= LEDBar_BITClk;
#endif
/* ensure analog pins are set to OUTPUT */
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
#if 0 == MYMODEL
pinMode(lumpin, INPUT);
#elif 1 == MYMODEL
pinMode(A3, OUTPUT);
#endif
pinMode(A4, OUTPUT);
pinMode(A5, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(relay, OUTPUT);
pinMode(mainbtn, INPUT);
pinMode(selector, INPUT);
// read from EEPROM stored values
#if 1 == EPR
load_eeprom();
#endif
// Setup display
setup_display();
// init fstop/linear mode
selstatus = digitalRead(selector);
lastselstatus = selstatus;
if (selstatus == HIGH) {
bitSet(timer_mode,0); // f/stop mode
} else {
bitClear(timer_mode,0); // linear mode
}
// TODO: add event listener for keypad
//keypad.addEventListener(keypadEvent);
// cleanup and get ready to start
delay(waitTime);
say_reset();
init_timermode();
errlet = millis();
digitalWrite(relay, LOW); // shut down the relay
}
void loop() {
if (millis() - errlet <= 20) {
} else {
btnstatus = digitalRead(mainbtn);
if (btnstatus != lastbtnstatus) {
// main button has been toggled
if (HIGH == btnstatus) {
// main button has been pressed
btnhigh = true;
}
lastbtnstatus = btnstatus;
}
errlet = millis();
}
selstatus = digitalRead(selector);
if (selstatus != lastselstatus) {
// mode change
lastselstatus = selstatus;
timer_mode = (selstatus == HIGH) ? MODFSTFREE : MODLINFREE;
say_timermode();
}
#if 0 == MYMODEL
lum = analogRead(lumpin);
if (lum < lastlum-10 || lum > lastlum+10) {
brightness = 100*lum/1024;
Serial7Segment.write(0x7A); // Brightness control command
Serial7Segment.write((byte) brightness);
lastlum = lum;
}
#endif
if (running && btnhigh) {
// shut down the relay and pause the timer
digitalWrite(relay, LOW);
btnhigh = false;
running = false;
if (timer_mode == MODLINDDS && firstpress) {
firstpress = false;
}
} else if (running && !btnhigh) {
switch (timer_mode) {
case MODLINFREE:
off();
break;
case MODLINUP:
stopwatch();
break;
case MODLINDOWN:
countdown();
break;
case MODLINDDS:
firstpress ? off() : countdown();
break;
case MODFSTFREE:
off();
break;
case MODFSTPREC:
off();
break;
case MODFSTTEST:
test_strip();
break;
case MODFSTDOWN:
countdown();
break;
}
} else if (!running && btnhigh) { // not running
btnhigh = false;
switch (timer_mode) {
case MODLINFREE:
case MODFSTFREE:
digitalWrite(relay, HIGH); // powerup the relay
running = true;
off();
break;
case MODLINUP:
digitalWrite(relay, HIGH); // powerup the relay
running = true;
last_time = millis(); // reset timer counter
stopwatch();
break;
case MODLINDOWN:
time_countdown = appo_time;
#if 1 == EPR
eeprom_write_word((uint16_t *)1, time_countdown);
#endif
if (time > 0) {
digitalWrite(relay, HIGH); // powerup the relay
running = true;
last_time = millis(); // reset timer counter
countdown();
}
break;
case MODLINDDS:
time_dds = appo_time;
#if 1 == EPR
eeprom_write_word((uint16_t *)3, time_dds);
#endif
if (time > 0) {
digitalWrite(relay, HIGH); // powerup the relay
running = true;
if (firstpress == true) {
off();
} else {
last_time = millis(); // reset timer counter
countdown();
}
}
break;
case MODFSTPREC:
off();
break;
case MODFSTTEST:
time_fsttest = appo_time;
#if 1 == EPR
eeprom_write_word((uint16_t *)5, time_fsttest);
#endif
digitalWrite(relay, HIGH); // powerup the relay
running = true;
last_time = millis(); // reset timer counter
if (time < 10)
time = 10;
time_succ = time;
time = 0;
test_strip();
break;
case MODFSTDOWN:
time_fstdown = appo_time;
#if 1 == EPR
eeprom_write_word((uint16_t *)7, time_fstdown);
#endif
if (time > 0) {
digitalWrite(relay, HIGH); // powerup the relay
running = true;
last_time = millis(); // reset timer counter
countdown();
}
break;
}
} else { // not running nor button released, so read keypad input
read_key();
}
}