Photocell - trigger pin if reading is over threshold for X seconds - Help

Howdy all!

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);
}

You could try something like this

const byte photocellPin = A0;     // the cell and 10K pulldown are connected to a0
const byte Over800MonoflopPin = 12;
const byte LEDpin = 11;
const byte TriggerPin = 10;

void setup(void) {
  pinMode(LEDpin, OUTPUT);
  pinMode(Over800MonoflopPin, OUTPUT);
  digitalWrite(Over800MonoflopPin, HIGH);
  pinMode(TriggerPin, OUTPUT);
  Serial.begin(115200);
}

void loop(void) {
  static unsigned long lastPrinted;
  static unsigned long ledStarted;
  static unsigned long firstOver800;
  static int lastPhotocellReading;

  unsigned long topLoop = millis();
  int photocellReading = analogRead(photocellPin);
  byte LEDbrightness = map(photocellReading, 1023, 0, 0, 255);
  analogWrite(LEDpin, LEDbrightness);

  if (photocellReading > 800) {
    if (lastPhotocellReading <= 800) {
      firstOver800 = topLoop;
    }
    if (firstOver800 && topLoop - firstOver800 > 10000) {
      digitalWrite(TriggerPin, HIGH);
    }
  } else {
    firstOver800 = 0;
    digitalWrite(TriggerPin, LOW);
  }
  lastPhotocellReading = photocellReading;
  if (topLoop - lastPrinted > 1000) {   // print once a second
    Serial.print(F("photocell "));
    Serial.print(photocellReading);
    Serial.print(F(" LED "));
    Serial.println(LEDbrightness);
    lastPrinted = topLoop;
  }
  if (digitalRead(Over800MonoflopPin) == HIGH && photocellReading > 800)  {
    digitalWrite(Over800MonoflopPin, LOW);
    ledStarted = topLoop;
  }
  if (digitalRead(Over800MonoflopPin) == LOW && topLoop - ledStarted > 1000) {
    digitalWrite(Over800MonoflopPin, HIGH);   // turn LED off
  }
}

Wow thanks for this! Ill give it a try!