Hi
I'm completely new to Arduino and pretty new to electronics in general. I had this idea to create and automatic chicken door. Opens when its light and closes when its dark, pretty simple. I had it going using an LDR and a latching relay with some read switches to stop it on the up and down. The problem was chickens are not that bright and they would always leave it too late to get through the door leaving half of them stuck outside.
My Arduino project was to create a program that would delay the closing by 1 hr after a certain light level.
I use the standard light senor project and added a relay which drives the 2 pole latching relay.
the basis of the program was for it to sense the light level. Once it dropped below a certain value it would record the time using the millis() function and then set a time from the current time plus a specified delay of 1 hr. It would then loop and test if that time was reached.
So far I can get the program to run for up to a value of 32500 mS delay. any longer and the whole thing does nothing, ie if I set the DoorDelay to 33000 or longer it hangs. I can't work out why, is there a limit on the millis function?.
I would appreciate anyone giving me some keywords to look into to make this work, not necessarily after a resolved solution, happy to muddle along learning, just point me in a direction. I have tried searching the forum but when you are complete new its hard to know what to look for as there are so many topics and catagories and jargon words.
Code is below
// Pins
int sensorPin = 0;
int relayPin = 2;
// Variables
int lowThreshold = 400; // Low light threshold to close door
int highTreshold = 520; // upper light threshold to open door
int DoorDelay = 32500; // interval added to current time to close the door, needs to be 1 hr or 3600000
unsigned long currentMillis; // sets a variable to hold the time the light reached low threshold
unsigned long Doorclose = 0; // sets a variable to hold the time to close the door after low light threshold
int DoorPosition = 0; // Stores the current door position 0 open 1 closed
int sensorValue = 0; // Sets the variable to hold the LDR level
void setup() {
// Start Serial & set pin to output
Serial.begin(9600);
pinMode(relayPin,OUTPUT); // relay to switch the latching relay to drive the motor up or down
}
void loop() {
// Read the sensor pin
sensorValue = analogRead(sensorPin);
// If light level is low is detected, set the time to shut the door
if (sensorValue < lowThreshold){
if (DoorPosition == 0){ // Test door is currently open
if (Doorclose > 0){ // Test the door set time has been set
if (millis() >= Doorclose){ // check if door shut time has been reached
digitalWrite(relayPin, LOW); // close door
Doorclose = 0; // Set door close variables back to zero
DoorPosition = 1; // set the door position to closed
}
}
else
{
// If door set time has not been set
currentMillis = millis(); //get the current "time"
Doorclose = currentMillis + DoorDelay; // set the time to close the door from current time plus 1 hr
}
}
// if door is closed already do nothing
}
// If light level goes up again, open the door
if (sensorValue > highTreshold){
digitalWrite(relayPin, HIGH);
DoorPosition = 0; // set door position ready for door close if statement
}
}







