How to provide a one second (LOW) output pulse, each time the input returns to normal state

dl324

Joined Mar 30, 2015
16,839
// Note: The photocell has a resistance of 120 ohms with lights applied. (normal mode) (HIGH to pin "A0").
// Note: The photocell has a resistance of 950 ohm when the light is blocked. (LOW to pin "A0").
These statements are meaningless without knowing how the photocell is connected. Normally the photocell (Light Dependent Resistor (LDR)) would be connected as a voltage divider and the voltage at their junction would be used to determine HIGH vs LOW voltage.

Since a LOW output on the LED pin is expected to turn the LED on, this would imply that the output is driving the cathode of the LED which has it's anode connected to V+ through a current limiting resistor.
Code:
if (sensorValue < 700) {
  if (lastState == HIGH) {
    digitalWrite(ledPin, LOW);
    delay(1000);
    digitalWrite(ledPin, HIGH);
  }
  lastState = LOW;
} else {
  lastState = HIGH;
}
The code won't allow a LOW to HIGH transition to turn on the LED. That would execute the else clause for sensorValue < 700. If the state before that was LOW, the code simply sets lastState to HIGH and goes back to reading the sensor value without turning on the LED.

This part of the flow chart won't allow a LOW to HIGH transition to turn on the LED:
1574626985344.png
If the last state of A0 was LOW and it went HIGH on this iteration, it would go to the box that sets state to HIGH and then loops back to read the sensor without turning the LED on.

The second decision element only allows a HIGH to LOW transition on A0 to turn on the LED. If A0 was previously LOW, you take the NO branch which causes A0 to be read again.

If you show how the LDR is connected to A0, we could see what might be causing the LED to turn on when it shouldn't.

TLDR = I didn't look at the video. I'm using a browser that doesn't support HTLM5.
 
Top