using a port as an input port

Hi!

Im new to the arduino platform and Im just fooling around with different components to learn the system. I want to connect a push button to one port and when it's pushed just light up a led on the processor. My source looks like this:

int button = 1; int led = 13;

void setup() { pinMode(button, INPUT); pinMode(led, OUTPUT) ; }

void loop() { digitalWrite(led, LOW); if(digitalRead(button)==HIGH) { digitalWrite(led, HIGH); delay(5000); } }

The behaviour now is that the led is always on, which means that there is always power on the port where the button is placed. I have tried it with or without the button connected, same thing.

Question: Do I have to reset the port Im using in some way to not get random values? Or how do I change my code to solve this task?

You don’t say how your button is wired, but my guess is it’s floating.
You may need a pull-up or pull-down resistor.

The processor has built-in pull-ups that can be enabled, but they invert the logic of your push button.
You wire your button between ground and an input pin, and when the switch is closed, the pin reads LOW.

Also pin 1 is the serial Tx.

const int button = [glow]2[/glow];
const int led = 13;

void setup()   {                
   pinMode(button, INPUT); 
   [glow]digitalWrite (button, HIGH); //enable pull up[/glow].
   pinMode(led, OUTPUT) ;
}

void loop()                     
{
 digitalWrite(led, LOW);
 if(digitalRead(button)== [glow]LOW[/glow])
 {
   digitalWrite(led, HIGH);
   delay(5000);
 }
}

Thanks!! That works just perfect. Now I just have to work out how the pull-up and pull-downs works.