Turn on LED with LDR , Millis() "State of Change"

I am attempting to turn on a LED with State of Change on a LDR and Millis()

Basically the LED must be turned on when the LDR Read out is > 800 for longer than 3 seconds else it should be turned of. I have looked at some of the de bounce coding and had some help from the guys on this form but I would just like to check if I am missing something here:

int LDR = A0; // select the input pin for ldr
const int LED = 8;
int LDRValue = 0; // variable to store the value coming from the sensor
int prev_LDRValue = 0; // variable to store the previous state of the sensor

// The timers for the check of the LDR
long startOnLDR = 0; // The Last time that input was recieved

void setup() {
pinMode (LED, OUTPUT);
Serial.begin(9600); //sets serial port for communication
}
void loop() {
LDRValue = analogRead(LDR); // read the value from the sensor
Serial.println(LDRValue); //prints the values coming from the sensor on the screen

if (prev_LDRValue<=800 && LDRValue>800){// New Code to start Timing
startOnLDR=millis();// Start the Timer
prev_LDRValue=LDRValue;
}
if (LDRValue>800) {
startOnLDR=0;
prev_LDRValue=LDRValue;

if (startOnLDR!=0 and (millis()-startOnLDR) >=1000) {
 digitalWrite (LED , HIGH);
}else{
 digitalWrite (LED , LOW);  
}
delay(100);
{
}
}
}

Best regards
Paul

Whilst the compiler doesn’t care about code format, humans do, so a little indentation helps a lot to visualise your code.

Try to think how you would do this, with only a clock on the wall (with a second hand), a lightmeter, a sheet of paper and pencil.

It also doesn’t really matter what the last reading was, only that the current reading is not lower than the threshold.

Your first if statement check for LDR has gone from below 800 to above 800, and set startOnLDR to millis.

if (prev_LDRValue<=800 && LDRValue>800){// New Code to start Timing
startOnLDR=millis();// Start the Timer
prev_LDRValue=LDRValue;
}

Your second if statement check for LDR above 800, and if so, you set startOnLDR back to zero.

if (LDRValue>800) {
startOnLDR=0;
prev_LDRValue=LDRValue;

Inside this second if statement you have a third if statement, and it compares startOnLDR not equal 0.

So, if your first if is true, your second will also be true, and your third is never true. Led will never turn on.

I think you want < in second if statement, and use a closing bracket } before third if statement.

Gabriel_swe:
Your first if statement check for LDR has gone from below 800 to above 800, and set startOnLDR to millis.

if (prev_LDRValue<=800 && LDRValue>800){// New Code to start Timing

startOnLDR=millis();// Start the Timer
prev_LDRValue=LDRValue;
}





Your second if statement check for LDR above 800, and if so, you set startOnLDR back to zero.


if (LDRValue>800) {
startOnLDR=0;
prev_LDRValue=LDRValue;




Inside this second if statement you have a third if statement, and it compares startOnLDR not equal 0.

So, if your first if is true, your second will also be true, and your third is never true. Led will never turn on.

I think you want < in second if statement, and use a closing bracket } before third if statement.

Ah Now it all makes sense! The moment to saw your comment it all came together! Thank you so much for spending the time to explain where I went wrong. Now for the next step to keep the LED on after the LDR goes to low again

If some one is looking at this later the working code is as follow

int LDR = A0; // select the input pin for ldr
const int LED = 8;
int LDRValue = 0; // variable to store the value coming from the sensor
int prev_LDRValue = 0; // variable to store the previous state of the sensor

// The timers for the check of the LDR
long startOnLDR = 0; // The Last time that input was recieved

void setup() {
pinMode (LED, OUTPUT);
Serial.begin(9600); //sets serial port for communication
}
void loop() {
LDRValue = analogRead(LDR); // read the value from the sensor
Serial.println(LDRValue); //prints the values coming from the sensor on the screen

if (prev_LDRValue<=800 && LDRValue>800){// New Code to start Timing
startOnLDR=millis();// Start the Timer
  prev_LDRValue=LDRValue;
}
if (LDRValue<800) {
startOnLDR=0;
  prev_LDRValue=LDRValue;}
  {
if (startOnLDR!=0 and (millis()-startOnLDR) >=3000) {
 digitalWrite (LED , HIGH);
}else{
 digitalWrite (LED , LOW);  
}
delay(100);
{
}
}
}

Thanks for this post. It was exactly the help I needed also.

Why is there a delay(100) down the bottom?
You went to the trouble to implement millis() timing…
-and-
The next lesson will discuss indentation.