Need to add which button pressed variable into debouncing routine.

I honestly do my best to search but I come up short sometimes due to the fact that I'm not quite sure what to search for. I do try though. I just made a button array board for my project. I borrowed code for debouncing routine and tested all buttons with it and it's working perfectly. :slight_smile:

My problem is I need a way to plug which button is pressed into the debouncing routine. What would you call that? I wish we could just do digitalRead on all the pins when the debounce state is at idle. Right now I have it reading upSwitchPin only. I have 7 buttons as you can see from code up, down, left, right, edit, stop, run. BTW, I have it printing 1's on the lcd just to make sure its working.

If there's a better way to do this I'm all ears. Thanks alot!

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
int backlight = 13;
int upSwitchPin = 22;
int downSwitchPin = 24;
int leftSwitchPin = 26;
int rightSwitchPin = 28;
int editSwitchPin = 30;
int stopSwitchPin = 32;
int runSwitchPin = 34;

typedef enum DEBOUNCE_STATE
{
  DEBOUNCE_STATE_IDLE,
  DEBOUNCE_STATE_CHECK,
  DEBOUNCE_STATE_RELEASE
} debounceState_t;

typedef	enum SWITCH
{
	SWITCH_NONE,
	SWITCH_1,	
	SWITCH_2
}	switch_t;


//********CONSTANTS********
#define DEBOUNCE_PERIOD_MIN 50

// Switch debounce state machine state variable
debounceState_t debounceState;
// Switch debounce timer
long lastDebounceTime;
// Switch press status
switch_t switchStatus;
// Seconds timer
int timerSeconds;


void setup()
{
pinMode (upSwitchPin, INPUT);
pinMode (downSwitchPin, INPUT);
pinMode (leftSwitchPin, INPUT);
pinMode (rightSwitchPin, INPUT);
pinMode (editSwitchPin, INPUT);
pinMode (stopSwitchPin, INPUT);
pinMode (runSwitchPin, INPUT);
pinMode (backlight, OUTPUT);
digitalWrite(backlight, HIGH);
lcd.begin(20, 4);
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("BUTTON TEST");
}
void loop()
{

  // Simple switch debounce state machine (for switch #1 (both analog & digital
	// switch supported))
  switch (debounceState)
  {
  case DEBOUNCE_STATE_IDLE:
    // No valid switch press
    switchStatus = SWITCH_NONE;
   
    // If switch #1 is pressed
		
			if (digitalRead(upSwitchPin) == LOW)
		        {
				// Intialize debounce counter
				lastDebounceTime = millis();
				// Proceed to check validity of button press
				debounceState = DEBOUNCE_STATE_CHECK;
			}	
    break;

  case DEBOUNCE_STATE_CHECK:
		
			// If switch #1 is still pressed
			if (digitalRead(upSwitchPin) == LOW)
		
			{
				// If minimum debounce period is completed
				if ((millis() - lastDebounceTime) > DEBOUNCE_PERIOD_MIN)
				{
					// Proceed to wait for button release
					debounceState = DEBOUNCE_STATE_RELEASE;
				}
			}
			// False trigger
			else
			{
				// Reinitialize button debounce state machine
				debounceState = DEBOUNCE_STATE_IDLE; 
			}
    break;

  case DEBOUNCE_STATE_RELEASE:
		
     if (digitalRead(upSwitchPin) == HIGH)
		{
      // Valid switch 1 press
      switchStatus = SWITCH_1;
      
      lcd.print("1");
      // Reinitialize button debounce state machine
      debounceState = DEBOUNCE_STATE_IDLE; 
    }
    break; 
  
  } 
}

Debouncing a switch simply means ignoring any state changes for a short period of time. If, after you determine that a switch was pressed, you do something that takes time, debouncing is not necessary (as it's already done).

If not, a simple delay(10) will pause the code long enough for any switches to stop bouncing.

It isn't clear from your snippet what happens later, so it isn't clear that you are not trying to solve a non-existent problem.

Nice! I'll try it thanks.

Your method is working fine(delay(200) though) and I 'may' be able to make that work although, I can't use delay() in my sketch. If other process weren't happening at same time it would be fine. I was thinking about only being able to access menu when main process was at IDLE but I need real time editing etc.

The code above isn't just a snipped its a whole sketch. The only thing the sketch does as it was when I posted is when up button is pressed it prints "1" to the lcd screen.

What I'm trying to do is instead of using upSwitchPin as the variable to start debouncing routine I'd like to use a variable that holds which button was pressed.

check to see if any buttons are pressed
when a button is pressed save that button in a variable
start debouncing routine on the button pressed

200 milliseconds of bouncing for a switch would have me bouncing that switch into the nearest trashcan.

What is the rest of the sketch doing? I see some writing to an LCD, which is ssslllooowww. That alone may take more time than a switch will bounce.

Reading an array of switches in a loop takes time. Perhaps more than a switch bounces.

You're faster than me haha!

The code above isn't just a snipped its a whole sketch. The only thing the sketch does as it was when I posted is when up button is pressed it prints "1" to the lcd screen.

What I'm trying to do is instead of using upSwitchPin as the variable in the debouncing routine I'd like to use a variable that holds which button was pressed.

check to see if any buttons are pressed
when a button is pressed save that button in a variable
start debouncing routine on the button pressed

I could probably leave it at 150ms and it would work. Yes, crap switches lol. Without any delay it fills the whole screen up with one press...

EDIT:I went about it a completely different way and its working. The debounce time is only 10 ms and its working fine. Weird right?

EDIT:I went about it a completely different way and its working. The debounce time is only 10 ms and its working fine. Weird right?

Depends on how you are now going about it. I suspect that the change is to detect edges (pressed to released or released to pressed) rather than flats (pressed or released).

Dude, you friggin rock! Spot on. :slight_smile: