How to set a time limit for a void loop?

I'm running a loop to constantly check a motion sensor. Once the motion is detected I want to send a signal. (Here it turns an LED on.) I want it to stay on as long as there is motion in range, but to turn off immediately (or within a specified time limit) once there's no motion.

I would want the signal off within say, 200 milliseconds. (1/5 of a second) However, the LED and the signal stays on for about 1500 milliseconds (1.5 seconds) as seen in the serial monitor attachment. I've tried setting the LED pin to LOW immediately after setting it to HIGH but I still get the 1.5 second delay.

#include <arduino-timer.h>
byte sensorPin = 3;
int ledPin9 = 9;   //the pin the LED is connected to
                  //we are controlling brightness so 
                  //we use one of the PWM (pulse width
                  // modulation pins)
unsigned long DELAY_TIME = 500; // 1500 = 1.5 sec
unsigned long delayStart = 0; // the time the delay started
bool delayRunning = false; // true if still waiting for delay to finish
bool ledOn = false; // keep track of the led state
byte state = digitalRead(sensorPin);  

void setup()
{
  pinMode(sensorPin,INPUT);
  pinMode(ledPin9, OUTPUT); //sets the led pin to output
  Serial.begin(9600);
  
  digitalWrite(ledPin9, LOW); // turn led off
  ledOn = false; 

   // start delay
  delayStart = millis();
  delayRunning = true; 
}

void magic()
{
  if(state == 1)
    {
      Serial.println("Somebody is in this area!");
      digitalWrite(ledPin9, HIGH);   
    }
  else
  {
    Serial.println("No one!");
    digitalWrite(ledPin9, LOW);
  }

  /*
  delay(DELAY_TIME);
  if (delayRunning && ((millis() - delayStart) >= DELAY_TIME)) 
  {
    delayStart += DELAY_TIME; // this prevents drift in the delays
  }
  */
}

void loop()
{
  state = digitalRead(sensorPin);  
  digitalWrite(ledPin9,state);
  magic();
  delay(500);
  digitalWrite(ledPin9, LOW);
}
//

i don't see any lag when I run your program. but i configured the input as INPUT_PULLUP

a motion sensor.

What sensor? Please post a link to the specs of the sensor, and a schematic showing how you wired it to the Arduino (hand-drawn is fine).

If you mean a PIR sensor module, these often have an adjustable on-time, set by a small trim-pot, which may be set to 1.5s. Your sketch may be switching the LED off after the trigger, but is immediately switching the LED back on again because the pin is still high. Maybe you need to have your sketch detect the state of the pin changing from LOW to HIGH, rather than simply detecting its current level.

Please do not post screen images of the serial monitor. You can simply copy the text from there and paste it into your post between code tags.

+1 karma for posting your code in code tags!

Keyes Studio PYE-IR sensor.

I can't get a text copy of the serial monitor because the info disappears when I stop the program. I looked into other ways to capture it before posting, I don't want to have to install another app just to do that. No, CTRL-C does not work in this case.

Screen Shot 2020-10-12 at 1.06.03 PM.png

Screen Shot 2020-10-12 at 1.06.03 PM.png

Here is the link to the component I asked you to post. It says:

Output Delay Time (High Level): About 2.3 to 3 Seconds

Could that explain the problem?

Very well, thank you. Never thought to look there. That info wasn't in the pdf I downloaded from them that had the code and whatnot.

I've tried several iterations for the sensor I want -- a photoelectric sensor and an ultrasonic one, but neither of those have the range I want. I wanted to make it relatively cheap as I need four of them for a game I'm creating. I've already done it with physical pushbuttons (don't even need an Arduino there!) but wanted to go wireless.

The kit I have has 48 different sensors -- maybe I can try something else!

BTW, that wiki entry has a screen shot of the Serial Monitor too, rather than a text entry. :smiley:

Maybe someone can tell me how to cut that response time down? After all, if you can "overclock" a computer...

alfaniner:
Maybe someone can tell me how to cut that response time down?

Don't use delay().

-jim lee

You can get slightly larger PIR sensors which are adjustable and have an option to select a repeatable trigger. I would get one and experiment.

jimLee:
Don't use delay().

I agree you should be removing those delays. But with your curent sensor, I don't think that will be enough.

I can't get a text copy of the serial monitor because the info disappears when I stop the program

Don't stop the program, instead deselect Autoscroll then use the mouse and left button to select the required text and CTRL-C to copy it to the clipboard and paste it here in code tags

I would want the signal off within say, 200 milliseconds. (1/5 of a second) However, the LED and the signal stays on for about 1500 milliseconds (1.5 seconds) as seen in the serial monitor attachment. I've tried setting the LED pin to LOW immediately after setting it to HIGH but I still get the 1.5 second delay.

You can do that with your existing sensor. I tried to explain how in post #2, but I'm not sure you picked up on it. You can have your signal go off after 200ms. What you can't have, because of your sensor, is have it re-trigger within 2~3 seconds of the previous trigger.