I have been searching but I have not been able to find the right solution yet. I am sure it is out there. But, here is my question:
I have a photocell that detects if the brightness is over an 800 reading then do something. It works great. However, I want to add a timer to it so that it needs to detect if the brightness is over 800 for 10 seconds then do something. Some sort of if photocellReading > 800 for 10000ms then trigger pin 11. I have posted the working code below. Any help in this would be greatly appreciated.
Thanks!
int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the sensor divider
int LEDpin = 11; // pin that triggers a relay
int LEDbrightness; //
void setup(void) {
Serial.begin(9600);
}
void loop(void) {
photocellReading = analogRead(photocellPin); //reading light value
Serial.println(photocellReading); //writing to serial monitor
photocellReading = 1023 - photocellReading;
LEDbrightness = map(photocellReading, 0, 1023, 0, 255); //mapping
analogWrite(LEDpin, LEDbrightness);
int photocellReading = analogRead(A0);
if (photocellReading > 800) {
digitalWrite(LEDpin, LOW); // turn LED on
delay (1000); // turn LED for one second
}
else {
digitalWrite(LEDpin, HIGH); // turn LED off
}
delay(50);
}