Auto Door Programming Advice

I dont know how to program very well, so I was hoping someone would take a look at what I have done so far and give me suggestions or let me know if I am doing something wrong per my description below.

The project is an automated pet door: I have a motor and a photocell. When the sun comes up, I would like for the motor to operate in one direction(door open), then when the sun goes down, it needs to go the other direction(door closed). Everything seems to work with what I have wrote so far, but I just want to see if anyone has any suggestions for me. I added a delay so the light switch has to detect light for a constant 5min before the motor is energized.

Also, I would like to add a manual switch that would override the photocell

Like i said, I dont know how to program, so if this code does not operate the way I’m describing it, please let me know and any suggestions would be greatly appreciated!!!

int relay1 = 6;
int relay2 = 7;

int motor_on = 300;
int motor_off = 25;

void setup() 
{
 Serial.begin(9600);
 pinMode(relay1,OUTPUT);
 pinMode(relay2,OUTPUT);
}

void loop()
{
int sensor = analogRead(0);
 if (sensor > motor_on){       //Door Open
   delay(10000);
   digitalWrite(relay1, LOW);
   digitalWrite(relay2, HIGH); 
 }
 if (sensor < motor_off){     //Door Close
   //delay(5000);
   digitalWrite(relay1, HIGH);
   digitalWrite(relay2, LOW);
 }
   Serial.println(sensor,DEC);
   
}

Please use [ code ] tags next time.

Why do you find that the photocell has crossed the threshold and then wait 10 seconds? Why not keep checking that it remains below the threshold for all of those 10 seconds? That way you can eliminate some false triggers like shadows.

Is there a limit switch that stops the motor when it reaches the end position? Your Arduino code never stops the motor.

MorganS:
Please use [ code ] tags next time.

Why do you find that the photocell has crossed the threshold and then wait 10 seconds? Why not keep checking that it remains below the threshold for all of those 10 seconds? That way you can eliminate some false triggers like shadows.

Is there a limit switch that stops the motor when it reaches the end position? Your Arduino code never stops the motor.

I was using 10 seconds when I was testing the code, but I plan to change it to 5 min. What I was trying to accomplish was to have 5min of solid light before it opened the door which I thought would eliminate shadow, or flashlight or whatever else.

Yes there are limit switches on the door which take power off the motor when tripped.

How would I need to change the code for it to continue checking that it stays below or above the threshold for a given period of time?

bslaughter5:
. I added a delay so the light switch has to detect light for a constant 5min before the motor is energized.]

This code

if (sensor > motor_on){       //Door Open
   delay(10000);

does not do that. It measures the light once and then does something 10 seconds later.

To check the light level for 10 seconds you need something like this pseudo code

void loop() {
  if (lightLevel is too low) {
     startMillis = millis();
  }
  if (millis() - startMillis > 10000) {
     // light has been high for 10 seconds
     // do your stuff
  }
}

...R

Robin2:
This code

if (sensor > motor_on){       //Door Open

delay(10000);



does not do that. It measures the light once and then does something 10 seconds later.

To check the light level for 10 seconds you need something like this pseudo code



void loop() {
  if (lightLevel is too low) {
    startMillis = millis();
  }
  if (millis() - startMillis > 10000) {
    // light has been high for 10 seconds
    // do your stuff
  }
}




...R

Thank you for the advice, I will try that out.

Would I be able to use the code like you suggested then add a delay on the serial print line and test it? I would like to cover the sensor and see the serial output to confirm that it has to have light, or dark for the specified period of time.

I tried doing it with the delay on my original code, but the delay would actually cause the serial print line to delay also, so I never knew what my threshold actually was.

bslaughter5:
Would I be able to use the code like you suggested then add a delay on the serial print line and test it?

In general, yes. But don't use delay() because the Arduino can do nothing during a delay(). Use millis() to manage the timing without blocking as illustrated in Several Things at a Time

...R

bslaughter5:
I tried doing it with the delay on my original code, but the delay would actually cause the serial print line to delay also, so I never knew what my threshold actually was.

Print first, delay second. For testing, a delay of a few seconds to slow down output to serial monitor is not a problem.