Debounce multiple buttons and leds

Hi all,

I'm trying to debounce multiple push buttons and turn multiple leds on/off.

The first element of the array is the button number. The second, third, fourth and fifth are leds.

int schPins[6][10] = {
        {  8, 3, 0, 0, 0, 0, 0 ,0 ,0 ,0},   // push button 8, led 3, led 0, led 0, led 0, Ledstate, buttonState, lastDebounceTimer, lastButtonstate, reading
        {  9, 5, 0, 0, 0, 0, 0 ,0 ,0 ,0},   // push button 9, led 5, led 0, led 0, led 0, Ledstate, buttonState, lastDebounceTimer, lastButtonstate, reading
        {  10, 6, 3, 0, 0, 0, 0 ,0 ,0 ,0},  // push button 10, led 6, led 3, led 0, led 0, Ledstate, buttonState, lastDebounceTimer, lastButtonstate, reading 
        };       
int schCount = 3;   

int ledPins[3] = {3, 5, 6};
int ledCount = 3;

unsigned long debounceDelay = 50; 

void setup() {
  Serial.begin(9600);
  for(int i = 0; i < schCount; i++){
    pinMode(schPins[i][0], INPUT);
    schPins[i][5] = HIGH;
    schPins[i][8] = LOW;
  }
  for(int i = 0; i < ledCount; i++){
    pinMode(ledPins[i], OUTPUT);
  }
}

void loop() {
  for(int i = 0; i < schCount; i++){
    schPins[i][9] = digitalRead(schPins[i][0]);
      if (schPins[i][9] != schPins[i][8]) {
        // reset the debouncing timer
        schPins[i][7] = millis();
      }

    if ((millis() - schPins[i][7]) > debounceDelay) {
      
      if (schPins[i][9] != schPins[i][6]) {
        
        schPins[i][6] = schPins[i][9];
        // only toggle the LED if the new button state is HIGH
        if (schPins[i][6] == HIGH) {
          //Serial.println(schPins[i][5]);
          schPins[i][5] = !schPins[i][5];
        }
      }
    }
    for(int y = 1; y < 5; y++){
        digitalWrite(schPins[i][y], schPins[i][5]);
        Serial.print("i= ");
        Serial.print(i);
        Serial.print(" y= ");
        Serial.print(y);
        Serial.print(" Waarde=");
        Serial.print(schPins[i][y]);
        Serial.print(" - ");
        Serial.println(schPins[i][5]);
    }
    schPins[i][8] = schPins[i][9];
  } 
}

It give some strange behavior. Is this the right way or is there a better way.

I've not read the details but You should define a struct to represent a button, associated leds and status etc so that you get meaningful variable names rather than weird indexes into your array.

Then have an array of such struct

Your code will be more readable

Thank you for de reply. I thought it was not that difficult. But I will take a look at STRUCT.

Oke, I changed the code...

It's working but I down want to repeat the code (I have 25 push buttons). Any suggestion?

struct BUTTONS {
  int button;
  int led1;
  int led2;
  int led3;
  int led4;
  int curstate;
  int btnstate;
  int btntimer;
  int lbtnstate;
  int btnreading;  
};
typedef struct BUTTONS Button;

Button b1 = {25, 3, 5, 6, 0, HIGH, 0 ,0 ,LOW ,0};
Button b2 = {31, 6, 0, 0, 0, HIGH, 0 ,0 ,LOW ,0};

unsigned long debounceDelay = 10; 

void setup() {
  Serial.begin(9600);
  
    pinMode(25, INPUT);
    pinMode(31, INPUT);
    
    pinMode(3, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
}

void loop() {
    b1.btnreading= digitalRead(b1.button);
    b2.btnreading= digitalRead(b2.button);
    
    if (b1.btnreading != b1.lbtnstate) {
      Serial.print("Change1 - 1");
      b1.btntimer = millis();
      if ((millis() - b1.btntimer) > debounceDelay) {
        Serial.print("Change1 - 2");
        if (b1.btnreading != b1.btnstate) {
          Serial.print("Change1 - 3");
          b1.btnstate = b1.btnreading;
          // only toggle the LED if the new button state is HIGH
          if (b1.btnstate == HIGH) {
            b1.curstate = HIGH;
            Serial.println(" Just pushed b1.");
          }
        }
      }
      digitalWrite(b1.led1, b1.curstate);
      digitalWrite(b1.led2, b1.curstate);
      digitalWrite(b1.led3, b1.curstate);
      digitalWrite(b1.led4, b1.curstate);
      b1.lbtnstate = b1.btnstate;
    } 
    
    if (b2.btnreading != b2.lbtnstate) {
      Serial.print("Change2 - 1");
      b2.btntimer = millis();
      if ((millis() - b2.btntimer) > debounceDelay) {
        Serial.print("Change2 - 2");
        if (b2.btnreading != b2.btnstate) {
          Serial.print("Change2 - 3");
          b2.btnstate = b2.btnreading;
          // only toggle the LED if the new button state is HIGH
          if (b2.btnstate == HIGH) {
            b2.curstate = !b2.curstate;
            Serial.print(b2.curstate);
            Serial.println(" Just pushed b2");
          }
        }
      }
      digitalWrite(b2.led1, b2.curstate);
      digitalWrite(b2.led2, b2.curstate);
      digitalWrite(b2.led3, b2.curstate);
      digitalWrite(b2.led4, b2.curstate);
      b2.lbtnstate = b2.btnstate;
    }
 }

Any suggestion?

Arrays! You CAN make arrays of structs just like arrays of ints.

Ofcourse, Stupid me... It works now. Thanks!!

Good

you could also save some memory - if it's needed - by using bytes and bool instead of int (or bit fields for the states) in the struct.

One cool thing about structs is that you can move functions into the struct. One advantage of this is that you don't have to use array indexes all the time. Just call buttons[2].myFunction(), and myFunction will work on the leds and what not that it belongs to.

Hi everyone,

Since it is the only topic I've found concerning multiple leds with the debounce function.
masterg_nl would you mind to share the last version of the code you used with the arrays of structs?
I'd like to have a look at it to understand how you did it.

Thanks a lot