Arduino: Inexplicable behavior with buttons

Go To StackoverFlow.com

2

I'm just starting to delve into the world of arduino's with several Teensy's and Teensy++'s and so far it has gone great with exception of the extremely weird behavior I have been getting when trying to take input from multiple buttons.

Pretty much, all I have is a teensy++, a LCD display and 3 buttons. The code I have just reads the state of all three buttons and then posts the results to the screen, 0 or 1. Couldn't be simpler, which is why I am having trouble figuring out why it isn't working.

With a single button, the above works fine. But as soon as I add one of the other buttons into the mix, instead of working right, when I press button 1, both button 1 and button 2 turn to 1. Button 2 does nothing. Button three also does nothing, and does not even turn to one when any other button is pressed.

My first inclination was that my clumsy ass has bridged some traces with solder on the board. I grabbed one of my new ones and soldered it anew, checking every contact to make sure that it was good, and it was. Instead of using the breadboard wires that I usually use I used solid core wire to make sure the wires were not the issue. I also switched breadboards in case that one was faulty.

Same problem. I am not sure what could be causing this to not work. I am hoping that one of you guru's can swoop in and tell me that I'm doing something totally wrong because I can't figure out why it is not working at all

This is my code:

#include <LiquidCrystal.h>

LiquidCrystal lcd(5,6,7,8,9,10);

void setup(){

pinMode(13,INPUT);pinMode(21,OUTPUT);  //Button one
pinMode(12,INPUT);pinMode(20,OUTPUT);  //Button two
pinMode(11,INPUT);pinMode(19,OUTPUT);  //Button three

digitalWrite(21,HIGH); //Power buttons
digitalWrite(20,HIGH);
digitalWrite(19,HIGH);

lcd.begin(16,4);

}

int resp1 = 3;  //Variables for responses 
int resp2 = 3;
int resp3 = 3;

void loop(){

 resp1 = digitalRead(13); //Read button 1
 resp2 = digitalRead(12); //Read button 2
 resp3 = digitalRead(11); //Read button 3

 //Print it to screen
 lcd.clear();
 lcd.print("Mike's Devboard");
 lcd.setCursor(0,1);
 lcd.print("Btn 1:");
 lcd.print(resp1);
 lcd.print("    Btn 2:");
 lcd.print(resp2);
 lcd.setCursor(0,2);
 lcd.print("Btn 3:");
 lcd.print(resp3);
 delay(48);
}    

Any idea's?

2012-04-04 03:25
by Mike Fryer
Can you post a wiring schema ? Looking at your code i see a few weird things. 1) dont use an output as a power supply for your buttons use the gnd and the 5V add a resistor to in serial between the button and the 5v as in the arduino tutorialSibster 2012-04-04 06:20
Exactly what I was doing wrong, thanks. I thought the resistor was only nessisary to stop the LED from blowing up but reading it more carefully I see why I need it. It's working now - Mike Fryer 2012-04-04 16:50


1

Reading the comments for the button pins I guess, that you connect both sides of each buttons to the mentioned pins of the chip without anything else (i.e. no resistor etc.)

If that's the case then

pinMode(13,INPUT);pinMode(21,OUTPUT);
digitalWrite(21,HIGH); //Power buttons

will drive pin 21 HIGH and pin 13 into high impedance mode as long as the button is open. This means that the input pin is basically "floating" - any influence of the environment will make it toggle. It is basically random input.

If you close the button, the input pin is connected to HIGH and gives a strong HIGH also.

What you need:

You need to bring the input pins into defined, "strong" states in both situations - when the button is open and when it is open.

You could do it like this:

pinMode(13,INPUT); digitalWrite(13, HIGH);  // input pin with internal pullup enabled
pinMode(21,OUTPUT); digitalWrite(21,LOW);   // other pin to ground

Now one leg of the button is always LOW (on ground). When the button is open, the input pin is "pulled up" to HIGH by an internal resistor of the CPU. When the button is closed some current flows from HIGH through the resistor through the button into the other pin which is still LOW. This drives the input pin LOW also.

See Digial Pins in the tutorial for more on this.

One noteable side-effect:

You will have "negated" input: You will read HIGH (1) if the button is open and LOW (0) when the button is closed.

2012-04-04 20:14
by A.H.


0

You have to reset the values of the three variables resp1, resp2 and resp3, which you are using to keep the state of the buttons at the end of the loop() function.

2012-04-04 06:15
by Sudar
The digitalReads will make sure, that the values have defined states - A.H. 2012-04-04 19:53


0

Note that the function digitalRead() return a HIGH or LOW, which correspond to TRUE,FALSE or 1,0 . BTW there is no need to reset the values, they will be resigned to the state of the button each loop.

2012-04-04 19:57
by Kirill Kulakov
Ads