Switch on LED if the LDR active >=5 seconds??

Please someone help me to write adruino codes for

Switch on LED if the LDR active for >=5 seconds. not immediately.

thanks.

Not sure what you mean by the ldr being "active", although you might mean its reading is over or under some threshold?

Have a look at reply #4 here for a good clue.

spareProfile2:
Not sure what you mean by the ldr being "active", although you might mean its reading is over or under some threshold?

Have a look at reply #4 here for a good clue.

chanaka2134:
Please someone help me to write adruino codes for

Switch on LED if the LDR active for >=5 seconds. not immediately.

Thank you for the help.

I am planing to create light sensitive switch with a LDR

I found codes which immediately switch on the LED once the LDR is in the dark.

const int ledpin = 12;
const int ldrpin = A0;

void setup() {
Serial.begin(9600);

pinMode(ledpin, OUTPUT);
pinMode(ldrpin, INPUT);

}

void loop() {
int ldrStatus = analogRead(ldrpin);
if (ldrStatus >=600){
digitalWrite(ledpin, HIGH);
Serial.println(ldrStatus);
} else {
digitalWrite(ledpin,LOW);
Serial.println(ldrStatus);
}
}

What i really need is to switch on the LED if the LDR in dark for more than 5 seconds duration.not immediately.

That is to avoid unnecessary switching on due to temporary shadows .

Thank you

So what you have to do:

  • detect when the LDR reading drops below a certain value (see the state change detection example in the IDE)
  • after some time switch on the LED (use the millis() timer).

hi,

you can use millis() function to do this.

eg.

if(millis() - time_to_switch_LED >= 5000)
{
//turn ON LED here.

time_to_switch_LED = millis();//reset time to zero for next cycle
}
also don't forget to declare --variable as
long time_to_switch_LED = 0;// in your code's setup

wvmarle:
So what you have to do:

  • detect when the LDR reading drops below a certain value (see the state change detection example in the IDE)
  • after some time switch on the LED (use the millis() timer).

Sorry, im just a beginner and this is my first arduino project. Thank you for your help.

dnyaneshvar:
hi,

you can use millis() function to do this.

eg.

if(millis() - time_to_switch_LED >= 5000)
{
//turn ON LED here.

time_to_switch_LED = millis();//reset time to zero for next cycle
}
also don't forget to declare --variable as
long time_to_switch_LED = 0;// in your code's setup

Thank you for the help. i will try that. thanks