button debouncing, cancelling run commands works right

it has been deployed onto the trailer
This commit is contained in:
Thaddeus Hughes
2025-08-28 11:30:09 -05:00
parent 9bc81a9ebf
commit 9c295adb7f

View File

@@ -19,7 +19,7 @@ bool sensorFallingEdges[NUM_SENSORS] = {false, false, false, false};
uint32_t lastSensorDebounceTimes[NUM_SENSORS] = {0, 0, 0, 0}; uint32_t lastSensorDebounceTimes[NUM_SENSORS] = {0, 0, 0, 0};
void checkSensors() { void checkSensors() {
Serial.print(' '); //Serial.print(' ');
for (uint8_t i = 0; i < NUM_SENSORS; i++) { for (uint8_t i = 0; i < NUM_SENSORS; i++) {
bool reading = digitalRead(SENSORS[i]); bool reading = digitalRead(SENSORS[i]);
@@ -34,10 +34,10 @@ void checkSensors() {
} }
lastSensorStates[i] = reading; lastSensorStates[i] = reading;
Serial.print(currentSensorStates[i] ? '-':'T'); //Serial.print(currentSensorStates[i] ? '-':'T');
} }
Serial.print(' '); //Serial.print(' ');
} }
bool checkSensor(int8_t ch) { bool checkSensor(int8_t ch) {
@@ -74,17 +74,8 @@ void checkButtons() {
} }
lastButtonStates[i] = reading; lastButtonStates[i] = reading;
if (buttonFallingEdges[i]) {
buttonFallingEdges[i] = false;
lastRepeatTimes[i] = millis();
}
/*if (currentButtonStates[i] == LOW && (millis() - lastRepeatTimes[i]) >= REPEAT_INTERVAL) { //Serial.print(currentButtonStates[i] ? '-':'T');
lastRepeatTimes[i] = millis();
onButtonPush(i);
}*/
Serial.print(currentButtonStates[i] ? '-':'T');
} }
//Serial.println(); //Serial.println();
} }
@@ -140,9 +131,9 @@ void setOutputs(int8_t cmd) {
lastt = millis(); lastt = millis();
} }
Serial.print("CMD_"); //Serial.print("CMD_");
Serial.print(currentcmd); //Serial.print(currentcmd);
Serial.print(" "); //Serial.print(" ");
setOutputsRaw(currentcmd); setOutputsRaw(currentcmd);
} }
@@ -155,11 +146,13 @@ int8_t state;
#define STATE_JOG 1 #define STATE_JOG 1
#define STATE_RUN 2 #define STATE_RUN 2
#define JOG_DEBOUNCE 160
// handle state transitions // handle state transitions
void changeState(int8_t newState, int8_t newChannel=0) { void changeState(int8_t newState, int8_t newChannel=0) {
switch(newState) { switch(newState) {
case STATE_JOG: case STATE_JOG:
timer = millis() + 200; timer = millis() + JOG_DEBOUNCE;
break; break;
case STATE_RUN: case STATE_RUN:
if (state != newState || channel != newChannel) if (state != newState || channel != newChannel)
@@ -177,6 +170,7 @@ void changeState(int8_t newState, int8_t newChannel=0) {
Serial.print(":"); Serial.print(":");
Serial.print(newChannel); Serial.print(newChannel);
Serial.print(" "); Serial.print(" ");
Serial.println();
} }
state = newState; state = newState;
@@ -184,43 +178,34 @@ void changeState(int8_t newState, int8_t newChannel=0) {
} }
uint32_t btn_runt=0;
#define DEBOUNCE_BTN_RUN 350
void runFSM() { void runFSM() {
// if any button is pressed, cancel any command, run the output manually. // if any button is pressed, cancel any command, run the output manually.
bool overridden = false; bool overridden = false;
for (int8_t i=0; i<4; i++) { for (int8_t i=0; i<4; i++) {
if (currentButtonStates[i]) continue; if (currentButtonStates[i]){
buttonFallingEdges[i] = false;
continue;
}
if (!digitalRead(53)) { if (!digitalRead(53)) {
changeState(STATE_OFF); changeState(STATE_OFF);
setOutputs(checkSensor(i+1) ? CMD_OFF:i+1); setOutputs(checkSensor(i+1) ? CMD_OFF:i+1);
} else if (!digitalRead(51)) { } else if (!digitalRead(51)) {
if (buttonFallingEdges[i]) {
// TODO: iron this if (state == STATE_RUN) {
if (state == STATE_RUN) {
// if running and a different channel, just shut off
if (channel != i+1) {
changeState(STATE_OFF);
// if running and the same channel, and we are past the time of the button being pressed
} else if (millis() > btn_runt) {
changeState(STATE_OFF); changeState(STATE_OFF);
} else { } else {
changeState(STATE_RUN, i+1); changeState(STATE_RUN, i+1);
btn_runt = millis() + DEBOUNCE_BTN_RUN;
} }
} else {
changeState(STATE_RUN, i+1);
btn_runt = millis() + DEBOUNCE_BTN_RUN;
} }
} else { } else {
changeState(STATE_OFF); changeState(STATE_OFF);
setOutputs(i+1); setOutputs(i+1);
} }
buttonFallingEdges[i] = false;
overridden = true; overridden = true;
} }
@@ -259,8 +244,44 @@ void runFSM() {
} }
} }
uint32_t rf_runt = 0; /*#define NUM_CMDS 8
#define DEBOUNCE_RF 500 #define DEBOUNCE_RF 500
bool lastCmdStates[NUM_CMDS] = {LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW};
bool currentCmdStates[NUM_CMDS] = {LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW};
bool cmdRisingEdges[NUM_CMDS] = {0,0,0,0,0,0,0,0};
uint32_t lastCmdDebounceTimes[NUM_BUTTONS] = {0, 0, 0, 0, 0};
void checkButtons() {
for (uint8_t i = 0; i < NUM_CMDS; i++) {
if (reading != lastCmdStates[i]) {
lastCmdDebounceTimes[i] = millis();
}
if ((millis() - lastCmdDebounceTimes[i]) > DEBOUNCE_DELAY) {
cmdRisingEdges[i] = false;
if (reading != currentCmdStates[i]) {
currentCmdStates[i] = reading;
if (currentCmdStates[i]) {
cmdRisingEdges[i] = true;
}
}
}
lastCmdStates[i] = reading;
Serial.print(currentButtonStates[i] ? '-':'T');
}
//Serial.println();
}*/
#define DEBOUNCE_RF 300
bool cmdFirstSeen[8] = {0,0,0,0,0,0,0,0};
uint32_t cmdValidTil[8] = {0,0,0,0,0,0,0,0}; //{-10000,-10000,-10000,-10000,-10000,-10000,-10000,-10000};
uint32_t cmdSeenThisScan[8] = {0,0,0,0,0,0,0,0}; //{-10000,-10000,-10000,-10000,-10000,-10000,-10000,-10000};
//bool currentCmdState[8] = {0,0,0,0,0,0,0,0};
//bool currentCmdState[8] = {0,0,0,0,0,0,0,0};
void checkRFTransmissions() { void checkRFTransmissions() {
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN]; // Buffer for incoming message uint8_t buf[RH_ASK_MAX_MESSAGE_LEN]; // Buffer for incoming message
@@ -278,12 +299,50 @@ void checkRFTransmissions() {
if (buf[3]!='b') return; if (buf[3]!='b') return;
if ((buf[4]+buf[5]) != buf[6]) return; // checksum if ((buf[4]+buf[5]) != buf[6]) return; // checksum
if (buf[5] < 49 || buf[5] > 52) return; // valid channel if (buf[5] < 49 || buf[5] > 52) return; // valid channel
//if (buf[0] != 'J' && buf[0] != 'R') return; // valid command if (buf[4] != 'J' && buf[4] != 'R') return; // valid command
if (buf[4] == 'J') int8_t cmd_no = (buf[4] == 'J' ? 0:4) + buf[5]-49;
if (cmdValidTil[cmd_no] < millis()) {
cmdFirstSeen[cmd_no] = true;
}
cmdSeenThisScan[cmd_no] = true;
cmdValidTil[cmd_no] = millis() + DEBOUNCE_RF;
}
for (uint8_t i=0;i<8;i++) {
if (cmdSeenThisScan[i] && i<4) {
changeState(STATE_JOG, i+1);
} else if (cmdFirstSeen[i]) {
if (state == STATE_RUN) {
changeState(STATE_OFF);
} else {
changeState(STATE_RUN, i-4+1);
}
}
cmdFirstSeen[i] = false;
cmdSeenThisScan[i] = false;
}
/*if (buf[4] == 'J')
changeState(STATE_JOG, buf[5]-48); changeState(STATE_JOG, buf[5]-48);
if (buf[4] == 'R') { if (buf[4] == 'R') {
// if running if (state == STATE_RUN) {
if (channel == buf[5]-48) {
if (lastTimeOfCmd[cmd_no] > lastTimeOfCmd+DEBOUNCE_RF) {
changeState(STATE_OFF);
}
} else {
changeState(STATE_OFF);
}
} else {
if (lastTimeOfCmd[cmd_no] > lastTimeOfCmd+DEBOUNCE_RF) {
changeState(STATE_RUN, buf[5]-48);
lastTimeOfCmd[cmd_no] = millis();
}
}*/
/*// if running
if (state == STATE_RUN) { if (state == STATE_RUN) {
// if running and a different channel, just shut off // if running and a different channel, just shut off
if (channel != buf[5]-48){ if (channel != buf[5]-48){
@@ -298,10 +357,10 @@ void checkRFTransmissions() {
} else { } else {
changeState(STATE_RUN, buf[5]-48); changeState(STATE_RUN, buf[5]-48);
rf_runt = millis() + DEBOUNCE_RF; rf_runt = millis() + DEBOUNCE_RF;
} }*/
} //}
} //}
} }
void setup() { void setup() {
@@ -323,15 +382,15 @@ void loop() {
runFSM(); runFSM();
// check if both traps are closed // check if both traps are closed
if(!checkSensor(CMD_F_C) || !checkSensor(CMD_R_C)) { /*if(!checkSensor(CMD_F_C) || !checkSensor(CMD_R_C)) {
digitalWrite(RELAY_4A, HIGH); digitalWrite(RELAY_4A, HIGH);
} else { } else {
digitalWrite(RELAY_4A, LOW); digitalWrite(RELAY_4A, LOW);
} }*/
//Serial.println("loop"); //Serial.println("loop");
Serial.println(); //Serial.println();
//delay(5); //delay(5);
} }