I am trying to build an automatic chicken door for my chicken coop. I am using a light sensor to detect when to open/close (dawn/dusk), upper and lower limit switches to detect door open/close and to stop the motor and a car power window assembly. I have attached a schematic. I had ChatGPT create the original sketch, but what I have now is really no where near that original. It didnt work at all so I have heavily modified it. I currently am able to turn one direction when there is light on the sensor and it will reverse when I cover the sensor. The problem is that it does not stop when I push the limit switches. I have been using the serial monitor and the switches show a change and I am pretty positive I have everything wired correctly. I have decent knowledge of electronics and I'm pretty new to Arduino. I am using and Arduino Uno for development and I will probably be using the Nano on the final project. I have spent hours playing around with the sketch trying to get this to work so I figured it might be time to reach out for some help.
Here is my latest version of my sketch:
#define MOTOR_OPEN_RELAY_PIN 2 // Pin for controlling the open relay
#define MOTOR_CLOSE_RELAY_PIN 3 // Pin for controlling the close relay
#define LIGHT_SENSOR_PIN A0 // Pin for reading the light sensor
#define UPPER_LIMIT_SWITCH_PIN 10 // Pin for detecting the upper limit switch
#define LOWER_LIMIT_SWITCH_PIN 11 // Pin for detecting the lower limit switch
int lightThreshold = 500; // Light threshold to determine when to open/close the door (adjust as needed)
bool isDoorOpen = false; // Variable to track the door state
void setup() {
pinMode(MOTOR_OPEN_RELAY_PIN, OUTPUT);
pinMode(MOTOR_CLOSE_RELAY_PIN, OUTPUT);
pinMode(UPPER_LIMIT_SWITCH_PIN, INPUT);
pinMode(LOWER_LIMIT_SWITCH_PIN, INPUT);
digitalWrite(UPPER_LIMIT_SWITCH_PIN, HIGH);
digitalWrite(LOWER_LIMIT_SWITCH_PIN, HIGH);
digitalWrite(MOTOR_CLOSE_RELAY_PIN, LOW);
digitalWrite(MOTOR_OPEN_RELAY_PIN, LOW);
Serial.begin(9600);
}
void loop() {
int lightValue = analogRead(LIGHT_SENSOR_PIN); // Read the light sensor value
// Check if the door should be opened
if (isDoorOpen == false && lightValue >= lightThreshold) {
digitalWrite(MOTOR_OPEN_RELAY_PIN, HIGH); // Activate the open relay
digitalWrite(MOTOR_CLOSE_RELAY_PIN, LOW); // Deactivate the close relay
isDoorOpen = true; // Update the door state
}
// Check if the door should be closed
if (isDoorOpen == true && lightValue < lightThreshold) {
digitalWrite(MOTOR_OPEN_RELAY_PIN, LOW); // Deactivate the open relay
digitalWrite(MOTOR_CLOSE_RELAY_PIN, HIGH); // Activate the close relay
isDoorOpen = false; // Update the door state
}
if (UPPER_LIMIT_SWITCH_PIN == LOW && lightValue >= lightThreshold) {
digitalWrite(MOTOR_OPEN_RELAY_PIN, LOW); // Deactivate the open relay
digitalWrite(MOTOR_CLOSE_RELAY_PIN, LOW); // Deactivate the close relay
}
if (LOWER_LIMIT_SWITCH_PIN == LOW && lightValue < lightThreshold) {
digitalWrite(MOTOR_CLOSE_RELAY_PIN, LOW); // Deactivate the close relay
digitalWrite(MOTOR_OPEN_RELAY_PIN, LOW); // Deactivate the open relay
}
// Print values on the serial monitor
Serial.print("Light: ");
Serial.println(lightValue);
int UL = digitalRead(UPPER_LIMIT_SWITCH_PIN);
Serial.print("UL SW ");
Serial.println(UL);
int LL = digitalRead(LOWER_LIMIT_SWITCH_PIN);
Serial.print("LL SW ");
Serial.println(LL);
int OPENRELAY = digitalRead(MOTOR_OPEN_RELAY_PIN);
Serial.print("Open Relay ");
Serial.println(OPENRELAY);
int CLOSERELAY = digitalRead(MOTOR_CLOSE_RELAY_PIN);
Serial.print("Close Relay ");
Serial.println(CLOSERELAY);
Serial.print("Door Open? ");
Serial.println(isDoorOpen);
delay(1000); // Delay for 1 second
}


