simply what i am trying to do here is, with a button a led on 12 will turn on, and vice versa... the problem is every time i upload my program into the arduino the led turns on by itself before i even press the button... what am i doing wrong!?
igobyjoey:
simply what i am trying to do here is, with a button a led on 12 will turn on, and vice versa... the problem is every time i upload my program into the arduino the led turns on by itself before i even press the button... what am i doing wrong!?
How have you connected your LED? Are you using a pull-down resistor?
Your button input when not pressed is causing a 'floating input' condition on the input pin and returning random value. Either wire a pull-down resistor from the pin to ground (10K ohms) will fix the problem, or a simpler method is to wire your button from ground to the input pin and then do a pinMode(2,INPUT_PULLUP); command in your setup function. Then reading a low means the button is pressed and reading a high means the button is not being pressed.
Try looking at an example in the Arduino IDE: File>Examples>01.Basics>Blink or File>Examples>10.StarterKit>p02_SpaceshipInterface. One of the beginning projects in the Arduino book that comes with the starter kit guides you through an example just like what you are trying to do. There is definitely something wrong with your code, but I don't have my Uno board with me to figure it out. Keep trying!
Yes, button between ground and pin2. Then do the pinMode like I listed and change your code logic to reflect that a low means pressed and a high means not pressed.
so does this mean that in the arduino world.. a LOW code for a button means that the button is pressed and a HIGH code means that the button is not pressed...!?
Looking at your code again. Your 'buton1' variable should be initialized to 0. It is used with digitalRead() to check for voltage on pin 2 , for example: buton1 = digitalRead(2);.
igobyjoey:
so does this mean that in the arduino world.. a LOW code for a button means that the button is pressed and a HIGH code means that the button is not pressed...!?
No, it just depends on if you want to add an external pull-down resistor between the pin and ground or not. Both ways work, one requires a resistor the other utilize a programmable internal pull-up resistor that is free.
If you have an example or tutorial sketch/schematic that having you wire the button between +5 vdc and an input pin, but showing no pull-down resistor wired between the input pin and ground, then you have a poor/incorret example/tutorial and that's a fact.