Please help me modify the basic PIR code.

I just bought a PIR. Everything works great and I just want to tweak it. How do I make it so that the light stays ON for 1 minute after detecting motion, regardless of whether’s any motion.

Then, after 1 minute is up it will shut off automatically if there is no motion. If there is motion at the end of the 1st minute however, the light should stay on for a 2nd minute.

Can anyone show me how to do this in the code? THANK YOU.

/* PIR sensor tester*/

int ledPin = 9; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input

Serial.begin(9600);
}

void loop(){
 
val = digitalRead(inputPin); // read input value


if (val == HIGH) 
{ // check if the input is HIGH
    digitalWrite(ledPin, HIGH); // turn LED ON
    if (pirState == LOW) {
        // we have just turned on
        Serial.println("Motion detected!");
        // We only want to print on the output change, not state
        pirState = HIGH;
    }
}else 
{
    digitalWrite(ledPin, LOW); // turn LED OFF
    if (pirState == HIGH){
        // we have just turned off
        Serial.println("Motion ended!");
        // We only want to print on the output change, not state
        pirState = LOW;
    }
}

}
/* PIR sensor tester*/

int ledPin = 9; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
unsigned long lastDetectTime;

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input

Serial.begin(9600);
}

void loop(){

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


if (val == HIGH) 
{ // check if the input is HIGH
    lastDetectTime = millis();
    digitalWrite(ledPin, HIGH); // turn LED ON
    if (pirState == LOW) {
        // we have just turned on
        Serial.println("Motion detected!");
        // We only want to print on the output change, not state
        pirState = HIGH;
    }
}else 
{
    if (millis() - lastDetectTime > 60000)
        digitalWrite(ledPin, LOW); // turn LED OFF
    if (pirState == HIGH){
        // we have just turned off
        Serial.println("Motion ended!");
        // We only want to print on the output change, not state
        pirState = LOW;
    }
}

}

Ah, I see what you did there. Thank you for introducing me to the handy millis() function. I saw it earlier when Googling for help but did not understand how to implement it until I saw your code! THANK YOU johnwasser!

How long can Arduino run before millis stops counting? Will it run to infinity? Or will I have to reset millis() every once in a while?

Just a note: When doing timing with millis() always subtract the start time from the current time and compare against an interval.

// This version works even if the millis() timer rolls over (which it does every 49 days or so)
if (millis() - startTime >= 1000)  // This will let you know when it has been 1 second since startTime

// This version has problems with roll-over
if (millis() >= startTime + 1000)

Also note that millis() returns a value of type "unsigned long". Storing it in an int or (signed) long will cause problems.

Ah okay, thank you for explaining that to me. :)