Event after Analog value High for a timed period.

Hello Guys

I am building a Security light relay board that will will be triggered by various inputs. So far I have been able to get everything working well but now I would like to add "timed" event.

I have a LDR that triggers the relay when a certain threshold is crossed but now I would like the trigger only after the threshold was crossed for a set time.
Some thing like this:

if (LDRValue > 80) // and stays at that value for 3 seconds - how will I do this?
if(stateRelay == HIGH){
      stateRelay = LOW;
      digitalWrite (LED , LOW);
      digitalWrite (CheckPin , HIGH);
    } else {
       stateRelay = HIGH;
       digitalWrite (LED , HIGH);
           }

I have read many posts where button press is used with millis() to time between events but I can figure out how to check for a time elapsed before event.

Can someone maybe help?

Below is the current working code is full if some one would like to use it.

 //Constant Values
const int LDR = A0;
const int Relay = 10;   // The Relay that Controles the Lights
int pinButton = 8;     // The main switch that will be wall mounted (Might be a "bell" Push button
int AlarmRelay = 9 ;  // The Relay that will be triggered when the alarm goes off
int LED = 13;         // The Led that will show that the trigger is recieved ( Else will need to go outside )
int CheckPin = 11;     // This pin is Used to ensure that Timer is only used when the Alarm Relay is activated first
//

//Varialbles
int LDRstate = LOW;
int ALARMstate = LOW ;
int previous2 = LOW;
int stateCheckPin = LOW;

// Values for Alarm trigger
int stateButtonAlarm ;              // to Chek the state of the Alarm Relay
int ledState = LOW;                // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
long OnTime = 100;               // milliseconds of on-time
long OffTime = 5000;           // milliseconds of how long before the light time goes off after Alarm has stopped

// VALUES for LDR
int sensorValue = 0;  // variable to store the value coming from the sensor
int previous = LOW;    // the previous reading from the input pin
long time = 0;         // the last time the output pin was toggled

// Values for Push Button
unsigned long LastOnTime = 0;
boolean RelayState = LOW;
int stateRelay = LOW;
int stateButton;
long debounce = 2000;
 
void setup() {
  pinMode (AlarmRelay,INPUT);
  pinMode(pinButton, INPUT);
  pinMode(Relay, OUTPUT);
  pinMode (LED, OUTPUT);
  pinMode (CheckPin,OUTPUT);
  Serial.begin(9600); //sets serial port for communication

}
 
void loop() {

// Cell Phone Switch Code 

   int LDRValue = analogRead(LDR); // read the value from the LDR
   Serial.println(LDRValue); //prints the values coming from the sensor on the screen

// code for checking the LDR and if the phone is on
  // If the screen is ON...
  if(LDRValue > 80) {

   //  ...and it has been Less than 1 seconds since it was last seen on...
    if (millis() - LastOnTime > 1000UL) {
   if(stateRelay == HIGH){
      stateRelay = LOW;
      digitalWrite (LED , LOW);
      digitalWrite (CheckPin , HIGH);
    } else {
       stateRelay = HIGH;
       digitalWrite (LED , HIGH);
       digitalWrite (CheckPin , HIGH);
    }

    }
   
  LastOnTime = millis();
  }

 // Code for house on off switch so that if the cell phone switch was on it can be swithed off inhouse
   
  stateButton = digitalRead(pinButton);  
  if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
    if(stateRelay == HIGH){
      stateRelay = LOW;
      digitalWrite (CheckPin , HIGH);
     
    } else {
       stateRelay = HIGH;
       digitalWrite (CheckPin , HIGH);
  }
    time = millis();
  }
  digitalWrite(Relay, stateRelay);
  digitalWrite (LED , stateRelay);
 // digitalWrite (CheckPin , stateRelay);
  
    delay (200) ;
  previous == stateButton;

{

// Alarm Code
}
stateButtonAlarm = digitalRead(AlarmRelay);
stateCheckPin = digitalRead(CheckPin); 
   unsigned long currentMillis = millis();
   if((stateButtonAlarm == HIGH  ) && (currentMillis - previousMillis >= OnTime))
  {
    stateRelay = HIGH;  // Turn it on
    previousMillis = currentMillis;  // Remember the time
    digitalWrite (CheckPin , LOW);   // Chage to low so that timer can be used to switch the Light off
    digitalWrite(LED, stateRelay);  // Update the actual LED
    digitalWrite (Relay,stateRelay);
  }
  else if ((stateButtonAlarm == LOW && stateCheckPin == LOW ) && (currentMillis - previousMillis >= OffTime)) // Only activate it Checkpin is Low 
  {
    stateRelay = LOW;  // turn it off
    previousMillis = currentMillis;   // Remember the time
    digitalWrite(LED, stateRelay);    // Update the actual LED
    digitalWrite (Relay,stateRelay);
   
  }
}

This is not a trivial problem, but google search "debouncing". This may give you more insight into what you want to do.

Perhaps something like

if (prev_LDRValue<=80 && LDRValue>80){
  startOnLDR=millis();
  prev_LDRValue=LDRValue;
}

if (LDRValue<80) {
  startOnLDR=0;
  prev_LDRValue=LDRValue
}

if (startOnLDR!=0 and (millis()-startOnLDR) >=3000) {
   //do whatever you need to here
}

Cool thank you so much for responding. I will play with this and give some feed back!!

Look up "state machine" - you need to maintain state for a problem like this, because it involves
behaviour that depends on the history, not just the currently observable readings. In particular you
need record when the state transitions from low to high analog. If the current state is high and
its been at least N ms since it became high, you know its been high continuously for at least N ms.