DELAY

hi. im learning about arduinos but i dont realy know what i am doing when question is code !

i was trying do a small sketch about delay but doesn't work:

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

int buttonState = 0;        

void setup() { 
  pinMode(ledPin, OUTPUT);       
  pinMode(buttonPin, INPUT);     
}

void loop(){  
  buttonState = digitalRead(buttonPin);


  if (buttonState == HIGH) {        // turn LED on:    
    digitalWrite(ledPin, HIGH); 
    delay(5000);
    digitalWrite(ledPin, LOW);

  }
}

if you see its like button example ,but I add delay for trigger an selenoid during 5 secs if wireloop from pin 2 is open(its pull-up to +5v). i have the wire loop from pin 2 Normally closed and when i open the wire it blinks like say in the code ,BUT don't start any delay until i close the circuit again ,and than it count 5 second until led goes off. Why the hell it only start counting after close wire and not when i open the circuit? is the code saying(second i understand) if buttonstate(wire loop from gnd to pin2) is high ,led goes on and after 5second it shoud turn off even with buttonstate HIGH!? right?? C language always kill l me:/

sorry about my inglish

I am unclear how things are hooked up. I suggest you use the INPUT_PULLUP argument to set pin 2 at a known state. That known state will be normally HIGH with nothing connected to the pin, so you want to change your if condition to buttonState==LOW. That way when you ground pin 2 it should fire the LED.

If the button pin is tied to 5V as you say then the LED will immediately turn on and appear to stay on because buttonState = digitalRead(buttonPin);will return HIGH. Only when you press the button for more than 5 seconds will it turn off becausebuttonState = digitalRead(buttonPin);will return LOW.

that is the schematic. that loop is an N:C alarm circuit. if someone open or cut it. triguer that solenoit for 5seconds. at least is what i want to try..

the problem is right now, as the code is .it trigger pin13 when open,but only count 5 seconds nd turn led off after I close that loop circuit again... it should react as UKHeliBob say..but not

You need to unplug all the output crap. Start with the state change detection example, to learn when the switch BECOMES (NOT is) pressed (or released). When you KNOW the switch is working correctly, using Serial.print(ln)(), then you can make the output do what you want.

i will start from beginning again with this example:arduino.cc/en/Tutorial/InputPullupSerial after understand what im doing wrong , i will try make code again for my project, i even dont know there were a serial monitor on arduino ide :blush: :P i think i started from de end:P

i think i started from de end:P

Everybody has to start somewhere. It's easier to start at the beginning, though. 8)

Good luck. With your attitude, though, I don't think you'll have any real troubles. Just take it one step at a time. Get one piece of hardware OR one piece of code working at a time, and sooner than you think, with a lot less frustration, the project will be complete.