-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandleInputs.ino
161 lines (150 loc) · 7.42 KB
/
handleInputs.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
uint32_t timeWhenInterrupted;
void initInputs() {
pinMode(encoderPushPin, INPUT_PULLUP);
pinMode(startButtonPin, INPUT_PULLUP);
pinMode(startButtonLEDPin, OUTPUT);
digitalWrite(startButtonLEDPin, LOW);
pinMode(batchSelectionButtonPin, INPUT_PULLUP);
pinMode(batchSelectionLEDPin, OUTPUT);
digitalWrite(batchSelectionLEDPin, LOW);
pinMode(stopButtonPin, INPUT_PULLUP);
pinMode(INPUT1, INPUT_PULLUP);
pinMode(INPUT1LEDPin, OUTPUT);
digitalWrite(INPUT1LEDPin, LOW);
pinMode(completeLEDPin, OUTPUT);
digitalWrite(completeLEDPin, LOW);
pinMode(stopButtonLEDPin, OUTPUT);
digitalWrite(stopButtonLEDPin, LOW);
pinMode(dischargeValvePin, OUTPUT);
for (uint8_t i = 0; i < NBINS; i++) {
pinMode(binSelectionButtonPin[i], INPUT_PULLUP);
pinMode(binSelectionLEDPin[i], OUTPUT);
digitalWrite(binSelectionLEDPin[i], LOW);
pinMode(butterflyValvePin[i], OUTPUT);
digitalWrite(butterflyValvePin[i], LOW);
}
pinMode(valveOpenIndicatorPin, OUTPUT);
digitalWrite(valveOpenIndicatorPin, LOW);
}
void handleInputs() {
static long oldPosition;
static uint32_t lastPress;
static bool settingBatch;
static ProcessStates stateWhenInterrupted;
static uint32_t lastStopPressed;
static bool lastStopState;
int32_t encoderPosition = setWeightEncoder.read() / 2; // Read the current encoder input.
digitalWrite(INPUT1LEDPin, digitalRead(INPUT1)); // Have INPUT1LED follow the state of INPUT1.
switch (processState) {
case SET_WEIGHTS:
if (selectedBin == NBINS && // No bin selected so not setting a bin quantity,
settingBatch == false) { // and not setting the number of batches to run.
if (digitalRead(batchSelectionButtonPin) == LOW) { // Batch selection button pressed: set the number of batches to run.
settingBatch = true;
digitalWrite(batchSelectionLEDPin, HIGH); // Switch on the button's LED.
strcpy_P(systemStatus, PSTR("Set batches to run."));
updateDisplay = true;
}
else if (digitalRead(startButtonPin) == LOW) { // Start button pressed: check if we can start the batch.
nBatch = 0; // It's the first batch.
setState(STANDBY); // All is OK to start, go to Standby mode.
}
else {
for (uint8_t i = 0; i < NBINS; i++) { // Check for bin selection button presses.
if (digitalRead(binSelectionButtonPin[i]) == LOW) { // We have a button pressed.
selectedBin = i; // Record which bin was selected.
digitalWrite(binSelectionLEDPin[selectedBin], HIGH); // Switch on the button's LED
sprintf_P(systemStatus, PSTR("Set weight of bin %u."), selectedBin + 1);
updateDisplay = true;
}
}
}
}
else { // A bin has been selected or we're setting the number of batches - read the encoder.
if (encoderPosition != oldPosition) { // If encoder position has changed, calculate the batches accordingly.
if (settingBatch) {
nBatches += (oldPosition - encoderPosition); // Calculate the new number of batches.
nBatches = constrain(nBatches, 1, MAX_BATCHES); // Make sure it's within limits.
}
else {
binTargetWeight[selectedBin] += (oldPosition - encoderPosition); // Calculate the new weight.
binTargetWeight[selectedBin] = constrain(binTargetWeight[selectedBin], MIN_WEIGHT, MAX_WEIGHT); // Make sure it's within limits.
}
updateDisplay = true;
}
if (digitalRead(encoderPushPin) == LOW) { // Encoder pressed to confirm a setting.
if (settingBatch) {
settingBatch = false;
digitalWrite(batchSelectionLEDPin, LOW); // Switch off the button's LED.
}
else {
digitalWrite(binSelectionLEDPin[selectedBin], LOW); // Switch off the button's LED.
selectedBin = NBINS;
}
strcpy_P(systemStatus, PSTR(""));
updateDisplay = true;
}
}
break;
case STANDBY:
case FILLING_BIN:
case FILLING_PAUSE:
case DISCHARGE_BATCH:
if (digitalRead(stopButtonPin) == LOW) { // When stop pressed: interrupt the process.
stateWhenInterrupted = processState;
timeWhenInterrupted = millis(); // Record when we were paused.
lastStopPressed = millis(); // Record when the button was pressed.
setState(STOPPED);
}
break;
case STOPPED:
if (digitalRead(stopButtonPin) == LOW) { // Check the stop button: long press for total cancel.
if (lastStopState == HIGH) { // Previous state high: it's just pressed.
lastStopState = LOW; // We're pressed now.
lastStopPressed = millis(); // Record when it happened.
strcpy_P(systemStatus, PSTR("Hold stop: cancel."));
updateDisplay = true;
}
else if (millis() - lastStopPressed > CANCEL_DELAY) { // It's pressed long enough: cancel process.
setState(SET_WEIGHTS);
}
}
else if (millis() - lastStopPressed > 50) { // Delay for debounce.
if (lastStopState == LOW) {
strcpy_P(systemStatus, PSTR("Paused."));
updateDisplay = true;
}
lastStopState = HIGH; // Button not pressed.
}
if (digitalRead(startButtonPin) == LOW) { // Start button pressed.
digitalWrite(stopButtonLEDPin, LOW); // Switch off the LED in the stop button.
digitalWrite(startButtonLEDPin, HIGH); // Switch on the LED in the start button.
setState(stateWhenInterrupted); // Continue where we were.
if (stateWhenInterrupted == DISCHARGE_BATCH) { // We have to recalculate the start time, to correct for the time we were paused.
lastFillCompleteTime += millis() - timeWhenInterrupted;
}
}
break;
case COMPLETED:
bool action = false;
digitalWrite(startButtonLEDPin, LOW); // Switch off the LED of the Start button.
digitalWrite(completeLEDPin, HIGH); // Switch on the "complete" LED.
if (digitalRead(batchSelectionButtonPin) == LOW || // Batch selection button pressed, or
digitalRead(startButtonPin) == LOW || // Start button pressed, or
digitalRead(stopButtonPin) == LOW || // Stop button pressed, or
digitalRead(encoderPushPin) == LOW || // encoder button pushed, or
encoderPosition != oldPosition) { // encoder moved:
action = true; // User action!
}
for (uint8_t i = 0; i < NBINS; i++) { // Check for bin selection button presses.
if (digitalRead(binSelectionButtonPin[i]) == LOW) { // We have a button pressed.
action = true;
}
}
if (action) { // User did something: clear complete status.
setState(SET_WEIGHTS);
}
break;
}
oldPosition = encoderPosition;
}