Offline
Jr. Member
Karma: 0
Posts: 63
The Stuff of Legend!
|
 |
« on: July 15, 2009, 10:16:35 pm » |
Ugh... really starting to hate this whole "programming" thing... Board reads pin as High, even with nothing attached... something wrong with my code? (That's basically right out of a book) Worked before  #define Button 2 int val= 0; void setup () { pinMode(Button, INPUT); Serial.begin(9600); } void loop () { val=digitalRead(Button); if (val ==HIGH) { Serial.println("one");} else { Serial.println("two");} }
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 1
Posts: 170
|
 |
« Reply #1 on: July 15, 2009, 10:35:55 pm » |
Actually, this is a hardware problem  Turns out "unattached" is not the same as "ground" With nothing attached to the pin, it's a "floating" voltage that varies randomly. If you want to make sure it's low, attach a wire from the pin to ground (digital pin 14) If you really want to see how it floats, try it on an analog pin. Watch the value change as you move your hand closer and further away. #define Button 2 int val= 0;
void setup () { Serial.begin(9600); }
void loop () { val=analogRead(Button); //read ANALOG pin 2 Serial.println(val); // and show value 0-1023 } Good luck! P.S. Just to be clear, I included the sketch just so you could see how the value floats with nothing attached. Your code is fine.
|
|
|
|
« Last Edit: July 15, 2009, 10:38:39 pm by Kitep »
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 63
The Stuff of Legend!
|
 |
« Reply #2 on: July 15, 2009, 11:33:26 pm » |
Hmmh  well that answers that one... But I can't use a button either, always reads high if not attached to ground... isn't that how you make a switch? (Connected v. not?) (Button checks out electromechanically with ohmmeter)
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 63
The Stuff of Legend!
|
 |
« Reply #3 on: July 15, 2009, 11:33:47 pm » |
oh and thank you very much btw 
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 6
Arduino rocks
|
 |
« Reply #4 on: July 15, 2009, 11:44:33 pm » |
Jack, What you're looking for is a button with a pull-up, or pull-down resistor. http://en.wikipedia.org/wiki/Pull-up_resistorWith a pull-down resistor connected between the pin and ground, and then the button connected between the pin and V+ (or what have you), the pin will read low until the button is pressed. -Matt
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 63
The Stuff of Legend!
|
 |
« Reply #5 on: July 15, 2009, 11:47:48 pm » |
k : ) thanks! (Weird that I got it to work w/o that now that I think about it... I use open pins for random numbers normally anyways 
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 1
Posts: 170
|
 |
« Reply #6 on: July 16, 2009, 12:36:45 am » |
well that answers that one... I knew the answer cause I was doing the exact same thing just a couple of months ago... 
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 63
The Stuff of Legend!
|
 |
« Reply #7 on: July 16, 2009, 12:40:09 pm » |
(Sry if ressurecting old thread) One last thing that I noticed now... the book Had a pull down resistor, I wired it wrong  But there's no resistor in the current path? it doesn't hurt the chip to have a short circuit?
|
|
|
|
|
Logged
|
|
|
|
|
Left Coast, CA (USA)
Offline
Brattain Member
Karma: 279
Posts: 15293
Measurement changes behavior
|
 |
« Reply #8 on: July 16, 2009, 04:15:11 pm » |
But there's no resistor in the current path? it doesn't hurt the chip to have a short circuit? When an Arduino pin is programmed to be an input pin it is a high impedance load and will draw almost no current, so with a external pull down resistor the only current draw is from the ground through the external pull down resistor to the external voltage source, ohms law can help you determine the amount of current that the pull down resistor will draw. Now grounding a Arduino pin that is programmed to be an OUTPUT pin and setting the pin to high in software will cause a short circuit and damage the pin. Conversely wiring an output pin to +5vdc and setting the pin to a low output will also cause a short circuit and damage the pin, But Arduino pins set to input mode can only really be damaged by wiring to voltages above or below the maximum allowed, generally 0 volts and +5vdc ( so no negitive voltages of volts above +5volts allowed) for 5 volt systems. That make sense? Lefty
|
|
|
|
« Last Edit: July 16, 2009, 04:18:28 pm by retrolefty »
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 63
The Stuff of Legend!
|
 |
« Reply #9 on: July 16, 2009, 04:17:15 pm » |
neat, tyvm! 
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 230
Arduino rocks
|
 |
« Reply #10 on: July 18, 2009, 05:01:46 am » |
A convenient way to connect a switch is between an input pin and ground. The processor chip conveniently has built-in pullup resistors that you can enable with your software so you can eliminate the external pullup resistor. Additionally it's usually more convenient and better practice to connect one side of all your switches to ground instead of the +5V supply. Your code would look like this - assuming a switch connected between pin 3 and ground: int switchPin = 3;
void setup() { pinMode(switchPin, INPUT); // makes pin 3 an input pin digitalWrite(switchPin, HIGH); // writing a HIGH to an INPUT pin // connects the internal pullup resistor }
void loop() { if(digitalRead(switchPin) == LOW) { ....... // switch is closed } else { ......// switch is open } .... }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 63
The Stuff of Legend!
|
 |
« Reply #11 on: July 18, 2009, 11:11:11 am » |
Hmm... wish some docs had come with the chip  Much easier.... and keeping that pin high all the time doesn't drain batteries b/c the inputs have very high resistance?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 230
Arduino rocks
|
 |
« Reply #12 on: July 18, 2009, 05:31:00 pm » |
Well think about it. Any current that might flow from the input pin through the internal pullup would have no place to go when the switch is open - as most pushbuttons normally are. Current would only flow ( and a very small current at that - 5V/20kohms = 0.25 milliamps - when you push the button.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 11
Arduino rocks
|
 |
« Reply #13 on: August 05, 2009, 02:41:03 am » |
Thanks for the info about the input, it was making me lose sleep
|
|
|
|
|
Logged
|
|
|
|
|
|