0

Buttons
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Buttons

by kotchi on Mon Sep 07, 2009 10:57 pm

First i would like to add that i am really new to all this, so bear with me.

I'm having trouble with some buttons.
I understand how the tact switch that comes with the arduino works. It switches beetwen grounded (G) and 5 volv (V), deciding wich one will be connected to the digital I/O.
Now i'm building a small project where i'm planning on using larger buttons than small tact switches.

The one i have looks like this.
Image
With only 2 legs, opens when pushed, closed otherwise. Now the problem i have is that i can only connect it to either G or V because one leg always needs to go to the I/O.

Now i tried connecting one to V and the other one to I/O but i can never get any good HIGH or LOW readings. Sometimes i can push it once and it switches between HIGH and LOW, but wont work the second time i push it. Or it just switches between HIGH/LOW like crazy.

Is there a way to get a good read from these kind of switches or am i using it wrong, or is there some other kind of button more suitable for this kind of work.

Any help would be appreciated.
kotchi
 
Posts: 7
Joined: Wed Jul 22, 2009 10:14 am

Re: Buttons

by len17 on Tue Sep 08, 2009 12:18 am

You need to use a pull-up or pull-down resistor so that the input pin isn't left "floating" when the switch is open.

The Arduino conveniently has a built-in pull-up resistor for each input pin, but you have to add a line of code to turn it on. Then you can connect the switch from the input pin to ground. Here's a sketch that shows how it works:

Code: Select all | TOGGLE FULL SIZE
#define pinInSwitch 5

void setup()
{
  Serial.begin(57600);
  pinMode(pinInSwitch, INPUT);
  digitalWrite(pinInSwitch, HIGH); // turn on pull-up resistor
}

void loop()
{
  if (digitalRead(pinInSwitch) == LOW) {
    Serial.println("LOW");
  } else {
    Serial.println("HIGH");
  }
}

len17
 
Posts: 394
Joined: Sat Mar 14, 2009 7:20 pm

Re: Buttons

by kotchi on Tue Sep 08, 2009 8:09 am

Thanks, now its working.
kotchi
 
Posts: 7
Joined: Wed Jul 22, 2009 10:14 am

Re: Buttons

by adafruit on Tue Sep 08, 2009 5:46 pm


adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

Re: Buttons

by kotchi on Thu Sep 10, 2009 9:00 am

Yeah, i read that one and understood how it worked with a tact switch. But i couldnt get a button with only two legs to work and needed that one line to turn on the pull up resistor (digitalWrite(pinInSwitch, HIGH);).

I have run into another problem now, so i thought i should place it in the same topic since its the same project.

I'm trying to print something on my lcd. The word 'arming' to be precise. I got the printing part down, even made a timer so far. The problem is this:
I can write;
lcd.print("ARMIN");
and it will print it, and everything else will show. But if i try to spell the entire word:
lcd.print("ARMING");
The lcd wont print anything, or it will show random characters/signs. But the rest of the code runs. Buzzer/light etc.

And i am pulling my hair out because i just cant understand why it wont work.
I've tried printing other words and it seems the limit of characters is 5. But i have printed longer words/sentences before that.
kotchi
 
Posts: 7
Joined: Wed Jul 22, 2009 10:14 am

Re: Buttons

by kotchi on Thu Sep 10, 2009 9:00 am

Yeah, i read that one and understood how it worked with a tact switch. But i couldnt get a button with only two legs to work and needed that one line to turn on the pull up resistor (digitalWrite(pinInSwitch, HIGH);).

I have run into another problem now, so i thought i should place it in the same topic since its the same project.

I'm trying to print something on my lcd. The word 'arming' to be precise. I got the printing part down, even made a timer so far. The problem is this:
I can write;
Code: Select all | TOGGLE FULL SIZE
lcd.print("ARMIN");

and it will print it, and everything else will show. But if i try to spell the entire word:
Code: Select all | TOGGLE FULL SIZE
lcd.print("ARMING");

The lcd wont print anything, or it will show random characters/signs. But the rest of the code runs. Buzzer/light etc.

And i am pulling my hair out because i just cant understand why it wont work.
I've tried printing other words and it seems the limit of characters is 5. But i have printed longer words/sentences before that.
kotchi
 
Posts: 7
Joined: Wed Jul 22, 2009 10:14 am

Re: Buttons

by kotchi on Thu Sep 10, 2009 10:06 am

Made some progress, it seems that it works if i enter a longer sentence. Could there be a reason for this? Is there like a minimum or maximum amount of characters?
If i write a word between 6-10 characters it doesnt work. But i can write say "ODA-903" and it works. It just feels completely random.
kotchi
 
Posts: 7
Joined: Wed Jul 22, 2009 10:14 am

Re: Buttons

by mtbf0 on Thu Sep 10, 2009 10:51 am

well that's just plain bizarre. can we see the code with a little context.

or you could hyphenate "arm-ing."
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Re: Buttons

by mike31416 on Thu Sep 10, 2009 11:04 am

Memory corruption?

mike31416
 
Posts: 126
Joined: Wed Aug 26, 2009 12:06 pm

Please be positive and constructive with your questions and comments.