PIR sensor with timer, first time and need help

Hi..i have just bought an arduino uno yesterday and im in need of an assistance.. Im just trying to create a PIR motion light sensor, but having problems with the timer. Note, i am very very new at this.. and i am really weak with the coding. I want my sensor to detect someone walking inside, and automatically on the LED. So far, i am only able to have it on for 5 sec(since i put a delay). But i want it to only light up when there is motion, and when there is no motion for 5 secs, it will turn off. The loop goes on and on.Kind of like in a meeting room, when there's somebody in there, it lights up, and when there isn't,it goes off. I have attached my schematic down below.
P/S : i've already checked Arduino Forum , but im not really sure if it is the same. Any help is appreciated, thank you. Be nice pls XD. here is my coding so far;

int ledPin = 13;// choose the pin for the LED
int pirPin = 2;               // choose the input pin (for PIR sensor)
int val = 0;                    // variable for reading the pin status

void setup() {

  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(pirPin, INPUT);     // declare sensor as input 

  

}

void loop(){

  val = digitalRead(pirPin);  // read input value

  if (val == HIGH) {            // check if the input is HIGH

    digitalWrite(ledPin, HIGH);  // turn LED ON
    delay(5000);

  } else {

    digitalWrite(ledPin, LOW); // turn LED OFF

    delay(300);    
  }

}

Moderator edit: code tags

a little help? anyone?

You'll need to get rid of the delays and use millis instead. Take a look at the blink without delay example to see millis in use. For your code, whenever the PIR is high, store millis in a variable and turn the LED on. Independently, check that variable against millis to see whether five seconds have passed and if so, turn the LED off.

If I were you, I'd use the technique demonstrated in the 'blink without delay' sketch to turn the output off after a delay.

// incomplete, untested

void handleMotionSensor()
{
    static unsigned long lastMotionTime  = 0;

    if(motionDetected())
    {
        lastMotionTime = millis();
        lampOn();
    }
    else
    {
        if(millis() - lastMotionTime >= LampDuration)
        {
            lampOff();
        }
    }
}

yeah i've went through the whole forum ( sigh) and came up with this coding like the guy above me posted..the only problem is..when i put my hand for a short while(1-3 secs)..it turns off 2 seconds later..nothing wrong there, but ive noticed if i put my hand a little while longer(lets say 10 secs), the time it takes for it to turn off is longer than 2 secs..any ideas why?

int ledPin = 13;// choose the pin for the LED
int pirPin = 2; // choose the input pin (for PIR sensor)
int val = 0;
unsigned long detectTime;

void setup() {

Serial.begin(9600);
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(pirPin, INPUT); // declare sensor as input

}

void loop(){

val = digitalRead(pirPin);
// read input value

if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH);
detectTime = millis();
// turn LED ON
}

if ( (millis() - detectTime) == 2000){

digitalWrite(ledPin,LOW);
}

}

if ( (millis() - detectTime) == 2000)

You want to use >= here rather than ==, because this code might not execute just as the difference exactly equals 2000.

You might want to add another led that lights when motion is detected and turns off when there is no motion. This is just for debugging. Or you could use serial print. The reason the light stays on longer than your two seconds may be because the pir sensor is still detecting motion. Depending on your motion sensor, it's on board micro-controller may be programmed that way.

I need to put serial print to determine when motion starts and ends? So it is most probably then sensor and not the coding right? I'm gonna give it a shot and we'll see how it goes..thanks guy

aveaqim:
I need to put serial print to determine when motion starts and ends? So it is most probably then sensor and not the coding right? I'm gonna give it a shot and we'll see how it goes..thanks guy

Yes, with the serial print, you can see when motion starts and stops. Or you can do the same thing with an additional LED so you can look at it instead of the computer screen. Your preference. Let us know how it goes.