if statement activates only if more than 5 seconds

Hello, I have a project i'm working on which activates when a LDR senses x amount of light intensity. For example:

if(sensorValue >= 850)
{
digitalWrite(ledPin1, HIGH);
delay(100);
}
.
I am try to figure out how to do this but only if sensor value is high for 5 seconds.
So if LDR sensorValue is 850 for 5 seconds, then it will activate ledPin1, else nothing happens.

You can try this

if (senosr > 850 ) && (!(started)) {
	elapsedTime=millis();
	started=1;
}
else {
    started=0;
    elapsedTime=0;
}    
    
if ((millis()-elapsedTime > 5000 ) && started ) 
                 do something

Sorry, I was editing.

noweare:
You can try this

if (senosr > 850 ) && (!(started)) {

elapsedTime=millis();
started=1;
}
else {
   started=0;
   elapsedTime=0;
}    
   
if ((millis()-elapsedTime > 5000 ) && started )
                do something

this is what i did, i will show you what i did below (my system works if 3 ldr sensors are all above 850)

int started;
int elapsedTime;

if(sensorValue >= 850 && sensorValue2 >= 850 && sensorValue3 >= 850 && !(started))
{
elapsedTime=millis();
started=1;
}
else {
started=0;
elapsedTime=0;
}
if ((millis()-elapsedTime > 5000 ) && started )

{

digitalWrite(ledPin1, HIGH);
delay(100);
lcd.setCursor(2,0);
lcd.print("Park in Area");
delay(100);
myservo.write(160);
delay(100);
lcd.setCursor(8,1);
lcd.print("B");
delay(6000);
lcd.clear();
delay(1000);
myservo.write(30);
delay(100);
digitalWrite(ledPin1, LOW);

}

I think the else needs to have a condition added like this
otherwise if the sensor is above 850 and started the if will
reset the timer.

if (sensor > 850 ) && (!(started)) {
 elapsedTime=millis();
 started=1;
}
else if (sensor < 850 )
    started=0;
    elapsedTime=0;
}

Hey thanks alot you are a genius hehe, i tried to figure this out for days :slight_smile:

2donboi2:
I am try to figure out how to do this but only if sensor value is high for 5 seconds.
So if LDR sensorValue is 850 for 5 seconds, then it will activate ledPin1, else nothing happens.

IMHO the simplest way is to go about this backwards - reset the clock every time the value is too low. Then if the clock has been running for 5 seconds you know the sensor has been high all that time. Like this

if (sensorValue < 850) {
  lastTimeSensorLow = millis(); // reset the clock
}

if (millis() - lastTimeSensorLow >= sensorInterval) {
   // do stuff as the sensor has been high throughout the interva;
}

…R

karma added

Robin2:
IMHO the simplest way is to go about this backwards - reset the clock every time the value is too low. Then if the clock has been running for 5 seconds you know the sensor has been high all that time. Like this

if (sensorValue < 850) {

lastTimeSensorLow = millis(); // reset the clock
}

if (millis() - lastTimeSensorLow >= sensorInterval) {
  // do stuff as the sensor has been high throughout the interva;
}




...R

It doesnt work. Maybe its because im sort of newbie

here is exactly what im doing…

int lastTimeSensorLow;
int sensorInterval;
int elapsedTime;
int started;

if((sensorValue >= 850 && sensorValue2 >= 850 && sensorValue3 >= 850) && (!(started)) )
{
elapsedTime=millis();
started=1;
}

if (sensorValue < 850)
{
lastTimeSensorLow = millis();
}

if (millis() - lastTimeSensorLow >= sensorInterval)

{
bla bla
}

No. With Robin’s solution, you don’t need or want the “started” variable at all.

2donboi2:
It doesnt work. Maybe its because im sort of newbie

here is exactly what im doing...

int lastTimeSensorLow;

int sensorInterval;
int elapsedTime;
int started;

All of those time variables need to be unsigned long, not int or things will not work after a minute or so...

blh64:
All of those time variables need to be unsigned long, not int or things will not work after a minute or so…

Thanks you, I now have the solution. Thank you guys.