You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//UNDB v8 modified to v9 spec (put Sel/Alt on A7/A6, Up/Down on A0/A1, relay on A3, led on 9, and cathode B4 on A2), relay disabled, Sel/Alt buttons reversed, with 6-digit display.
//For sports exhibit: how many times can you press Sel in N seconds?
2
+
//For 6-tube display
3
+
4
+
#include"configs/v8c-6tube-relayswitch-pwm.h"
5
+
6
+
// Hardware inputs and value setting
7
+
byte btnCur = 0; //Momentary button currently in use - only one allowed at a time
8
+
byte btnCurHeld = 0; //Button hold thresholds: 0=none, 1=unused, 2=short, 3=long, 4=set by btnStop()
9
+
unsignedlong inputLast = 0; //When a button was last pressed
10
+
unsignedlong inputLast2 = 0; //Second-to-last of above
11
+
//TODO the math between these two may fail very rarely due to millis() rolling over while setting. Need to find a fix. I think it only applies to the rotary encoder though.
12
+
13
+
word timerStart = 5;
14
+
byte isRunning = 0; //1 if running, 2 if finished(?)
15
+
unsignedlong runStart = 0;
16
+
word btnCount = 0;
17
+
word timerCur = 5;
18
+
word timerLast = 5;
19
+
20
+
////////// Main code control //////////
21
+
22
+
voidsetup(){
23
+
Serial.begin(9600);
24
+
initInputs();
25
+
delay(100);
26
+
initOutputs(); //depends on some EEPROM settings
27
+
updateDisplay();
28
+
}
29
+
30
+
voidloop(){
31
+
//TODO does not account for millis rollover - should be restarted at least once every 49 days
32
+
//while (millis() < start + ms) ; // BUGGY version
33
+
//while (millis() - start < ms) ; // CORRECT version
34
+
//https://arduino.stackexchange.com/a/12588
35
+
if(isRunning==1) {
36
+
unsignedlong now = millis();
37
+
if(now-runStart >= timerStart*1000) { //If timer has finished, stop it
38
+
Serial.println(F("Stopping."));
39
+
timerCur = 0;
40
+
timerLast = 0;
41
+
isRunning = 0; //or 2
42
+
tone(piezoPin,getHz(beepTone),500);
43
+
updateDisplay();
44
+
} else {
45
+
timerCur = timerStart - ((now - runStart)/1000); //Update timercur value
46
+
if(timerLast>timerCur) {
47
+
Serial.print(timerCur,DEC);
48
+
Serial.println(" seconds left");
49
+
updateDisplay();
50
+
//tone(piezoPin,getHz(beepTone),50);
51
+
timerLast = timerCur;
52
+
}
53
+
}
54
+
}
55
+
checkInputs(); //if inputs have changed, this will do things + updateDisplay as needed
56
+
cycleDisplay(); //keeps the display hardware multiplexing cycle going
57
+
}
58
+
59
+
60
+
////////// Control inputs //////////
61
+
62
+
voidinitInputs(){
63
+
//TODO are there no "loose" pins left floating after this? per https://electronics.stackexchange.com/q/37696/151805
64
+
pinMode(mainSel, INPUT_PULLUP);
65
+
pinMode(mainAdjUp, INPUT_PULLUP);
66
+
pinMode(mainAdjDn, INPUT_PULLUP);
67
+
pinMode(altSel, INPUT_PULLUP);
68
+
//rotary encoder init
69
+
//TODO encoder support
70
+
}
71
+
72
+
voidcheckInputs(){
73
+
//TODO can all this if/else business be defined at load instead of evaluated every sample? OR is it compiled that way?
74
+
//TODO potential issue: if user only means to rotate or push encoder but does both?
75
+
//check button states
76
+
checkBtn(mainSel); //main select
77
+
if(mainAdjType==1) { checkBtn(mainAdjUp); checkBtn(mainAdjDn); } //if mainAdj is buttons
ctrlEvt(btn,2); //hey, the button has been short-held
104
+
}
105
+
}
106
+
//If the button has just been released...
107
+
if(btnCur==btn && bnow==HIGH) {
108
+
btnCur = 0;
109
+
if(btnCurHeld < 4) ctrlEvt(btn,0); //hey, the button was released
110
+
btnCurHeld = 0;
111
+
}
112
+
}
113
+
voidbtnStop(){
114
+
//In some cases, when handling btn evt 1/2/3, we may call this so following events 2/3/0 won't cause unintended behavior (e.g. after a fn change, or going in or out of set)
115
+
btnCurHeld = 4;
116
+
}
117
+
118
+
119
+
////////// Input handling and value setting //////////
120
+
121
+
voidctrlEvt(byte ctrl, byte evt){
122
+
// Serial.print(F("Button "));
123
+
// Serial.print(ctrl,DEC);
124
+
// Serial.println(F(" pressed"));
125
+
//Handle control events (from checkBtn or checkRot), based on current fn and set state.
//UNDB v8 modified to v9 spec (put Sel/Alt on A7/A6, Up/Down on A0/A1, relay on A3, led on 9, and cathode B4 on A2), relay disabled, Sel/Alt buttons reversed, with 6-digit display.
0 commit comments