I am having trouble implementing debounce into my code, the aim of which is to have five buttons, A to E, and each button has a corresponding LED, LED A, LED B, LED C etc.
I want the code to work so when I press a button it will turn on its LED and the LED will stay on, if I then press a different button, it will turn off the previously on LED and its own LED on.
If a button is pressed twice, it will turn off all LEDs.
After a lot of reading and YouTube I have achieved this with the below code, but now I have connected a breadboard to test it I am getting issues with debouncing. The code works but the switching is intermittent.
It seem to me that each example of debouncing code I have tried conflicts with my existing code and due to my limited knowledge of Arduino I am struggling to find a solution that keeps my original code working.
Is there an obvious way of doing it that I am missing?
int buttonPin1 = 1;
int buttonPin2 = 2;
int buttonPin3 = 3;
int buttonPin4 = 4;
int buttonPin5 = 5;
int ledPin1 = 6;
int ledPin2 = 7;
int ledPin3 = 8;
int ledPin4 = 9;
int ledPin5 = 10;
int ledState1 = LOW;
int ledState2 = LOW;
int ledState3 = LOW;
int ledState4 = LOW;
int ledState5 = LOW;
int buttonState1;
int lastButtonState1 = LOW;
int buttonState2;
int lastButtonState2 = LOW;
int buttonState3;
int lastButtonState3 = LOW;
int buttonState4;
int lastButtonState4 = LOW;
int buttonState5;
int lastButtonState5 = LOW;
int dt = 50;
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
digitalWrite(ledPin1, HIGH); //turn LED1 off
digitalWrite(ledPin2, HIGH); //turn LED2 off
digitalWrite(ledPin3, HIGH); //turn LED3 off
digitalWrite(ledPin4, HIGH); //turn LED4 off
digitalWrite(ledPin5, HIGH); //turn LED5 off
ledState1 = HIGH; //set LED1 state to off
ledState2 = HIGH; //set LED2 state to off
ledState3 = HIGH; //set LED3 state to off
ledState4 = HIGH; //set LED4 state to off
ledState5 = HIGH; //set LED5 state to off
dt;
}
void loop() {
///BUTTON 1///
buttonState1 = digitalRead(buttonPin1); //get new button1 state
if (lastButtonState1 == HIGH && buttonState1 == LOW) { //if the button1 is pressed
if (ledState1 == HIGH) { //if LED1 is off
digitalWrite(ledPin1, LOW); //turn LED1 on
digitalWrite(ledPin2, HIGH); //turn LED2 off
digitalWrite(ledPin3, HIGH); //turn LED3 off
digitalWrite(ledPin4, HIGH); //turn LED4 off
digitalWrite(ledPin5, HIGH); //turn LED5 off
ledState1 = LOW; //set LED1 state to on
ledState2 = HIGH; //set LED2 state to off
ledState3 = HIGH; //set LED3 state to off
ledState4 = HIGH; //set LED4 state to off
ledState5 = HIGH; //set LED5 state to off
}
else { //or
digitalWrite(ledPin1, HIGH); //turn LED1 off
ledState1 = HIGH; //change LED1 state to off
}
}
lastButtonState1 = buttonState1; //set old button1 state
///BUTTON 2///
buttonState2 = digitalRead(buttonPin2); //get new button2 state
if (lastButtonState2 == HIGH && buttonState2 == LOW) { //if the button2 is pressed
if (ledState2 == HIGH) { //if LED2 is off
digitalWrite(ledPin1, HIGH); //turn LED1 off
digitalWrite(ledPin2, LOW); //turn LED2 on
digitalWrite(ledPin3, HIGH); //turn LED3 off
digitalWrite(ledPin4, HIGH); //turn LED4 off
digitalWrite(ledPin5, HIGH); //turn LED5 off
ledState1 = HIGH; //set LED1 state to off
ledState2 = LOW; //set LED2 state to on
ledState3 = HIGH; //set LED3 state to off
ledState4 = HIGH; //set LED4 state to off
ledState5 = HIGH; //set LED5 state to off
}
else { //or
digitalWrite(ledPin2, HIGH); //turn LED2 off
ledState2 = HIGH; //change LED2 state to off
}
}
lastButtonState2 = buttonState2; //set old button2 state
///BUTTON 3///
buttonState3 = digitalRead(buttonPin3); //get new button3 state
if (lastButtonState3 == HIGH && buttonState3 == LOW) { //if the button3 is pressed
if (ledState3 == HIGH) { //if LED3 is off
digitalWrite(ledPin1, HIGH); //turn LED1 off
digitalWrite(ledPin2, HIGH); //turn LED2 off
digitalWrite(ledPin3, LOW); //turn LED3 on
digitalWrite(ledPin4, HIGH); //turn LED4 off
digitalWrite(ledPin5, HIGH); //turn LED5 off
ledState1 = HIGH; //set LED1 state to off
ledState2 = HIGH; //set LED2 state to off
ledState3 = LOW; //set LED3 state to on
ledState4 = HIGH; //set LED4 state to off
ledState5 = HIGH; //set LED5 state to off
}
else { //or
digitalWrite(ledPin3, HIGH); //turn LED3 off
ledState3 = HIGH; //change LED3 state to off
}
}
lastButtonState3 = buttonState3; //set old button3 state
///BUTTON 4///
buttonState4 = digitalRead(buttonPin4); //get new button4 state
if (lastButtonState4 == HIGH && buttonState4 == LOW) { //if the button4 is pressed
if (ledState4 == HIGH) { //if LED4 is off
digitalWrite(ledPin1, HIGH); //turn LED1 off
digitalWrite(ledPin2, HIGH); //turn LED2 off
digitalWrite(ledPin3, HIGH); //turn LED3 off
digitalWrite(ledPin4, LOW); //turn LED4 on
digitalWrite(ledPin5, HIGH); //turn LED5 off
ledState1 = HIGH; //set LED1 state to off
ledState2 = HIGH; //set LED2 state to off
ledState3 = HIGH; //set LED3 state to off
ledState4 = LOW; //set LED4 state to on
ledState5 = HIGH; //set LED5 state to off
}
else { //or
digitalWrite(ledPin4, HIGH); //turn LED4 off
ledState4 = HIGH; //change LED4 state to off
}
}
lastButtonState4 = buttonState4; //set old button4 state
///BUTTON 5///
buttonState5 = digitalRead(buttonPin5); //get new button5 state
if (lastButtonState5 == HIGH && buttonState5 == LOW) { //if the button5 is pressed
if (ledState5 == HIGH) { //if LED5 is off
digitalWrite(ledPin1, HIGH); //turn LED1 off
digitalWrite(ledPin2, HIGH); //turn LED2 off
digitalWrite(ledPin3, HIGH); //turn LED3 off
digitalWrite(ledPin4, HIGH); //turn LED4 off
digitalWrite(ledPin5, LOW); //turn LED5 on
ledState1 = HIGH; //set LED1 state to off
ledState2 = HIGH; //set LED2 state to off
ledState3 = HIGH; //set LED3 state to off
ledState4 = HIGH; //set LED4 state to off
ledState5 = LOW; //set LED5 state to on
}
else { //or
digitalWrite(ledPin5, HIGH); //turn LED5 off
ledState5 = HIGH; //change LED5 state to off
}
}
lastButtonState5 = buttonState5; //set old button5 state
dt; //delay
}
