help with light meter

I was thinking of a project to detect light falls using a lrd, the project should feel sudden falls in the ambient light accompanied by darkness (because a sudden drop of light in a very brigth environment does not necessarily mean darkness) from there to feel the sudden fall of light accompanied by darkness must await the return of the light for a maximum of 5 seconds, if it remains dark should turn on a led.

the design should only feel sudden drops of light so that walking from a light to dark environment should not turn on the led so little the sunset should activate it.
I would like help with the code.

Please post what you have already and let us know where the problems are.

here is the code

bright.ino (430 Bytes)

bright.ino (430 Bytes)

Please format your code nicely (ctrl-T in the Arduino IDE) and then post it between code </> tags instead of as attachment, so it looks like this:

#define led 13
#define sensor A0
#define dark 100
#define limit 600
int bright = analogRead(sensor);
int last_bright = bright;

void setup() {
  // put your setup code here, to run once:
  pinMode(sensor, INPUT);
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
}

void loop() {
  // put your main code here, to run repeatedly:
  if ((last_bright - bright > limit) && (bright < dark)) {
    digitalWrite(led, HIGH);
  }
  delay(100);
  last_bright = bright;
}

like this?

#define led 13
#define sensor A0
#define dark 100
#define limit 600
int bright = analogRead(sensor);
int last_bright = bright;

void setup() {
// put your setup code here, to run once:
pinMode(sensor, INPUT);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
}

void loop() {
// put your main code here, to run repeatedly:
if ((last_bright - bright > limit) && (bright < dark)) {
digitalWrite(led, HIGH);
}
delay(100);
last_bright = bright;
}

Not quite, like this:

#define led 13
#define sensor A0
#define dark 100
#define limit 600
int bright = analogRead(sensor);
int last_bright = bright;

void setup() {
  // put your setup code here, to run once:
  pinMode(sensor, INPUT);
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
}

void loop() {
  // put your main code here, to run repeatedly:
  if ((last_bright - bright > limit) && (bright < dark)) {
    digitalWrite(led, HIGH);
  }
  delay(100);
  last_bright = bright;
}

Note that you should move the top-level code setting up last_bright’s value into the setup()
function - toplevel code executes too early for anything to work properly (except declaring variables).

I think you just need to low-pass filter the brightness with a cutoff of a minute or two, then threshold.