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.
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;
}
}