Help adding a time-frame action

Hey guys,

I'm writing a simple piece of code but I've been struggling with it when I trying to figure out how to add a time-frame action.

I'm basically building a laser tripwire with the following code,

 void setup() {
  
Serial.begin(9600);
pinMode(4, OUTPUT);
pinMode(13, OUTPUT);
}

void loop(){
digitalWrite(4, HIGH);

//Serial.println(analogRead(0));

if(analogRead(0) < 800){

digitalWrite(13, HIGH);

} else{
digitalWrite(13, LOW);
}
}

And I just want the alarm - pin 13 - to fire when the analogRead(0) values are lower than 800, for a specific time frame, so there is a kind of a time filter.

I've tried delay but I don't want to "hang up" the code while the delay is running.

Could you help me with this?

There's a most important example in the IDE called " blink without delay"
Have a good look at it.

The sketch is in your IDE. FILE->Examples->2. Digital->Blink Without Delay

Here's the Tutorial page

It's old, has minor errors that won't affect you unless you run it over 3 weeks and it tells you to put a led in pin 13 that is almost certainly on your board already. But the principle holds.

If you want it to 'run forever' then make the time variable that are type long into unsigned long.

This expression: currentMillis - previousMillis

will always return the difference between the current and previous times up the limit of the unsigned integer variable type used.

Just one question? Does it HAVE to be a laser? I have lasers and my biggest worry is that somehow the beam will get reflected or tilted or someone will fall or otherwise cross an eye with that beam. I don't want that or to be standing in court explaining myself.

You don't generally NEED a laser. The 'tripwire' is not the light. It is what the detector sees. If the light is a narrow beam then fine, that has to be aimed right on the detector. But if instead the detector is at the back of a narrow tube then any light becomes 'the beam' which means you don't have to have a visible in dust line for someone to go over or under or play Mission Impossible with. They won't even know where it is if you hide the detector(s). Use an IR led and IR detector or just regular light as long as the detector points at the bulb.

And you don't need to read analog either. A 5mm circle (the detector) will be ON or OFF with human parts blocking or not blocking the light.

With lasers, set up a double-slit and mess with interference patterns. Get holograms. Laser light has neat properties to mess with.

Don't take it out and goof around. if the cops see it they will KNOW it's on a gun and shoot first.
Think about that, when you can get shot for holding a TV remote, what will a laser mean to them?

Thanks for your help guys!

I was able to do it after your help!