Go Down

Topic: alternating LEDs using Bounce library (Read 1 time) previous topic - next topic

eceela

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

:) 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);
     }
   }
}

AWOL

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:
Quote
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

eceela

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

AWOL

OK, so show us what you've got so far.
When posting code, please use the # icon on the editor's toolbar.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

eceela

So far this is all I have :( 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.

Code: [Select]
#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);
     }
   }
}

AWOL

Add more LEDs
Code: [Select]
#define LED 12
...
pinMode(LED,OUTPUT);


and more buttons
Code: [Select]
#define BUTTON 8
Bounce bouncer = Bounce( BUTTON, 2);
pinMode(BUTTON,INPUT);  // I'm surprised the class doesn't do this


See the pattern?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

eceela

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??
Code: [Select]
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

AWOL

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
Code: [Select]
if ( ledValue == LOW) {
         ledValue = HIGH;
       } else {
         ledValue = LOW;

to
Code: [Select]
ledValue = ! ledValue;
but that's because I'm lazy and don't like typing.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

raron

#8
Mar 28, 2011, 08:24 pm Last Edit: Mar 28, 2011, 08:26 pm by raron Reason: 1
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 :P

eceela

@ 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 :) thanks for your reply

@ AWOL

So i added a new button and a new LED to the code
Code: [Select]
    #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?
Code: [Select]

    void loop() {

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



raron

Quote
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: http://arduino.cc/en/Reference/SwitchCase


Btw your first code part seems OK, except you did not make a "Bounce" object for the second button
Code: [Select]
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" (http://arduino.cc/en/Reference/HomePage) and what AWOL wrote:
Code: [Select]
ledValue = ! ledValue;


mparduino

eceela,

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

Go Up