I have two buttons defined as follows
const int buttonPinSwitch = 0;
const int buttonPinCycle = 3;
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonStateSwitch; // the current readingSwitch from the input pin
int buttonStateCycle;
int lastButtonStateSwitch = LOW; // the previous readingSwitch from the input pin
int lastButtonStateCycle = LOW;
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTimeSwitch = 0; // the last time the output pin was toggled
unsigned long lastDebounceTimeCycle = 0;
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
In my main loop I have the following code
// read the state of the switch into a local variable:
int readingSwitch = digitalRead(buttonPinSwitch);
int readingCycle = digitalRead(buttonPinCycle);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (readingSwitch != lastButtonStateSwitch) {
// reset the debouncing timer
lastDebounceTimeSwitch = millis();
}
if ((millis() - lastDebounceTimeSwitch) > debounceDelay) {
// whatever the readingSwitch is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (readingSwitch != buttonStateSwitch) {
buttonStateSwitch = readingSwitch;
// only toggle the LED if the new button state is HIGH
if (buttonStateSwitch == HIGH) {
ledState = !ledState;
}
}
}
//==== Start for button 2.
if (readingCycle != lastButtonStateCycle) {
// reset the debouncing timer
lastDebounceTimeCycle = millis();
}
if ((millis() - lastDebounceTimeCycle) > debounceDelay) {
// whatever the readingCycle is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (readingCycle != buttonStateCycle) {
buttonStateCycle = readingCycle;
// only toggle the LED if the new button state is HIGH
if (buttonStateCycle == HIGH) {
ledState = !ledState;
}
}
}
//==== End
If I push the 1st button, then the ledState is toggled. However while the same thing should happen for button two, when I push it nothing happens. It is like nothing is happening when button 2 is pushed or not pushed. I have tried using pull-up and pull-down resistors but it make no difference. The button does work as when I use the second button configuration in place of that for button 1, it works.
I think that there is some obvious step in the logic that I am missing.