help me with time delay and sensor

Hello i have this sensor http://www.geeetech.com/Documents/Infrared%20proximity%20switch%20Manual.pdf and i would like to create a time delay
for example

if(sensor==LOW)
{
digitalWrite(relay,HIGH);
}
if(sensor==HIGH)
{
//delay(5000);
digitalWrite(relay,LOW);
}

when the sensor is low and after the sensor is low the relay stoped after the 5sec

please help me.

Can you explain what you want to achieve and why the code you posted does not work?

yes this code is not working. this is the example who i would create. i want this

sensor on==>relay on==>sensor off==>delay 5sec with the relay on ==>after the 5sec relay off

hi,
im also new at this but it sounds like you want something like this

This is untested code :

const int sensor = 2;     // the number of the sensor pin
const int relay =  3;      // the number of the relay pin

int sensorState = 0;         // variable for reading the sensor status

void setup() {
  pinMode(sensor, INPUT);       // initialize the sensor pin as an input:
  pinMode(relay, OUTPUT);        // initialize the relay pin as an output:
}

void loop(){
  sensorState = digitalRead(sensor);   // read the state of the sensor value:

  if (sensorState == HIGH) {       // if it is, the sensorState is HIGH:
    digitalWrite(relay, HIGH);      // turn relay on:    
    delay(5000);                   // wait 5 seconds
    digitalWrite(relay, LOW);     // turn relay off:
  } 
  else {
    digitalWrite(relay, LOW);     // turn relay off:
  }
}

thinking about it, you could even remove the first digitalWrite(relay, LOW);    // turn relay off: as this could make the relay turn off for a split second while the sensor is still HIGH, and then you should still get about 5 second from the time the sensor go's LOW

no this code is not working.i put now in the arduino and it is not working

I suggest you post the whole sketch that you say isn't working and explain what it actually does. "It isn't working" doesn't give us much to go on.

hi, im just testing the code myself, and it works for me, but i am using a push button, but the time delay can be a bit out, i have changed the line that may turn the relay off and back on even when the input is high.

const int sensor = 2;     // the number of the sensor pin
const int relay =  13;      // the number of the relay pin

int sensorState = 0;         // variable for reading the sensor status

void setup() {
  pinMode(sensor, INPUT);       // initialize the sensor pin as an input:
  pinMode(relay, OUTPUT);        // initialize the relay pin as an output:
}

void loop(){
  sensorState = digitalRead(sensor);   // read the state of the sensor value:

  if (sensorState == HIGH) {       // if it is, the sensorState is HIGH:
    digitalWrite(relay, HIGH);      // turn relay on:    
    delay(5000);                   // wait 5 seconds
  } 
  else {
    digitalWrite(relay, LOW);     // turn relay off:
  }
}

no it not working

what trouble are you having?
also can you supply a wiring diagram?

please help me!!!!!!

sotisanis:
please help me!!!!!!

See reply #9. Quit whining and answer the questions.

the relay dont stoped when the sensor is off

the relay dont stoped when the sensor is off

And you know this how? There is no debug output to show the state of the sensor, so how do you KNOW that the sensor is off?