Go Down

Topic: Two Buttons, Two Inputs (Read 41980 times) previous topic - next topic

alanderego

Hello Everyone,
I am in need of help.
I am trying to use 2 buttons to 2 separate digital inputs (digital 2 and digital 3).
I used the tutorial for the button hook up and just doubled it for the second switch.
http://yfrog.com/3odsc03204sj
I used the example code and I could get only one button to work (obviously because the code is for only one switch, but at least I know it works).
I changed the code from "const int buttonPin = 2" to "const int buttonPin = 3" then I could get the second switch to work, but the first button would stop working.
I tried adding lines with "const int buttonPin = 2" and "const int buttonPin = 3" and it gave me an error "redefinition of const int buttonPin".
Could someone please inform me on how to change/add to the code to get both switches to work?

Thanks,
Alan

brutus1975

the

Code: [Select]
const int buttonPin =2

sets up a variable with the name "buttonPin".  You can only have one variable of the exact same name, which is the root of your problem.

You'll need two of these variables, one for each button, and they'll have to be unique.  Update your code with something like

Code: [Select]
const int buttonPin1=2
const int buttonPin2=3


In the rest of your code, anything relating to the first button, make sure it uses "buttonPin1" and anything relating to the second button uses "buttonPin2"

alanderego

Thanks for the quick reply.
I made the change and now only digital input 3 works.
I am not sure if I did "void setup" and the "void loop" correctly.
Here is what changes I made...
Code: [Select]

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin1 = 2;     // the number of the pushbutton pin 2
const int buttonPin2 = 3;     // the number of the pushbutton pin 3
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
 // initialize the LED pin as an output:
 pinMode(ledPin, OUTPUT);      
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin1, INPUT);
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin2, INPUT);    
}

void loop(){
 // read the state of the pushbutton value:
 buttonState = digitalRead(buttonPin1);
 // read the state of the pushbutton value:
 buttonState = digitalRead(buttonPin2);

 // check if the pushbutton is pressed.
 // if it is, the buttonState is HIGH:
 if (buttonState == HIGH) {    
   // turn LED on:    
   digitalWrite(ledPin, HIGH);  
 }
 else {
   // turn LED off:
   digitalWrite(ledPin, LOW);
 }
}


lostdog

Hi aland.

You have correctly changed the pushbuttons variables names, but you forget to change the buttonState names, so when the sketch read the pusbuttons, it write the result in the same variable both times, and only the last is working, because the last overwrite the previous...

Code: [Select]
// read the state of the pushbutton value:
 buttonState = digitalRead(buttonPin1);
 // read the state of the pushbutton value:
 buttonState = digitalRead(buttonPin2);


Moreover, you display the result of the two pushbuttons actions in a two different outputs. You should use two outputs whit two leds...


amacmullen14

That code works if you want it to function like an or gate, accepting either button press, but you could just wire the buttons in parallel to one digital pin, which could do the same thing only with simpler code.

lostdog

Hmm.. the code I was inserted in my previous message is take out from the aland code, is not a suggestion for fix the problem, only focusing where the problem is.
I leave the job of modify the code at aland, for learning.

I am new for arduino, but not for programming...
I think the second writing overwrite the first.
So, is not an OR function...

About the working of this highlighted code:
- the first line assign the value of the buttonpin1 to the variable buttonState
- the second line assign the value of the buttonpin2 to the same variable buttonState, overwiting it.
For example, if the first button is pressed, and second not pressed, the code write on the variable before the first result (pressed), later the second (not pressed).
Subsequently, the led output is written using the buttonState value, so I think that only the second assignement is used from the sketch.

alanderego

#6
Aug 23, 2009, 08:08 pm Last Edit: Aug 23, 2009, 08:16 pm by alanderego Reason: 1
Thanks for the great responses.
I guess I should say what I am doing and maybe you can see if I going in the wrong direction.
I want to make the Arduino Tweet me when my washer and dryer are done.
Here is what I planned...
When digital input 1 is asserted (AKA pushbutton 1 is pressed) that would send a tweet that the washer is done.
When digital input 2 is asserted (AKA pushbutton 2 is pressed) that would send a tweet that the dryer is done.
I got the arduino to tweet now I just need to figure out the button problem then how to combine the code for both.
@lostdog, I think you are right...It would be easier to wire in 2 LEDs.
I was just lazy to wire two up when the one is wired up already.

Thanks Again,

Alan

RoyK

As someone above stated you need to set up two buttonstate variables.

Your code would be something like this:

Code: [Select]
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin1 = 2;     // the number of the pushbutton pin 2
const int buttonPin2 = 3;     // the number of the pushbutton pin 3
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;


void setup() {
 // initialize the LED pin as an output:
 pinMode(ledPin, OUTPUT);      
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin1, INPUT);
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin2, INPUT);    
}

void loop(){
 // read the state of the pushbutton value:
 buttonState1 = digitalRead(buttonPin1);
 // read the state of the pushbutton value:
 buttonState2 = digitalRead(buttonPin2);

 // check whether a  pushbutton is pressed.
 // if it is, the buttonState is HIGH:
 
 if (buttonState1 == HIGH) {    
   // turn LED on:   // Tweet that the washer is done
   digitalWrite(ledPin, HIGH);  
 }
 if (buttonState2 == HIGH) {    
   // turn LED on:    //Tweet that the dryer is done
   digitalWrite(ledPin, LOW);  
 }
}



You will quickly find that you will have to add more code to keep track of whether you have already tweeted or you'll keep sending messages every time through the loop if the input(s) stay high. Heck, might bring down the internet! ;)

AlphaBeta

I just could not resist posting a hardware abstraction library version of this.

Quote

#include <Button.h> //http://www.arduino.cc/playground/Code/Button

Button button1 = Button(11,PULLUP);
Button button2 = Button(12,PULLUP);

void setup(){
 //setup
}

void loop(){
 if (button1.uniquePress()){ //you need to release this button in order for it to trigger again
   //do action associated with button1
 }
 if (button2.uniquePress()){ //you need to release this button in order for it to trigger again
   //do action associated with button1
 }
}

tasasaki

Wow it's a very good idea to quote codes using "Insert Quote" than "Insert Code".
I was frustrated with "Insert Code" because I couldn't post colored codes for emphasizing some important parts.

alanderego

#10
Aug 25, 2009, 09:31 am Last Edit: Aug 25, 2009, 09:38 am by alanderego Reason: 1
I finally got it to work.

Thanks again for the help...
@AlphaBeta... Thanks for push in the right direction.

Here is what I came up with (errrr... pieced together)...
If you guys see any holes, could you please point it out?

Quote

#include <Button.h>

Button button1 = Button(2);
Button button2 = Button(3);

void setup(){
 pinMode(13,OUTPUT); //debug to led 13
}

void loop(){
 if(button1.isPressed()){
     digitalWrite(13,HIGH);
 }
 if(button2.isPressed()){
       digitalWrite(13,HIGH);
 }
 else{
     digitalWrite(13,LOW);
 }
}


I am so surprised that there are so many ways to write code for the Arduino...
So I got the both the buttons to work and the twitter to tweet, now I just got to combine the two...
I appreciate all your help.

Alan

RoyK

just pay attention to what AlphaBeta showed you about the uniquePress() function of the button class......

TBAr

#12
Aug 26, 2009, 02:37 am Last Edit: Aug 26, 2009, 02:43 am by TBAr Reason: 1
Have you tested this code by only pressing button1?

It seems that if button2 is not pressed, the led will be turned off, no matter what the state of button1 is.

alanderego

Here is what happens...
The LED is always off when no button is pressed...
If I push button 1 the LED turns on.
If I let go of push button 1 the LED goes off.
Same for PB 2.
If I push both buttons the LED turns on.
If I let go of one of the buttons the LED stays on and vice versa.

This is what I wanted.

I am going to use this to trigger twitter (somehow...I am working on that part now) that the washer or dryer is done.

Thanks again for all the input,

Alan

TBAr

Ok, well that loop is certainly running fast enough for POV to be effective.

Good luck with the project, and don't forget uniquePress(), as RoyK and AlphaBeta mentioned.

Go Up