Push Button Timer

I'm new to Arduino and am having some trouble with this basic code i am trying to make. I want to push a button and have a LED turn on for three seconds then turn off automatically. I'm sure I have this wrong, but please help!!

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}

void loop(){

  buttonState = digitalRead(buttonPin);
  
  if (buttonState == HIGH) 
  {
    digitalWrite(ledPin, HIGH);
    delay (3000);
    digitalWrite(ledPin, LOW);
  }
  if (buttonState == LOW)
  {
    digitalWrite(ledPin, LOW);
  }
}

ChaseA:
I'm sure I have this wrong, but please help!!

Why are you sure you have it wrong? Is it not doing what you're expecting it to do?

when i push the button,the led just stays on

(From a fellow newbie) The following doesn't directly deal with your problem, but what does this do?

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}

void loop(){
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    digitalWrite(ledPin, HIGH);
    delay (3000);
  }
  digitalWrite(ledPin, LOW);
}

It should ensure that your LED turns off, at least.

ChaseA:
I'm new to Arduino and am having some trouble with this basic code i am trying to make. I want to push a button and have a LED turn on for three seconds then turn off automatically. I'm sure I have this wrong, but please help!!

const int buttonPin = 2;     // the number of the pushbutton pin

const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;        // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);   
}

void loop(){

buttonState = digitalRead(buttonPin);
 
  if (buttonState == HIGH)
  {
    digitalWrite(ledPin, HIGH);
    delay (3000);
    digitalWrite(ledPin, LOW);
  }
  if (buttonState == LOW)
  {
    digitalWrite(ledPin, LOW);
  }
}

ChaseA:
when i push the button,the led just stays on

The code looks like it should work. I suspect you have a hardware problem. You need to have a resistor connected between the button pin and ground to make sure it goes back to 0 when you take your finger off the button.

A simpler approach is to use pinMode( pin, INPUT_PULLUP) which engages an internal resistor that connects the pin to 5v. With that your button should connect between the pin and ground so that pressing it pulls the pin LOW and then you need to swap the logic of your code to match.

By the way, if the output goes low after 3 seconds even when the button is pressed there is no need for code to make the output go low when the button is not pressed.

And I presume you know that taking your finger off the button in the middle of the 3 second delay won't be noticed until the 3 seconds is over. If that matters you need to use the technique in the Blink Without Delay example sketch.

...R