Evading LDR's confusion when detecting a flash(lightning)

Hello there! I have to make the following arduino program:

“An arduino controls a photocell that is based on an LDR. When the voltage at A0 is higher than 3.5V, the terminal LED should go off ,and when the voltage at A0 is less than 2V the LED should light up.”

This is the “easy” part that i have already done. I’m working with UNO. The problem is what comes next:

“To prevent the lightning from causing false on / off the system must verify that the LDR lighting is kept at least 10 seconds. Any flash of light less than that time will be ignored.”

I have some ideas about this, but I can’t apply them yet. If someone could please help me with this condition, I would really aprecciate it.

This is the program so far:

<#include<SoftwareSerial.h>

int valor=A0;
int conversion,aux=0;
void setup() {
pinMode (valor, INPUT);
pinMode(13,OUTPUT);
Serial.begin(9600);
}

void loop() {
conversion=analogRead(valor)/4;
if (aux!=conversion)
{
Serial.println(conversion);
aux=conversion;
}
if (conversion>=178){
digitalWrite(13,LOW);
}
if (conversion<102){
digitalWrite(13,HIGH);
}
}

THANKS a lot!!

Study the blink without delay example to learn how to time events.

Please use code tags ("</>" button) to post code.

Any flash of light less than that time will be ignored."

So once you have a flash, make a note of the current time by recording the value given by the millis() function.

Then enter a while loop which checks the light is on and that 10 seconds have not expired by subtracting ten seconds from the initial value you got from the millis() function from current value you get from a new millis() cal, and the light is still on.

So the loop will last as long as the light stays on or until 10 seconds has passed. When the loop exits check if the light is still on, if it is it "valid" if not, it is a false alarm.

See the "Debounce" example how to ignore false (short) triggers. Set the debounceDelay as high as the longest lightning stroke will take. Remember that a single lightning flash typically consists of multiple short strokes, and the delay has to cover only one such short stroke. Find out yourself, also how fast your LDR reacts on (stretches?) short strokes.

If you want to check that something is ON continuously you actually need to check for when it is OFF - something like this pseudo code (in loop() )

if (thing is OFF) 
   lastTimeThingWasOff = millis();
   thingOn = false
}
else {
   thingOn = true
}

if (thingOn == true and (millis() - lastTimeThingWasOff >= requiredOnDuration) ) {
  // thing has been ON throughout the time
  // do whatever
}

...R

Thank you a lot! I ended up with this:

#include<SoftwareSerial.h>

int valor=A0;
int conversion,aux=0;
void setup() {
pinMode (valor, INPUT);
pinMode(13,OUTPUT);
Serial.begin(9600);
}

void loop() {
conversion=analogRead(valor)/4;
if (aux!=conversion)
{
Serial.println(conversion);
aux=conversion;
}
if (conversion>=178){
digitalWrite(13,LOW);
}
if (conversion<102){
digitalWrite(13,HIGH);
}
}

A lightning flash lasts about 30 microseconds.

analogRead takes around 100 microseconds.

LDRs are notoriously slow to react.

Please remember to use code tags when posting code.

AWOL:
A lightning flash lasts about 30 microseconds.

analogRead takes around 100 microseconds.

LDRs are notoriously slow to react.

Please remember to use code tags when posting code.

And then there is recovery time.

Paul

pulseLn

Capital l (eye) often looks like lower case l (el).

It’s pulseIn.

Quote: ""To prevent the lightning from causing false on / off the system must verify that the LDR lighting is kept at least 10 seconds. Any flash of light less than that time will be ignored."

This makes no sense to me at all! In 10 seconds you could have more than 50 lightening flashes.

Paul

Use state change detection: record the first time you cross a light level threshold (either up or down), then 10 seconds later change the state of the LED.

LDRs are indeed way too slow to record flashes from lightning.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.... :slight_smile:

AWOL:
A lightning flash lasts about 30 microseconds.

analogRead takes around 100 microseconds.

LDRs are notoriously slow to react.
...

and

wvmarle:
Use state change detection: record the first time you cross a light level threshold (either up or down), then 10 seconds later change the state of the LED.

LDRs are indeed way too slow to record flashes from lightning.

If the OPs intent is to only react to changes that persist longer than 10 seconds, this seems like a hardware problem to me. How about including an LC delay in the light detector circuit that takes some seconds for the output voltage to rise from a LOW value to HIGH when the detector is illuminated. Flashes wouldn't even register.