My First Arduino Project

I am a nebie to this arduino world and without any previous work, i came up with this simple program of a led turning on after i press a button.

This Is My Program:
int boton1 = 2;
int led = 12;

void setup()
{
pinMode(12,OUTPUT);
pinMode(2,INPUT);
Serial.begin (9600);
}

void loop(){

boton1=digitalRead(2);
if(boton1==HIGH){
digitalWrite(12,HIGH);
}
else{
digitalWrite(12,LOW);
}
}

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.

you mean like this...

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.

In Arduino World, LOW means a value of 0 and HIGH means a value of 255.

OOPS! LOW and HIGH are Boolian variables meaning true or false. Sorry. Read the Reference in the IDE (Help>Reference) it is very helpful.

Un4gvn:
In Arduino World, LOW means a value of 0 and HIGH means a value of 255.

Not correct, in the Arduino world (which is C/C++) LOW = 0, HIGH means any other value, not just 255.

In Arduino (gcc compiler) a Boolean variable is still a full byte size variable, so any value but 0 is considered HIGH.

retrolefty:

Un4gvn:
In Arduino World, LOW means a value of 0 and HIGH means a value of 255.

Not correct, in the Arduino world (which is C/C++) LOW = 0, HIGH means any other value, not just 255.

no worries, i get the real sarcasm... thx for the tip by the way

retrolefty:

Un4gvn:
In Arduino World, LOW means a value of 0 and HIGH means a value of 255.

Not correct, in the Arduino world (which is C/C++) LOW = 0, HIGH means any other value, not just 255.

Not correct - HIGH is defined as 1 in "Arduino.h".

In C/C++ an expression is false if it evaluates to zero, and true if it evaluates to any non-zero value.