Sydney, Australia
Offline
Newbie
Karma: 0
Posts: 17
|
 |
« on: March 27, 2011, 10:55:48 am » |
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/ButtonBounce 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); } } }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19380
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: March 27, 2011, 11:32:20 am » |
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.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Sydney, Australia
Offline
Newbie
Karma: 0
Posts: 17
|
 |
« Reply #2 on: March 27, 2011, 12:06:50 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19380
I don't think you connected the grounds, Dave.
|
 |
« Reply #3 on: March 27, 2011, 12:27:55 pm » |
OK, so show us what you've got so far. When posting code, please use the # icon on the editor's toolbar.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Sydney, Australia
Offline
Newbie
Karma: 0
Posts: 17
|
 |
« Reply #4 on: March 27, 2011, 12:58:01 pm » |
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. #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); } } }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19380
I don't think you connected the grounds, Dave.
|
 |
« Reply #5 on: March 27, 2011, 01:50:18 pm » |
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?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Sydney, Australia
Offline
Newbie
Karma: 0
Posts: 17
|
 |
« Reply #6 on: March 28, 2011, 05:23:58 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19380
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: March 28, 2011, 05:28:55 am » |
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.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Norway
Offline
Sr. Member
Karma: 4
Posts: 422
microscopic quantum convulsions of space-time
|
 |
« Reply #8 on: March 28, 2011, 01:24:32 pm » |
Just a thought: Do you know what debouncing is for? http://www.arduino.cc/en/Tutorial/DebounceI'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/DigitalReadEDIT: Wops, just saw you had a bounce thread, so I guess you know about it
|
|
|
|
« Last Edit: March 28, 2011, 01:26:17 pm by raron »
|
Logged
|
|
|
|
|
Sydney, Australia
Offline
Newbie
Karma: 0
Posts: 17
|
 |
« Reply #9 on: March 28, 2011, 10:56:22 pm » |
@ 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 #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); } } }
|
|
|
|
|
Logged
|
|
|
|
|
Norway
Offline
Sr. Member
Karma: 4
Posts: 422
microscopic quantum convulsions of space-time
|
 |
« Reply #10 on: March 29, 2011, 02:06:17 am » |
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/SwitchCaseBtw 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" ( http://arduino.cc/en/Reference/HomePage) and what AWOL wrote: ledValue = ! ledValue;
|
|
|
|
|
Logged
|
|
|
|
|
|