Help modifying on-time for arduino controlled IR sensor-lamp

Hi, I have an arduino controlling a lamp with an IR sensor and relay switching the lamp on when motion is detected. Right now the IR sensor detects motion and counts down from 2 minutes regardless if more motion is detected or not and switches off after that. I want to modify the code so that the 2 minute countdown restarts every time motion is detected even if it is in the middle of it's 2 minute duration, and not just have a two minute timer that switches the light off after the 2 minute duration ends. Here is the code - int irmotionPin = 4; //Pin of IR Motion Sensorint relayPin = 8; // Pin of Rela - Pastebin.com

The original code comes from this tutorial- https://www.brainy-bits.com/how-to-connect-and-use-a-relay-module/

Here is the video for anyone interested - Connect a Relay and PIR Motion Sensor to an Arduino - Tutorial - YouTube

All credit goes to them, it was a really fun project and I highly recommend it!

Welcome to the Forum.

You really ought to read the sticky posts at the top of page 1 to get an idea as to how to get full value from your own posts. As an example, cut and paste the code you would like comment on inside code tags. It's a lot easier for you to do it once than to ask those who might help to go link hopping, which most won't do.

smith6967:
I want to modify the code so that the 2 minute countdown restarts every time motion is detected even if it is in the middle of it's 2 minute duration,

That should be straightforward - but you need to post your program here so we can see it.

The way I would do it is like this pseudo code

if (movementDetected == true) {
   timerStartMillis = millis();
   if (lamp is off) {
      turn lamp on
  } 
}

if (millis() - timerStartMillis >= required duration) {
   turn lamp off
}

...R

Hello,

First thing to know, the sensor has 2 modes of operation,

  • Only one pulse at the beginning of presence detection, and

  • Continuous pulses while there are movements.

Your program needs to have a timer, which can suffer a count reset to each pulse, so the sensor must be set to send pulses to each movement.

The timer part is very simple, you can use a time of 1 second, and a count of up to 120 seconds, then when the sensor sends a pulse to the variable being incremented from 0 to 119 (it can be 8 bit variable) receives a reset, when the variable reaches a value above 119, the relay switches off.

In reality I think arduino is not necessary, the sensor can be configured to send only one pulse (and while there is movement its internal timer will keep the output active) and so the relay could be controlled directly by the presence sensor.

Maybe this will help: