Debounce (Turn ON and OFF 3 LED with one push button

please help i have 3 LED and one push button, i want to turn on and off LED with only one push button

my push button attach to pin 2
my 3 LED attach to pin 13,12,11

this is how i want to happen

when first press LED1 will be ON
when second press LED1 will OFF and LED2 will be ON
when third press LED1 remain OFF, LED2 will be OFF and finally LED3 will be on

this is what i want please i really need help.

this is my first try from example of debounce

/*
  Debounce

  Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
  press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
  a minimum delay between toggles to debounce the circuit (i.e. to ignore
  noise).

  The circuit:
   LED attached from pin 13 to ground
   pushbutton attached from pin 2 to +5V
   10K resistor attached from pin 2 to ground

   Note: On most Arduino boards, there is already an LED on the board
  connected to pin 13, so you don't need any extra components for this example.


  created 21 November 2006
  by David A. Mellis
  modified 30 Aug 2011
  by Limor Fried
  modified 28 Dec 2012
  by Mike Walters

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Debounce
*/

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin1 = 13;      // the number of the LED pin
const int ledPin2 = 12;      // the number of the LED pin
const int ledPin3 = 11;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  // set initial LED state
  digitalWrite(ledPin1, ledState);

}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;

      }
    }
  }

  // set the LED:
  digitalWrite(ledPin1, ledState);

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
  digitalWrite(ledPin2, !ledState); // here i added this code but only LED1 and LED2 will be ON vice versa



}

It is not completely clear to me from your description what LEDs should be on and when, but I guess it is something like this:

initial state: LED1 off ; LED2 off ; LED 3 off ;
press 1: LED1 on ; LED2 off ; LED 3 off ;
press 2: LED1 off ; LED2 on ; LED 3 off ;
press 3: LED1 off ; LED2 off ; LED 3 on ;
Repeat;

In that case, uses your debounce logic to cycle a counter 0,1,2,3,0,1,2,3 etc.
and then do something like, the example for each value of counter:

if (counter == 2 ) {
digitalWrite( ledPin1 , LOW ) ;
digitalWrite( ledPin1 , HIGH ) ;
digitalWrite( ledPin1 , LOW ) ;

}

Then optimise it all.

1 Like

Hi !

Take this and add few lines: https://www.arduino.cc/en/Tutorial/StateChangeDetection

/*
   State change detection (edge detection)

  Often, you don't need to know the state of a digital input all the time,
  but you just need to know when the input changes from one state to another.
  For example, you want to know when a button goes from OFF to ON.  This is called
  state change detection, or edge detection.

  This example shows how to detect when a button or button changes from off to on
  and on to off.

  The circuit:
  * pushbutton attached to pin 2 from +5V
  * 10K resistor attached to pin 2 from ground
  * LED attached from pin 13 to ground (or use the built-in LED on
    most Arduino boards)

  created  27 Sep 2005
  modified 30 Aug 2011
  by Tom Igoe

 This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/ButtonStateChange

  */

// this constant won't change:
 const int  buttonPin = 2;    // the pin that the pushbutton is attached to
 const int ledPin1 = 13;       // the pin that the LED is attached to
 const int ledPin2 = 12;       // added
 const int ledPin3 = 11;       // added
// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
   // initialize the button pin as a input:
   pinMode(buttonPin, INPUT);
   // initialize the LED as an output:
   pinMode(ledPin1, OUTPUT); 
   pinMode(ledPin2, OUTPUT); //added
   pinMode(ledPin3, OUTPUT); //added
   // initialize serial communication:
   Serial.begin(9600);
}


void loop() {
   // read the pushbutton input pin:
   buttonState = digitalRead(buttonPin);

   // compare the buttonState to its previous state
   if (buttonState != lastButtonState) {
     // if the state has changed, increment the counter
     if (buttonState == HIGH) {
       // if the current state is HIGH then the button
       // wend from off to on:
       buttonPushCounter++;
       Serial.println("on");
       Serial.print("number of button pushes:  ");
       Serial.println(buttonPushCounter);
     } else {
       // if the current state is LOW then the button
       // wend from on to off:
       Serial.println("off");
     }
     // Delay a little bit to avoid bouncing
     delay(50);
   }
   // save the current state as the last state,
   //for next time through the loop
   lastButtonState = buttonState;


   // turns on the LED every four button pushes by
   // checking the modulo of the button push counter.
   // the modulo function gives you the remainder of
   // the division of two numbers:

   //added
   if (buttonPushCounter % 1 == 0) { 
     digitalWrite(ledPin1, HIGH);
     digitalWrite(ledPin2, LOW);
     digitalWrite(ledPin3, LOW);
   } else if (buttonPushCounter % 2 == 0) {
     digitalWrite(ledPin1, LOW);
     digitalWrite(ledPin2, HIGH);
     digitalWrite(ledPin3, LOW);
   } else if (buttonPushCounter % 3 == 0) {
     digitalWrite(ledPin1, LOW);
     digitalWrite(ledPin2, LOW);
     digitalWrite(ledPin3, HIGH);
   }else {
     digitalWrite(ledPin1, LOW);
     digitalWrite(ledPin2, LOW);
     digitalWrite(ledPin3, LOW);
   }

}

And you can use the 'switch case' to manage your state handling.
'counter' is incremented each time a button press occurs.
'counter' can be reset as needed to get back to case 0:

switch (counter)
{
case 0:
//do something when counter equals 0, example turn on/off LEDs as needed
break;
case 1:
//do something when counter equals 1
break;
case 2:
//do something when counter equals 2
break;
case 3:
//do something when counter equals 3
break;

// etc.

default:
// if nothing else matches, do the default
// default is optional
break;
}

https://www.arduino.cc/en/Reference/SwitchCase

Or trim it down it even further:

. . .
unsigned long buttonPushCounter = 0 ;
. . .

loop() {

. . .
// get debounced buttonPushCounter
. . .
digitalWrite( ledPin1, ( buttonPushCounter % 4 == 1 ) ) ;
digitalWrite( ledPin2, ( buttonPushCounter % 4 == 2 ) ) ;
digitalWrite( ledPin3, ( buttonPushCounter % 4 == 3 ) ) ;

}

1 Like

Thank you so much all for the quick response now my problem was solve

all response very much appreciated.

here is my working code

/*
   State change detection (edge detection)

  Often, you don't need to know the state of a digital input all the time,
  but you just need to know when the input changes from one state to another.
  For example, you want to know when a button goes from OFF to ON.  This is called
  state change detection, or edge detection.

  This example shows how to detect when a button or button changes from off to on
  and on to off.

  The circuit:
  * pushbutton attached to pin 2 from +5V
  * 10K resistor attached to pin 2 from ground >> pulldown resistor
  * LED attached from pin 13 to ground (or use the built-in LED on
    most Arduino boards)

  created  27 Sep 2005
  modified 30 Aug 2011
  by Tom Igoe

 This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/ButtonStateChange

  */

// this constant won't change:
 const int  buttonPin = 2;    // the pin that the pushbutton is attached to
 const int ledPin1 = 13;       // the pin that the LED is attached to
 const int ledPin2 = 12;       // added
 const int ledPin3 = 11;       // added
// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
   // initialize the button pin as a input:
   pinMode(buttonPin, INPUT);
   // initialize the LED as an output:
   pinMode(ledPin1, OUTPUT); 
   pinMode(ledPin2, OUTPUT); //added
   pinMode(ledPin3, OUTPUT); //added
   // initialize serial communication:
   Serial.begin(9600);
}


void loop() {
   // read the pushbutton input pin:
   buttonState = digitalRead(buttonPin);

   // compare the buttonState to its previous state
   if (buttonState != lastButtonState) {
     // if the state has changed, increment the counter
     if (buttonState == HIGH) {
       // if the current state is HIGH then the button
       // wend from off to on:
       buttonPushCounter++;
       Serial.println("on");
       Serial.print("number of button pushes:  ");
       Serial.println(buttonPushCounter);
     } else {
       // if the current state is LOW then the button
       // wend from on to off:
       Serial.println("off");
     }
     // Delay a little bit to avoid bouncing
     delay(50);
   }
   // save the current state as the last state,
   //for next time through the loop
   lastButtonState = buttonState;


   // turns on the LED every four button pushes by
   // checking the modulo of the button push counter.
   // the modulo function gives you the remainder of
   // the division of two numbers:

   //added
   if (buttonPushCounter == 1) { 
     digitalWrite(ledPin1, HIGH);
     digitalWrite(ledPin2, LOW);
     digitalWrite(ledPin3, LOW);
   } else if (buttonPushCounter == 2) {
     digitalWrite(ledPin1, LOW);
     digitalWrite(ledPin2, HIGH);
     digitalWrite(ledPin3, LOW);
   } else if (buttonPushCounter == 3) {
     digitalWrite(ledPin1, LOW);
     digitalWrite(ledPin2, LOW);
     digitalWrite(ledPin3, HIGH);
   }else {
     digitalWrite(ledPin1, LOW);
     digitalWrite(ledPin2, LOW);
     digitalWrite(ledPin3, LOW);
     buttonPushCounter =0;
   }

}

Glad to help you !