two button debounce

Hi, I want to make debounce for two buttons. So turn on Red led or Green led knowing which button have pressed I only have a code for 1 button.

I think that I need a array for two several buttons.

Could anyone help me to improve my code two buttons or more ?

int boton = 11;
int led = 13;

boolean estadoAnterior = LOW;
boolean estadoActual = LOW;
boolean ledOn = false;
int cont=0;
void setup()
{
  pinMode(boton, INPUT);
  pinMode(led, OUTPUT);
  Serial.begin(9600);
}

boolean rebote(boolean eAnterior)
{
  boolean eActual = digitalRead(boton);
  if (eAnterior != eActual)
  {
    delay(5);
    eActual = digitalRead(boton);
  }
  return eActual;
}

void loop()
{
  estadoActual = rebote(estadoAnterior);
  if (estadoAnterior == LOW && estadoActual == HIGH)
  {
    ledOn = !ledOn;
    cont++;
    Serial.println(cont);
  }
  estadoAnterior = estadoActual;
  
  digitalWrite(led, ledOn);

}

I think you want to do one button several led.

const int buton = 2;
const int led1 = 3;
const int led2 = 4;
const int led3 = 5;

int buttonstatus = 0;
int value = 0;
void setup()
{

//in-out pin 
pinMode(buton, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);

}

void loop()
{
//button value written to button variable
buttonstatus = digitalRead(buton);
// press button and value 0

if ( (buttonstatus == HIGH) && (value == 0) )

{
// 1st press.

digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
value=1;
delay(10);
}

//draw button 
if ( (buttonstatus == LOW) && (value == 1) )

{

// 1st press button and draw button

value=2;
delay(10);
}


if ( (buttonstatus == HIGH) && (value == 2) )

{


digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
value=3;
delay(10);
}


if ( (buttonstatus == LOW) && (value == 3) )

{



value=4;
delay(10);
}


if ( (buttonstatus == HIGH) && (value == 4) )

{


digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
value=5;
delay(10);
}


if ( (buttonstatus == LOW) && (value == 5) )

{


value=6;
delay(10);
}


if ( (buttonstatus == HIGH) && (value == 6) )

{
.

digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);

value=7;
delay(10);
}


if ( (buttonstatus == LOW) && (value == 7) )

{


value=0;
delay(10);
}

}

Not I want debounce two or more buttons

First, download this library

The sketch:

#include <EdgeDebounceLite.h>
EdgeDebounceLite debounce;

const int buton1 = 2;
const int buton2 = 3;
const int led1 = 4;
const int led2 = 5;
const int led3 = 6;

void setup() {
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
}

void loop() {
  if (debounce.pin(button1) digitalWrite(led1, HIGH);
  else                      digitalWrite(led1, LOW);

  if (debounce.pin(button2) digitalWrite(led2, HIGH);
  else                      digitalWrite(led2, LOW);
}

Jacques

Also posted at c - debounce with two button to turn on different leds with Arduino - Stack Overflow

boolean rebote(boolean eAnterior)
{
  boolean eActual = digitalRead(boton);
  if (eAnterior != eActual)
  {
    delay(5);                                //<<< delay for 5 seconds
    eActual = digitalRead(boton);   //<<< then take the next read as debounced?
  }
  return eActual;
}

This code does not debounce. in the IDE, open Example/Digital/Debounce.ino