4 button's 4 leds debounce

Hello friends, I've found this debounce code online and have tried for hours to make it work x4

however I come up with several errors this is my first time doing this and when knowing how I plan on using my arduino for my house's lighting. here is my code wich makes 1 Light work but unsure how to proceed to the other Led's.

const int buttonPin2 = 2;
const int ledPin3 = 3;
const int buttonPin4 = 4;
const int ledPin5 = 5;
const int buttonPin8 = 8;
const int ledPin9 = 9;
const int buttonPin10 = 10;
const int ledPin11 = 11;

int ledState3 = HIGH; //the current state of the output pin
int buttonState2; //the current reading from the input pin
int lastButtonState2 = LOW; //the previous reading from the input pin

int ledState5 = LOW; //the current state of the output pin
int buttonState4; //the current reading from the input pin
int lastButtonState4 = HIGH; //the previous reading from the input pin

int ledState9 = LOW; //the current state of the output pin
int buttonState8; //the current reading from the input pin
int lastButtonState8 = HIGH; //the previous reading from the input pin

int ledState11 = LOW; //the current state of the output pin
int buttonState10; //the current reading from the input pin
int lastButtonState10 = HIGH; //the previous reading from the input pin

unsigned long lastDebounceTime2 = 0;
unsigned long debounceDelay2 = 50;

unsigned long lastDebounceTime4 = 0;
unsigned long debounceDelay4 = 50;

unsigned long lastDebounceTime8 = 0;
unsigned long debounceDelay8 = 50;

unsigned long lastDebounceTime10 = 0;
unsigned long debounceDelay10 = 50;

void setup() {
pinMode (buttonPin2, INPUT);
pinMode (ledPin3, OUTPUT);
pinMode (buttonPin4, INPUT);
pinMode (ledPin5, OUTPUT);
pinMode (buttonPin8, INPUT);
pinMode (ledPin9, OUTPUT);
pinMode (buttonPin10, INPUT);
pinMode (ledPin11, OUTPUT);

digitalWrite(ledPin3, ledState3);
digitalWrite(ledPin5, ledState5);
digitalWrite(ledPin9, ledState9);
digitalWrite(ledPin11, ledState11);
}

void loop() {
int reading = digitalRead(buttonPin2);

if (reading != lastButtonState2) {
lastDebounceTime2 = millis();
}

if ((millis() - lastDebounceTime2) > debounceDelay2) {

if (reading != buttonState2) {
  buttonState2 = reading;

  if (buttonState2 == HIGH) {
    ledState3 = !ledState3;
  }
}

}
digitalWrite(ledPin3, ledState3);
lastButtonState2 = reading;
}


Thank you in advance.

Back up a bit and do yourself this favor!

Get one button and one LED working, but use these names, or like these as your choice.

const int buttonPinA = 2;
const int ledPinA = 3;

int ledStateA = HIGH;       //the current state of the output pin
int buttonStateA;           //the current reading from the input pin
int lastButtonStateA = LOW; //the previous reading from the input pin

The whole idea of using constants and names is to get away from thinking about the actual pin numbers.

The code above isolates the relationship between names and numbers to one place in you code.

It makes reading the code easier.

Once you get button “A” working, it will be easy to copy and paste the exact same thing and change all ‘A’s to ‘B’ and boom! You are on your way.

There are better ways to do the same thing four times, but at this point it won’t hurt to do it literally and verbosely by copying the code and editing it,

You can look forward to learning about arrays, where multiple things of the same thing can be handled with loops.

HTH

a7

consider

  • using an array of button pins and button states
  • should button pins be configured INPUT_PULLUP?
  • using a sub-function to process a button using an array index
  • read a button into a local variable (as you do)
  • compare that button val to the button state (as you do)
  • update the state (as you do)
  • debounce just requires a short (10 msec) delay
  • check for the appropriate value representing a press (LOW or HIGH)
  • return an indication that a button is pressed
  • digitalWrite (ledPin, ! digitalRead (ledPin));
  • process the buttons using a for loop

Hello
I am using a 20msec timer to scan the current state of a button pin. This current state is compared to an old state and when the state changes the button is either pressed or released. This procedure makes the debouncing of buttons more easy.

Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

My take on debouncing buttons Buttons and other electro-mechanical inputs (introduction)
More examples, including debouncing 4 buttons Buttons and other electro-mechanical inputs (advanced)

I thought it might be prudent to check the OP's code for it even works with one button.

And it works as we found it.

If you aren't a pull-up resistor, INPUT_PULLUP pinMode will fix you up.

With the name changes, here is one button, debounced.

There is a minor adjustment to the logic as well.

I changed the OP's handling of the debounce period to correct a common flaw: There is no reason to delay reacting to a switch opening or closing - the trick is to delay reacting again until the debounce period.

Pushbuttons, unlike other signals that may need full debouncing and/or deglitching, do not capriciously indicate opening when you have them press, nor closing when you don't. So it makes no sense to delay reaction to either.

const int buttonPinA = 2;
const int ledPinA = 3;

int ledStateA = HIGH;        //the current state of the output pin
int buttonStateA;            //the current reading from the input pin
int lastButtonStateA = LOW;  //the previous reading from the input pin

unsigned long lastDebounceTimeA = 0;

// one constant for all buttins is probably OK
// this absurdly large "debounce" constant dose not impact repsonse time!
// use 50 in real deployment, here just for illustrative purposes I use 500

const unsigned long theDebounceDelay = 500;

void setup() {
pinMode (buttonPinA, INPUT_PULLUP);
pinMode (ledPinA, OUTPUT);

digitalWrite(ledPinA, ledStateA);
}

void loop() {
  int reading = digitalRead(buttonPinA);

  if ((millis() - lastDebounceTimeA) > theDebounceDelay) {

    if (reading != buttonStateA) {
      buttonStateA = reading;

      if (buttonStateA == LOW) {      // I like the press, not the release to take action
        ledStateA = !ledStateA;
      }

      lastDebounceTimeA = millis();   // don't act on the switch again until the debounce period has passed
    }
  }
  digitalWrite(ledPinA, ledStateA);
  lastButtonStateA = reading;
}

And of course I invite you to tinker with it for yourselves at

HTH

a7

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