Turn LED on if photoresistor has detected light for 5 minutes

Good Afternoon,

I am trying to determine how to turn an LED on after a Photo resistor has detected a value of 1 or greater for 5 minutes.

My assumption is that I need to increment a variable, if analog input is > 0 and then use delay(1000) to pause for one second and then check the analog input. Once the Variable reaches 300 (5 minutes) than set the LED pin to high.

A couple of items that I am struggling with:

1.) How do I reset the variable back to 0 if the analog value is 0 before the variable reaches 300.
2.) Once the variable reaches 300 I want to turn the LED on and stop the sketch until the arduino is reset or the analog input is at 0.

Here is what I have so far. Any help would be greatly appreciated.

const int analogPin = A0; // Photo Resistor
const int ledPin = 13;

int timerval = 0;

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communications:
Serial.begin(9600);
}

void loop() {
// read the value of the Photo Resistor:
int analogValue = analogRead(analogPin);

// if the timerval = 300, turn on the LED or increment timerval by 1:
if (analogValue > 0) {
++timerval;
delay(1000);

if (timerval = 300){
digitalWrite(ledPin,HIGH);
}

}
else {
digitalWrite(ledPin,LOW);
timerval = 0;
delay(1);
}

Serial.println(analogValue);
Serial.println(timerval);

}

    static unsigned long lastTimeInputWasZero = 0;
    if (analogRead(analogPin) == 0)
        lastTimeInputWasZero = millis();

    if (millis() - lastTimeInputWasZero > 5UL*60UL*1000UL) {
        // It has been five minutes or more since the last time the input was 0
    }
    else {
        // It has been less than five minutes since the last time the input was 0
     }

Thank you very much for this simple solution.

I am now having an issue that when the Arudino is power cycled or turned on for the first time, the second 'if' statement runs five or six times in a row. What are your thoughts on this?

Here is what I have now:

int analogPin = A0; // Photo Resistor
int ledPin = 13;

int timerval = 0;

void setup()
{
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communications:
Serial.begin(9600);
}

void loop()
{

static unsigned long lastTimeInputWasZero = 400;
if (analogRead(analogPin) == 400)
lastTimeInputWasZero = millis();

if (millis() - lastTimeInputWasZero > 5UL60UL1000UL) {
// It has been five minutes or more since the last time the input was 0
digitalWrite(ledPin,HIGH);
Serial.println("LED ON");
}
else {
// It has been less than five minutes since the last time the input was 0
digitalWrite(ledPin,LOW);
//Serial.println(analogRead(analogPin));
}

}

Brooks:
I am now having an issue that when the Arudino is power cycled or turned on for the first time, the second 'if' statement runs five or six times in a row. What are your thoughts on this?

Your initial value of lastTimeInputWasZero makes no sense.

You.also changed the analog value to 400, which means the variable name lastTimeInputWasZero doesn't accurately describe it.

First of all, the thing in the IDE that says it's for cut&paste to the forum is wrong. The code tags button is above the post edit window above the smileys and has a # sign on it.

This line ensures that only when the analogRead is exactly 400 will the lastTime be set to millis(). Is that really what you want?

    if (analogRead(analogPin) == 400)
        lastTimeInputWasZero = millis();

I changed the variable to accurately describe the value.

I have a threshold of about 400 or less with the photo resistor when it is "dark" . Anything over 400 is assumed to have detected light. It will not be exactly 400 though. What are your thoughts?

int analogPin = A0;    // Photo Resistor
int ledPin = 13;

int timerval = 0;


void setup() 
{
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);
}

void loop() 
{

 static unsigned long lastTimeInputWaslessThanFourHundred = 400;
    if (analogRead(analogPin) == 400)
        lastTimeInputWaslessThanFourHundred = millis();

    if (millis() - lastTimeInputWaslessThanFourHundred > 5UL*60UL*1000UL) {
        // It has been five minutes or more since the last time the input was 0
        digitalWrite(ledPin,HIGH);
        Serial.println("LED ON");
    }
    else {
        // It has been less than five minutes since the last time the input was 0
        digitalWrite(ledPin,LOW);
       Serial.println(analogRead(analogPin));
     }
    
  
}

Brooks:
static unsigned long lastTimeInputWaslessThanFourHundred = 400;

I still don't understand why you are assigning 400 as the initial time at which an input was read. 400 is a sensor reading threshold value, not a time.

(analogRead(analogPin) == 400)

lastTimeInputWaslessThanFourHundred

These two things don't match...

Brooks:
I changed the variable to accurately describe the value.

I have a threshold of about 400 or less with the photo resistor when it is "dark" . Anything over 400 is assumed to have detected light. It will not be exactly 400 though. What are your thoughts?

int analogPin = A0;    // Photo Resistor

int ledPin = 13;

int timerval = 0;

void setup()
{
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);
}

void loop()
{

static unsigned long lastTimeInputWaslessThanFourHundred = 400;
    if (analogRead(analogPin) == 400)
        lastTimeInputWaslessThanFourHundred = millis();

if (millis() - lastTimeInputWaslessThanFourHundred > 5UL60UL1000UL) {
        // It has been five minutes or more since the last time the input was 0
        digitalWrite(ledPin,HIGH);
        Serial.println("LED ON");
    }
    else {
        // It has been less than five minutes since the last time the input was 0
        digitalWrite(ledPin,LOW);
       Serial.println(analogRead(analogPin));
     }
   
 
}

My one thought is that "400 or less" would look more like:

    if (analogRead(analogPin) <= 400)

My other thought is that you need to practice examples more before you go for a project.

Those are my thoughts.