how i add time delay if LDR value 80 relay active....

how i add time delay if LDR value 80 relay active after 5 second…

const long headlightDelay = 5000; // 5 Seconds (Adjust as needed)

const int LDR = A0; // Light resistor
const int ignition = 2; // Ignition switch HIGH==On LOW==Off
const int strobe = 13; // Alarm strobe LED
const int sideLights = 3; // Sidelight LED
const int headLights = 4; // Headlight LED

int ldrAverage = 0; // Rolling average of LDR value to smooth out readings
long headlightOnTime = 0; // Time headlights were turned on
boolean sidelightState = false; // Flag, state of sidelights
boolean headlightState = false; // Flag, state of headlights

void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(sideLights, OUTPUT);
pinMode(headLights, OUTPUT);
pinMode(strobe, OUTPUT);
pinMode(ignition, INPUT_PULLUP);
ldrAverage = analogRead(LDR); // Prime the average
}

// the loop routine runs over and over again forever:
void loop() {
Serial.println(“Ignition on”);
while (digitalRead(ignition) == HIGH){ // Only operate lights if ignition is on
ldrAverage = (ldrAverage + analogRead(LDR))/2; //Read the analog sensor
//Serial.print(“Light Level:”);
//Serial.println(ldrAverage); // Send the ldrAverage to the Serial Monitor when available
delay(50); // delay in between reads for stability
if ((ldrAverage < 80) && (sidelightState == false)) { // If the ldrAverage is 20 or less
digitalWrite(sideLights, HIGH); // turn on LED
sidelightState = true;
Serial.println(“Sidelights On”);
}
if ((ldrAverage > 120) && (sidelightState == true)) { // If the ldrAverage is 30 or higher
digitalWrite(sideLights, LOW); // turn off LED
sidelightState = false;
Serial.println(“Sidelights Off”);
}
if ((ldrAverage < 40) && (headlightState == false)) { // If the ldrAverage is 10 or less
digitalWrite(headLights, HIGH); // turn on headLights
headlightState = true;
headlightOnTime = millis(); // Get time lights were turned on
Serial.println(“Headlights On”);
}
if ((ldrAverage > 80) && (headlightState == true)) { // If the ldrAverage is 20 or higher
//long tmp = millis();
//Serial.println(headlightOnTime);
//Serial.println(tmp);
if ((millis() - headlightOnTime) > headlightDelay){
digitalWrite(headLights, LOW); // turn off headLights
headlightState = false;
Serial.println(“Headlights Off”);
}
}
}
headlightOnTime = millis(); // Get time ignition was turned off
// Ignition is off
Serial.println(“Ignition off”);
while (digitalRead(ignition) == LOW){ // Only operate if ignition is off
if (headlightState == true) { // If headlights on
if ((millis() - headlightOnTime) > headlightDelay){
digitalWrite(headLights, LOW); // turn off headLights
headlightState = false;
Serial.println(“Headlights Off”);
digitalWrite(sideLights, LOW); // turn off LED
sidelightState = false;
Serial.println(“Sidelights Off”);
}
}
digitalWrite(strobe, HIGH); // turn on alarm strobe
delay(20);
digitalWrite(strobe, LOW); // turn off alarm strobe
delay(1980);
}
ldrAverage = analogRead(LDR); // Re-prime the average

}

Arduino Programming Course

imran1only:
how i add time delay if LDR value 80 relay active after 5 second…

I don’t understand the question.

Do you mean that the LDR should be above 80 for 5 seconds before the relay activates

OR

Do you mean that the relay activates the instant the LDR reaches 80 and the relay then stays on for 5 seconds even if the LDR falls below 80?

Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

…R

i mean if LDR value above 80 add delay for 5 seconds before the relay activates

imran1only:
i mean if LDR value above 80 add delay for 5 seconds before the relay activates

That is still a little unclear. I assume you want to check that the value stays above 80 throughout the 5 seconds, but your words could also mean that you want to wait 5 seconds from the instant the value first reaches 80.

Assuming you want the value to be above 80 for 5 seconds, this is how to do it (it may seem like the logic is upside down)

if (ldrValue < 80) {
   lastTimeLdrLow = millis(); // reset the time every time the value is too low
}

if (millis() - lastTimeLdrLow >= 5000) { 
  // LDR has been high for 5 seconds
  // do something
}

…R