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