Newbie debounce problem.

Greetings everyone, I’m new with arduino and having troubles with input buttons. After hard googling, I found that I have to use “debouncing” to catch signal correctly. After a while, I found example of debounce function, which showed by Jeremy Blum. It works perfectly, but whenever I’m trying to use it for multi-input buttons, first input & output works, others no.
Here’s the both code, first which works perfectly on single input and second (which I modified to for as multi-input) which doesn’t seem to work fine.

original version:

/*
 *  Switch multiple LED's with button.
 */
 
int switch_buttons = 7;
int switch_leds = 0;
boolean ledOn = false;
boolean lastButton = LOW;
boolean currentButton = LOW;

void setup() {
  
    pinMode(7, INPUT);
    
    pinMode(0, OUTPUT);
}

boolean debounce(boolean last, int switchPin)
{
    boolean current = digitalRead(switchPin);
    if(last != current)
    {
        delay(5);
        current = digitalRead(switchPin);
    }
    
    return current;
}

void loop()
{
        currentButton = debounce(lastButton, switch_buttons);
        if(currentButton == HIGH && lastButton == LOW)
        {
            ledOn = !ledOn;
            digitalWrite(switch_leds, ledOn);
        }
        lastButton = currentButton;
    }
}

modified version:

/*
 *  Switch multiple LED's with button.
 */
 
int switch_buttons[] = {7, 8, 9}; //INPUT pins are at 7, 8 and 9 pins.
int switch_leds[] = {0, 1, 2}; // LEDs are at 0, 1 and 2 pins.

// some variables which is required for bouncing.
boolean ledOn[3] = {false};
boolean lastButton[3] = {LOW};
boolean currentButton[3] = {LOW};

void setup() {
  
    pinMode(7, INPUT);
    pinMode(8, INPUT);
    pinMode(9, INPUT);
    
    pinMode(0, OUTPUT);
    pinMode(1, OUTPUT);
    pinMode(2, OUTPUT);
}

boolean debounce(boolean last, int switchPin)
{
    boolean current = digitalRead(switchPin);
    if(last != current)
    {
        delay(5);
        current = digitalRead(switchPin);
    }
    
    return current;
}

void loop()
{
    for(int i = 0; i < 3; i++)
    {
        currentButton[i] = debounce(lastButton[i], switch_buttons[0]);
        if(currentButton[i] == HIGH && lastButton[i] == LOW)
        {
            ledOn[i] = !ledOn[i];
            digitalWrite(switch_leds[0], ledOn[i]);
        }
        lastButton[i] = currentButton[i];
    }
}

what am I doing wrong?
Regards.

Something doesn’t seem right, unless it was intentional.

currentButton = debounce(lastButton, switch_buttons[0]);
if(currentButton == HIGH && lastButton == LOW)
* {*
ledOn = !ledOn*;*
digitalWrite(switch_leds[0], ledOn*);
_ }[/quote]
And these are missing a few things,
> boolean ledOn[3] = {false};
> boolean lastButton[3] = {LOW};
> boolean currentButton[3] = {LOW};*

Better?_

I don't get it what you mean by "missing". I'm just looking for right way to debounce multiple buttons.

Regards.

..[0] shall be probably ..[i] ??

@HM: the array initializers are ok in C, I think.

You have a debounce example that is provided to you by the arduino software. It can debounce a single button, BUT, if you use arrays like you've done here, it will work for multiple buttons/LEDs.

Your code is more of a latch and less of a debounce sketch.

@pito, Yes, that was my bad excuse me. But when I put there "i", only first switch turns all led's ON/OFF, others doesn't affect anything.
@HazardsMind, I've tried it without success, I'll re-try it more carefully. Will post results here, thanks for replies!

Regards.

...
    for(int i = 0; i < 3; i++)
    {
        currentButton[i] = debounce(LOW, switch_buttons[i]);
        if(currentButton[i] == HIGH)
        {
            ledOn[i] = !ledOn[i];
            digitalWrite(switch_leds[i], ledOn[i]);
        }
    }
...

Yes, I found that problem too, I was missing another "i" there :slight_smile:
Problem solved, thank you guys!