@@ -39,10 +39,14 @@ const byte altAdjFn = 255; //if equipped
39
39
40
40
const byte enableSoftAlarmSwitch = 1 ;
41
41
// 1 = yes. Use if using the integrated beeper or another non-switched device (bell solenoid, etc).
42
- // 0 = no. Use if the connected alarm device has its own switch (e.g. clock radio function switch). Alarm will be permanently on in software.
42
+ // 0 = no. Use if the connected alarm device has its own switch (e.g. clock radio function switch).
43
+ // Alarm will be permanently on in software.
43
44
const byte alarmRadio = 0 ;
44
- // 0 = no. Alarm output is connected to the onboard piezoelectric beeper or similar signal device. When alarm and timer go off, it will output a beep pattern for alarmDur minutes.
45
- // 1 = yes. Alarm output is connected to a relay to switch other equipment (like a radio). When alarm goes off, output will stay on for alarmDur minutes (120 is common). When timer is running, output will stay on until timer runs down.
45
+ // 0 = no. Alarm output is connected to the onboard piezoelectric beeper or similar signal device.
46
+ // When alarm and timer go off, it will output a beep pattern for alarmDur minutes.
47
+ // 1 = yes. Alarm output is connected to a relay to switch other equipment (like a radio).
48
+ // When alarm goes off, output will stay on for alarmDur minutes (120 is common).
49
+ // When timer is running, output will stay on until timer runs down.
46
50
const byte alarmDur = 3 ;
47
51
48
52
// How long (in ms) are the button hold durations?
@@ -56,10 +60,6 @@ byte clockFn = 0; //currently displayed function: 0=time, 1=date, 2=alarm, 3=tim
56
60
byte clockFnSet = 0 ; // whether this function is currently being set, and which option/page it's on
57
61
byte setupOptsCt = 3 ; // 1-index
58
62
byte setupOpts[4 ] = {0 ,0 ,1 ,1 }; // see ctrlEvt() switch(clockFnSet) for what these are/do
59
- // 0 = nothing, just a pad to make this 1-index
60
- // 1 = dim(1,0)
61
- // 2 = doesn't do anything, choose a number from 1 to 7
62
- // 3 = doesn't do anything, choose a number from 1 to 13
63
63
64
64
byte displayNext[6 ] = {15 ,15 ,15 ,15 ,15 ,15 }; // Blank tubes at start. When display should change, put it here
65
65
int btnPresses = 0 ;
@@ -73,34 +73,34 @@ int btnPresses = 0;
73
73
// RTC_DS1307 RTC;
74
74
75
75
void setup (){
76
- Serial.begin (57600 );
77
- // Wire.begin(); TODO
78
- // RTC.begin(); TODO
79
- initOutputs ();
80
- initInputs ();
76
+ Serial.begin (57600 );
77
+ // Wire.begin(); TODO
78
+ // RTC.begin(); TODO
79
+ initOutputs ();
80
+ initInputs ();
81
81
updateDisplay (); // initial fill of data
82
82
}
83
83
84
84
void loop (){
85
- // Things done every "clock cycle"
86
- checkInputs (); // will do things if necessary and updateDisplay
87
- cycleDisplay (); // keeps the multiplexing cycle going
85
+ // Things done every "clock cycle"
86
+ checkInputs (); // will do things if necessary and updateDisplay
87
+ cycleDisplay (); // keeps the multiplexing cycle going
88
88
}
89
89
90
90
91
91
// //////// Control inputs //////////
92
92
unsigned long inputSampleLast = 0 ; // millis() of last time inputs were sampled
93
93
int inputSampleDur = 50 ; // input sampling frequency (in ms) to avoid bounce
94
- // bool mainRotLast[2] = {0,0}; //last state of main rotary encoder inputs
94
+ // bool mainRotLast[2] = {0,0}; //last state of main rotary encoder inputs TODO implement
95
95
// bool altRotLast[2] = {0,0}; //and alt (if equipped)
96
96
97
97
void initInputs (){
98
98
// TODO are there no "loose" pins left floating after this? per https://electronics.stackexchange.com/q/37696/151805
99
99
pinMode (A0, INPUT_PULLUP);
100
100
pinMode (A1, INPUT_PULLUP);
101
- pinMode (A2, INPUT_PULLUP);
101
+ pinMode (A2, INPUT_PULLUP);
102
102
pinMode (A3, INPUT_PULLUP);
103
- // 4 and 5 used for I2C
103
+ // 4 and 5 used for I2C
104
104
pinMode (A6, INPUT); digitalWrite (A6, HIGH);
105
105
pinMode (A7, INPUT); digitalWrite (A7, HIGH);
106
106
// If using rotary encoders, capture their initial state
@@ -110,8 +110,8 @@ void initInputs(){
110
110
111
111
void checkInputs (){
112
112
// TODO can all this if/else business be defined at load instead of evaluated every sample?
113
- if (millis () >= inputSampleLast+inputSampleDur) { // time for a sample
114
- inputSampleLast = millis ();
113
+ if (millis () >= inputSampleLast+inputSampleDur) { // time for a sample
114
+ inputSampleLast = millis ();
115
115
// potential issue: if user only means to rotate or push encoder but does both?
116
116
checkBtn (mainSel); // main select
117
117
// if(mainAdjType==2) checkRot(mainAdj,mainAdjRotLast,true); //main rotary encoder
@@ -122,7 +122,7 @@ void checkInputs(){
122
122
// if(altAdjType==2) checkRot(altAdj,altAdjRotLast,true); //alt rotary encoder (if equipped)
123
123
// else
124
124
// if(altAdjType==1) { checkBtn(altAdj[0]); checkBtn(altAdj[1]); } //alt adj buttons
125
- } // end if time for a sample
125
+ } // end if time for a sample
126
126
}
127
127
128
128
// void checkRot(rot[],last[],triggerEvent){
@@ -139,38 +139,39 @@ void checkInputs(){
139
139
140
140
byte btnCur = 0 ; // momentary button currently in use - only one allowed at a time
141
141
unsigned long btnCurStart = 0 ; // when the current button was pressed
142
- byte btnCurHeld = 0 ; // hold thresholds passed: 0=none, 1=reserved TODO, 2=short, 3=long, 4=mute further actions from this button until after it's released
142
+ byte btnCurHeld = 0 ; // hold thresholds passed: 0=none, 1=reserved TODO, 2=short, 3=long,
143
+ // 4=mute further actions from this button until after it's released
143
144
void checkBtn (byte btn){
144
- // Changes in momentary buttons, LOW = pressed.
145
- // When a button event has occurred, will call ctrlEvt(btn,evt)
146
- // evt: 0=pressed, 1=released, 2=held past med point, 3=held past long point
145
+ // Changes in momentary buttons, LOW = pressed.
146
+ // When a button event has occurred, will call ctrlEvt(btn,evt)
147
+ // evt: 0=pressed, 1=released, 2=held past med point, 3=held past long point
147
148
bool bnow = readInput (btn);
148
- // If the button has just been pressed, and no other buttons are in use...
149
- if (btnCur==0 && bnow==LOW) {
150
- btnCur = btn; btnCurHeld = 0 ; btnCurStart = millis ();
149
+ // If the button has just been pressed, and no other buttons are in use...
150
+ if (btnCur==0 && bnow==LOW) {
151
+ btnCur = btn; btnCurHeld = 0 ; btnCurStart = millis ();
151
152
log (" Btn " ); log (String (btn)); log (" has been pressed\n " );
152
- ctrlEvt (btn,1 ); // hey, the button has been pressed
153
- }
154
- // If the button is being held...
155
- if (btnCur==btn && bnow==LOW) {
156
- if (millis () >= btnCurStart+btnLongHold && btnCurHeld < 3 ) {
157
- btnCurHeld = 3 ;
153
+ ctrlEvt (btn,1 ); // hey, the button has been pressed
154
+ }
155
+ // If the button is being held...
156
+ if (btnCur==btn && bnow==LOW) {
157
+ if (millis () >= btnCurStart+btnLongHold && btnCurHeld < 3 ) {
158
+ btnCurHeld = 3 ;
158
159
log (" Btn " ); log (String (btn)); log (" has been long-held\n " );
159
- ctrlEvt (btn,3 ); // hey, the button has been held past the long point
160
- }
161
- else if (millis () >= btnCurStart+btnShortHold && btnCurHeld < 2 ) {
162
- btnCurHeld = 2 ;
160
+ ctrlEvt (btn,3 ); // hey, the button has been held past the long point
161
+ }
162
+ else if (millis () >= btnCurStart+btnShortHold && btnCurHeld < 2 ) {
163
+ btnCurHeld = 2 ;
163
164
log (" Btn " ); log (String (btn)); log (" has been short-held\n " );
164
- ctrlEvt (btn,2 ); // hey, the button has been held past the med point
165
- }
166
- }
167
- // If the button has just been released...
168
- if (btnCur==btn && bnow==HIGH) {
169
- btnCur = 0 ;
165
+ ctrlEvt (btn,2 ); // hey, the button has been held past the med point
166
+ }
167
+ }
168
+ // If the button has just been released...
169
+ if (btnCur==btn && bnow==HIGH) {
170
+ btnCur = 0 ;
170
171
log (" Btn " ); log (String (btn)); log (" has been released\n\n " );
171
172
if (btnCurHeld < 4 ) ctrlEvt (btn,0 ); // hey, the button was released
172
173
btnCurHeld = 0 ;
173
- }
174
+ }
174
175
}
175
176
bool readInput (byte pin){
176
177
if (pin==A6 || pin==A7) return analogRead (pin)<100 ?0 :1 ; // analog-only pins
@@ -179,7 +180,9 @@ bool readInput(byte pin){
179
180
void btnStop (){ btnCurHeld = 4 ; }
180
181
181
182
void ctrlEvt (byte ctrl, byte evt){
182
- // In some cases, when reacting to evt 1/2/3, we may want to call btnStop() so following events 2/3/0 don't cause unintended behavior.
183
+ // Handle button (for now) events.
184
+ // In some cases, when reacting to evt 1/2/3, we may want to call btnStop()
185
+ // so following events 2/3/0 don't cause unintended behavior.
183
186
if (clockFn==255 ) { // SETUP menu
184
187
if (ctrl==mainSel) {
185
188
if (evt==1 ) { // mainSel press
@@ -222,7 +225,7 @@ void ctrlEvt(byte ctrl, byte evt){
222
225
} else { // normal function running
223
226
// short hold: enter clockFnSet
224
227
// press: increment counter
225
- if (ctrl==mainSel && evt==1 ) { btnPresses++; log (" Incrementing counter to " ); log (String (btnPresses)); log (" \n " ); updateDisplay (); return ; }
228
+ if (ctrl==mainSel && evt==1 ) {btnPresses++; log (" Incrementing counter to " ); log (String (btnPresses)); log (" \n " ); updateDisplay (); return ; }
226
229
}
227
230
}
228
231
}
@@ -246,21 +249,21 @@ void updateDisplay(){
246
249
// This function takes that data and uses it to edit displayNext[] for cycleDisplay() to pick up
247
250
switch (clockFn){ // which function are we displaying?
248
251
case 255 : // SETUP menu
249
- log (" Per SETUP, updating display to key=" ); log (String (clockFnSet)); log (" , val=" ); log (String (setupOpts[clockFnSet])); log (" \n " );
250
- editDisplay (clockFnSet, 4 , 5 , false ); // current option key, displayed on little tubes (4-5)
251
- editDisplay (setupOpts[clockFnSet], 0 , 3 , false ); // current option value, on big tubes (0-3)
252
+ log (" Per SETUP, updating display to key=" ); log (String (clockFnSet)); log (" , val=" ); log (String (setupOpts[clockFnSet])); log (" \n " );
253
+ editDisplay (clockFnSet, 4 , 5 , false ); // current option key, displayed on little tubes (4-5)
254
+ editDisplay (setupOpts[clockFnSet], 0 , 3 , false ); // current option value, on big tubes (0-3)
252
255
case 1 : // date
253
- break ;
256
+ break ;
254
257
case 2 : // alarm
255
- break ;
258
+ break ;
256
259
case 3 : // timer
257
- break ;
260
+ break ;
258
261
default : // clock
259
- // TODO currently we are just displaying the btnPresses count
260
- log (" Per normal running mode, displaying btnPresses=" ); log (String (btnPresses)); log (" \n " );
261
- editDisplay (btnPresses, 0 , 3 , true ); // big tubes: counter – pad with leading zeros
262
- blankDisplay (4 ,5 ); // small tubes blank
263
- break ;
262
+ // TODO currently we are just displaying the btnPresses count
263
+ log (" Per normal running mode, displaying btnPresses=" ); log (String (btnPresses)); log (" \n " );
264
+ editDisplay (btnPresses, 0 , 3 , true ); // big tubes: counter – pad with leading zeros
265
+ blankDisplay (4 ,5 ); // small tubes blank
266
+ break ;
264
267
} // end switch clockFn
265
268
} // end updateDisplay()
266
269
void editDisplay (int n, byte posStart, byte posEnd, bool leadingZeros){
@@ -321,10 +324,12 @@ void cycleDisplay(){
321
324
if (clockFnSet>0 ) { // but if we're setting, dim for every other 500ms since we started setting
322
325
if (setStartLast==0 ) setStartLast = millis ();
323
326
dim = 1 -(((millis ()-setStartLast)/500 )%2 );
324
- } else if (setStartLast>0 ) setStartLast=0 ;
327
+ } else {
328
+ if (setStartLast>0 ) setStartLast=0 ;
329
+ }
325
330
326
331
// Anode channel 0: tubes #2 (min x10) and #5 (sec x1)
327
- setCathodes (displayLast[2 ],displayLast[5 ]); // Via d2b decoder chip, set cathodes to old digits
332
+ setCathodes (displayLast[2 ],displayLast[5 ]); // Via d2b decoder chip, set cathodes to old digits
328
333
digitalWrite (anodes[0 ], HIGH); // Turn on tubes
329
334
delay (displayLastFade[0 ]/(dim?4 :1 )); // Display for fade-out cycles
330
335
setCathodes (displayNext[2 ],displayNext[5 ]); // Switch cathodes to new digits
@@ -343,7 +348,7 @@ void cycleDisplay(){
343
348
344
349
if (dim) delay (fadeMax/1.5 );
345
350
346
- // Anode channel 2: tubes #0 (hour x10) and #3 (min x1)
351
+ // Anode channel 2: tubes #0 (hour x10) and #3 (min x1)
347
352
setCathodes (displayLast[0 ],displayLast[3 ]);
348
353
digitalWrite (anodes[2 ], HIGH);
349
354
delay (displayLastFade[2 ]/(dim?4 :1 ));
@@ -354,37 +359,34 @@ void cycleDisplay(){
354
359
if (dim) delay (fadeMax*0.75 );
355
360
356
361
// Loop thru and update all the arrays, and fades.
357
- for ( int i = 0 ; i < 6 ; i ++ )
358
- {
359
- if ( displayNext[i] != displayLast[i] )
360
- {
362
+ for ( int i = 0 ; i < 6 ; i ++ ) {
363
+ if ( displayNext[i] != displayLast[i] ) {
361
364
displayNextFade[i] += fadeStep;
362
365
displayLastFade[i] -= fadeStep;
363
366
364
- if ( displayNextFade[i] >= fadeMax )
365
- {
367
+ if ( displayNextFade[i] >= fadeMax ){
366
368
displayNextFade[i] = 0 .0f ;
367
369
displayLastFade[i] = fadeMax;
368
370
displayLast[i] = displayNext[i];
369
371
}
370
372
}
371
373
}
372
- }
374
+ } // end cycleDisplay()
373
375
void setCathodes (int decValA, int decValB){
374
376
bool binVal[4 ]; // 4-bit binary number with values [1,2,4,8]
375
377
decToBin (binVal,decValA); // have binary value of decVal set into binVal
376
- for (byte i=0 ; i<4 ; i++) digitalWrite (binOutA[i],binVal[i]); // set bin inputs of SN74141 A, which in turn set tube cathodes
378
+ for (byte i=0 ; i<4 ; i++) digitalWrite (binOutA[i],binVal[i]); // set bin inputs of SN74141
377
379
decToBin (binVal,decValB);
378
- for (byte i=0 ; i<4 ; i++) digitalWrite (binOutB[i],binVal[i]); // set bin inputs of SN74141 B. Actual tubes powered depends on anode channel.
379
- }
380
+ for (byte i=0 ; i<4 ; i++) digitalWrite (binOutB[i],binVal[i]); // set bin inputs of SN74141
381
+ } // end setCathodes()
380
382
void decToBin (bool binVal[], int i){
381
383
// binVal is a reference (modify in place) of a binary number bool[4] with values [1,2,4,8]
382
384
if (i<0 || i>15 ) i=15 ; // default value, turns tubes off
383
385
binVal[3 ] = int (i/8 )%2 ;
384
386
binVal[2 ] = int (i/4 )%2 ;
385
387
binVal[1 ] = int (i/2 )%2 ;
386
388
binVal[0 ] = i%2 ;
387
- }
389
+ } // end decToBin()
388
390
389
391
390
392
// //////// Misc global utility functions //////////
0 commit comments