Sporadic pin select in button press State Machine

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!

Take a view into the libary used simply.

Hi Paul I did do that and from the reference. Nothing is immediately jumping out to me .

I also am a little confused on what is happening here:

for (byte i = 1; i <= MYsensitivity; i++) pinStatus = (pinStatus << 1) | digitalRead(pin);
  } while ((pinStatus != debounceDontCare) && (pinStatus != 0xffffffff));

Could you kindly shed a light on what is causing this?

Obviously the button library is 'thinking; that the switch states are constantly falling. Has the OP considered weather using the rising or falling pin state a good option over using RISEN or FALLEN?

what happens when the OP does not use the library to check pin state?

  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 you're pressing button 1 here at the point when it's checking button 2, hasRout1_strtd() is true because button1 is pressed but i = 2 because it's now checking button 2.

Hi, Yes I was being silly, with the reverse state the flags do work as intended! thanks all.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.