Using and LDR to trigger a function just once a day....?

I have a little program that opens and closes a door based on LDR thresholds.

My question is how do I modify the code to open based on the LDR reading then stay open for 5 minutes then close until the next morning.

What is tripping me up is that if the door is looking for a light intensity value of say 500 to open....then you give a delay or something of 5 minutes and tell it to close.....how do you get the controller to ignore the fact that the sensor reading is over 500 for the rest of the day.

Here is the code I would like to modify:

/*
 * Change Log
 * 3/16-Changed stepsPerRevolution to 800....motor runs smoothly

*/

#include <Stepper.h>

const int stepsPerRevolution = 800;  //number of steps per revolution (change according to ur motor)

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

#define ldr_pin A0
#define button 2
#define ENA 7
#define ENB 6
int ldr_value=0;
int ldr_average=0;
int total=0;

int speed=60; //motor speed
int threshold=500;  //change me
int lock=0; 

unsigned long duration=1*1*1;//1000*60*5; // 5min average duration  
unsigned long currentTime=0;
int numReadings=0;

void setup() {
 
  myStepper.setSpeed(speed);
  Serial.begin(9600);
  pinMode(button,INPUT_PULLUP);
  pinMode(ENA,OUTPUT);
  pinMode(ENB,OUTPUT);
 myStepper.setSpeed(speed); //set motor speed
 
  //Homing Sequence
 //Turn motor CCW once until it hits limit switch
 digitalWrite(ENA,HIGH);
 digitalWrite(ENB,HIGH);
 while(digitalRead(button)){
   myStepper.step(-1);
   delay(1);
  }
 digitalWrite(ENA,LOW);
 digitalWrite(ENB,LOW);
  Serial.println("Home Sequence Complete");
  
}

void loop() {

ldr_value=analogRead(ldr_pin);
total=total+ldr_value;
ldr_average=total/numReadings;
Serial.print("Average Value LDR: ");
Serial.println(ldr_average);
Serial.print("Value LDR: ");
Serial.println(ldr_value);
numReadings++;
delay(500);

if (millis()-currentTime>duration){
  currentTime=millis();
  numReadings=0;
  ldr_average=0;
  total=0;
  
  // if door is locked ....wait for daylight
if (lock==1){
  if (ldr_value>threshold){  //<---change back to (ldr_average>threshold)
     Serial.println("Day");
     lock=0; //unlocked
  //anticlockwise rotation
  Serial.println("counterclockwise");
  digitalWrite(ENA,HIGH);
  digitalWrite(ENB,HIGH);
 while(digitalRead(button)){
   myStepper.step(-1);
  delay(1);  
  }
  digitalWrite(ENA,LOW);
  digitalWrite(ENB,LOW);
}
    
    }

    //if door is unlocked-----wait for night
  else{
    if (ldr_value<threshold) //<---change back to (ldr_average<threshold)
{
  lock=1; //locked
  Serial.println("Night");
  // clock wise rotation 
  Serial.println("clockwise");
    digitalWrite(ENA,HIGH);
    digitalWrite(ENB,HIGH);
  myStepper.step(8000);
    
    delay(1);
    digitalWrite(ENA,LOW);
    digitalWrite(ENB,LOW);
}
    }
  }

}

how do you get the controller to ignore the fact that the sensor reading is over 500 for the rest of the day.

By detecting when the sensor reading becomes over 500 rather than when it is over 500

Take a look at the StateChangeDetection example in the IDE

Will do.... Thanks!

Basically what you do is read the level frequently and compare it with the previous reading. If both are above the trigger level do nothing. If the new reading is above the trigger level and the previous one was below it then take action

Extra code may be needed to avoid problems if the level changes up and down significantly around the trigger level

Even then, if it gets very dark during the day during a storm, and then the sun comes out again, I can't see how you will get it to tell the difference between that brightening up and the next morning's sun rise.

You could I guess keep track of the time and if the difference between two apparent dawns is less than say 8 hours, ignore the second one?

edit: or what happens if it's overcast at dawn, and it doesn't "see" the sun?

I'd plump for a Real Time Clock.

nielklot.... Don't RTC's require some kind of wifi or something to be reliable?

The rtc in vogue at the moment is the ds3231: look it up and check its precision. Coin cell battery to keep the time if the Arduino power goes off.

Even if it goes out of time by some 10's of minutes eventually, and I mean eveeennntualllly, will that matter in terms of where in the day the event hsppens? Your 5 mins will still be accurate.

If its important that the event happens near dawn, you could btw program that in by looking for the month from the clock. Then open at a different clock time in December compared to June.

There is/was a library that uses an LDR to set and maintain a Circadian clock. I cannot find the library online but have attached it for you to possibly look at.

Circadian.zip (4.1 KB)