Hi, I feel like the error is staring me right in the face. I just need help to see it.
Here the test code I made for this state machine.
#include <EdgeDebounceLite.h>
EdgeDebounceLite debounce;
// SWITCH MODES
enum SwitchStates {IS_OPEN, IS_RISING, IS_CLOSED, IS_FALLING };
SwitchStates switchState[2] = {IS_OPEN, IS_OPEN }; // both switches start in is open
// SWITCH MODES
enum SwitchModes {MY1_PULLUP, MY1_PULLDOWN}; //the two possible modes the pins could be in
SwitchModes switchMode[2] = {MY1_PULLUP, MY1_PULLUP}; //
byte buttonPins[2] {3, 4};
byte StatusLEDS[3] {6, 7, 8};
void switchMachine(byte i) {
byte pinIs = debounce.pin(buttonPins[i]);
if ((switchMode[i]) == MY1_PULLUP){pinIs = !pinIs; //Reverse the value read if the switch is in PULLUP mod
switch (switchState[i]) {
case IS_OPEN: { //State is IS_OPEN
if(pinIs == HIGH) //If the pin is HIGH
switchState[i] = IS_RISING; //We just changed form LOW to HIGH: State is now IS_RISING
break; //Get out of switch
}
case IS_RISING: { //State is IS_RISING
switchState[i] = IS_CLOSED; //It is not rising anymore, State is now IS_CLOSED
break; //Get out of switch
}
case IS_CLOSED: { //State is IS_CLOSED
if(pinIs == LOW) //If the pin is LOW
switchState[i] = IS_FALLING; //We just changed from HIGH to LOW: State is now IS_FALLING
break; //Get out of switch
}
case IS_FALLING: { //State is IS_FALLING
switchState[i] = IS_OPEN; //It is not falling anymore, State is now IS_OPEN
break;
}
}
}
}
// BUTTON FLAGS
bool hasRout1_strtd() { //Routine 1 active?
switchMachine(0); //Read switch 0
if (switchState[0] == IS_FALLING) //If it is in the state IS_FALLING
return true; //R1 started, return true
else //If not
return false; //return false
}
bool hasRout2_strtd() { //Routine 2 active?
switchMachine(1); //Read switch 1
if (switchState[1] == IS_FALLING) //If it is in the state IS_FALLING
return true; //R2 started, return true
else //If not
return false; //return false
}
void setup() {
for (byte i = 0 ; i <2 ; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
Serial.begin(9600);
}
void loop() {
for ( byte i = 0 ; i < 2 ; i++) {
if (hasRout1_strtd()) {
Serial.print(F("Button on pin "));
Serial.print(buttonPins[i]);
Serial.println(F(" was pressed."));
}
if (hasRout2_strtd()) {
Serial.print(F("Button on pin "));
Serial.print(buttonPins[i]);
Serial.println(F(" was pressed."));
}
}
}
The switch state machine drives the button flags. Debounce works, I have tested it with leds and so on, the buttons work and are in the right configuration, I have extensively tested them (they have physical pull up resistors.
debounce.pin(buttonPins[i]); //replaces digitalWrite
pinIs// is the variable for the state 1 or 0 of the pin
I think the error is in the state machine logic in itself or possibly how it iterates. When the switch and case makes it way down to a pinIs == LOW, the state is == FALLING. This makes the button flag true and so on. What am I missing here?
running the test in serial monitor, pressing one button causes both signals to be read, here's an example.
Let me know what you all think, kindly!