alternating LEDs using Bounce library

Heya I'm currently using the bounce code below and it's been written to control one LED with one Button. I wanted to use THREE LEDs and THREE Buttons.
Eg. Button 1, Button 2, Button 3, LED 1, LED 2, LED 3

I wanted to modify this code so that
LED 1 is initially on and when Button 1 is pressed, it turns off but LED 2 turns on
then when Button 2 is pressed, LED 2 turns off and LED 3 turns on
then when Button 3 is pressed, LED 3 turns off

:slight_smile: hope I explained it right!! I'd really appreciate any help!

#include <Bounce.h>
#define BUTTON 8
#define LED 12

int ledValue = HIGH;

// This example changes the state of the LED everytime the button is pushed
// Build the circuit indicated here: http://arduino.cc/en/Tutorial/Button

Bounce bouncer = Bounce( BUTTON, 2);

void setup() {
pinMode(BUTTON,INPUT);
pinMode(LED,OUTPUT);
}

void loop() {

if ( bouncer.update() ) {
if ( bouncer.read() == LOW) {
if ( ledValue == LOW) {
ledValue = HIGH;
} else {
ledValue = LOW;

}
digitalWrite(LED,ledValue);
}
}
}

First you're going to need to declare some more variables, like which pins your switches and LEDs are connected to.

Second, you're going to need to tighten up your specification:

LED 1 is initially on and when Button 1 is pressed, it turns off but LED 2 turns on
then when Button 2 is pressed, LED 2 turns off and LED 3 turns on
then when Button 3 is pressed, LED 3 turns off

What happens, after button 1 has been pressed and LED 2 turns on, if you press button 1 again.
And so on.

Sorry about that! So right now I haven't actually connected the other buttons/LEDs. I'm using an electronic brick chassis with digital pins d8 - d12 and analogue pins a1 - a5. The chassis looks like the one in this picture

So the idea is that nothing happens if you don't press the right button.
LED 1 is initially on and when Button 1 is pressed (nothing happens when button 2 or button 3 is pressed), it turns off but LED 2 turns on
then when Button 2 is pressed (nothing happens when button 1 or button 3 is pressed), LED 2 turns off and LED 3 turns on
then when Button 3 is pressed (nothing happens when button 1 or button 2 is pressed), LED 3 turns off

Hope that's enough info! Thanks so much for replying

OK, so show us what you've got so far.
When posting code, please use the # icon on the editor's toolbar.

So far this is all I have :frowning: I'm so sorry but I'm completely new to this. I don't know how to add more LEDs/Buttons to the code. It would be great if you could help.

#include <Bounce.h>
#define BUTTON 8
#define LED 12

int ledValue = HIGH;

// This example changes the state of the LED everytime the button is pushed
// Build the circuit indicated here: http://arduino.cc/en/Tutorial/Button


Bounce bouncer = Bounce( BUTTON, 2);

void setup() {
  pinMode(BUTTON,INPUT);
  pinMode(LED,OUTPUT);
}

void loop() {

   if ( bouncer.update() ) {
     if ( bouncer.read() == LOW) {
       if ( ledValue == LOW) {
         ledValue = HIGH;
       } else {
         ledValue = LOW;
         
       }
       digitalWrite(LED,ledValue);
     }
   }
}

Add more LEDs

#define LED 12
...
 pinMode(LED,OUTPUT);

and more buttons

#define BUTTON 8
Bounce bouncer = Bounce( BUTTON, 2);
 pinMode(BUTTON,INPUT);  // I'm surprised the class doesn't do this

See the pattern?

Sorry, I'm learning off a teacher that practically refuses to help us in any way. Yep, so I've added a new LED but how should I alter this part of the code??

void loop() {

   if ( bouncer.update() ) {
     if ( bouncer.read() == LOW) {
       if ( ledValue == LOW) {
         ledValue = HIGH;
       } else {
         ledValue = LOW;
         
       }
       digitalWrite(LED,ledValue);
   }

thanks so much for your help!! i really appreciate it

Well, I don't know what you've called your new objects so it is difficult to say exactly (hint), but I'd change this

if ( ledValue == LOW) {
         ledValue = HIGH;
       } else {
         ledValue = LOW;

to ledValue = ! ledValue;
but that's because I'm lazy and don't like typing.

Just a thought:

Do you know what debouncing is for? http://www.arduino.cc/en/Tutorial/Debounce

I'd suggest rethinking if including the debounce library is really neccesary. It's no harm if you keep using it, in a way it is perhaps useful for getting used to include and use libraries. But on the other hand, it keeps you from seeing what's really going on. Often that can be a good thing, easier programming etc, but sometimes not (like now imho).

Very simple, non-debounced example of digitalRead: http://arduino.cc/en/Reference/DigitalRead

EDIT: Wops, just saw you had a bounce thread, so I guess you know about it :stuck_out_tongue:

@ raron
Debounce was the first thing that I came across when I was looking for a way of turning an LED off and on. If there's an easier method then I'd prefer to use it instead. Basically the other tutorials that I did would turn an LED on if the button was held down but as soon as the button was released, it would turn off again. So debounce did what I wanted it to do but if you can suggest a code that's easier to understand then that'd be great :slight_smile: thanks for your reply

@ AWOL

So i added a new button and a new LED to the code

    #include <Bounce.h>
    #define BUTTON 8
    #define LED 12
    #define LEDB 11
    #define BUTTONB 7

    int ledValue = HIGH;
    // This example changes the state of the LED everytime the button is pushed
    // Build the circuit indicated here: http://arduino.cc/en/Tutorial/Button


    Bounce bouncer = Bounce( BUTTON, 2);

    void setup() {
      pinMode(BUTTON,INPUT);
      pinMode(LED,OUTPUT);
      pinMode(BUTTONB,INPUT);
      pinMode(LEDB,OUTPUT);
    }

Is this done correctly??

How should I edit this part of the code if I want LEDB to turn on once LED turns off?

    void loop() {

       if ( bouncer.update() ) {
         if ( bouncer.read() == LOW) {
           if ( ledValue == LOW) {
             ledValue = HIGH;
           } else {
             ledValue = LOW;
            
           }
           digitalWrite(LED,ledValue);
       }
       }
    }

Debounce was the first thing that I came across when I was looking for a way of turning an LED off and on.

Yes, if you use the same button to alternate between off and on, or similar functions, debouncing is a good idea. Arduino is fast enough to detect the spurious contact bounces that can occur when a button is (de)pressed, so it might seem like multiple quick presses while it was supposed to be only one.

But as you stated, in your application subsequent presses should do nothing (this can be considered debouncing too), so it doesn't really matter if there are contact bounce or not, right? Well maybe depending on how you code it.

As for an easier code (really lots of ways to do this), I would suggest learning about the switch case statement for a different approach: switch...case - Arduino Reference

Btw your first code part seems OK, except you did not make a "Bounce" object for the second button

Bounce bouncer2 = Bounce( BUTTONB, 2);

I haven't used the "Bounce" library so I don't know what the ",2" is for. Maybe for debounced time in milliseconds, in which case its a bit small imho. Try 20 (unless its for something else).

As for the second code segment, read up on the boolean operator "not" (Arduino - Home) and what AWOL wrote:

ledValue = ! ledValue;

eceela,

how do you make Bounce worked? Can you give me the instruction?