Hi All,
I have set up an automatic chicken door which works via a LDR.
My problem is that the door is closing to early when it’s still light.
For the variable daythreshold which at present is 400 I have had it as low as 175
and the door still closes at the same time.
LDR used -
Cadmium Sulphide (CdS) light dependent resistor cell. -
(Similar to Philips ORP12- Dark Resistance:
min10M- Light Resistance:
48k - 140k- Max Dissipation:
30mW- Rise Time: (0~63%) 40mS- Decay Time: (100~37%) 10mS- Dimensions: 5.1mm x 4.3mm ±0.2- Lead spacing: 3.4mm)
Current resistor value is 410K
I have included the code.
// Chicken Coop Code 22/08/2017
// darkthreshold 20 This has nothing to do with controlling the darkness setting
//daythreshold = 400
// Define pins & variables
const int EnMotorPin = 9; // enable motor - pin 9 PWM
const int CloseDoorPin = 8; // direction close motor - pin 8
const int OpenDoorPin = 7; // direction open motor - pin 7
const int photocellPin = A0; // photocell connected to analog 0
int photocellVal; // analog reading of the photocel
bool daylight = true; // daylight status
int darkthreshold = 20;
int daythreshold = 400; // sets hysteresis limits to stop door jittering
int systemState = 0;
int SPEED = 220;
// Reed switches top and bottom of coop door
int bottomSwitchPin = 2;
int topSwitchPin = 4;
bool topSwitchState; // true if door is up
bool bottomSwitchState; // true if door is down
void setup() {
Serial.begin(9600);
pinMode(bottomSwitchPin, INPUT);
pinMode(topSwitchPin, INPUT);
pinMode(EnMotorPin, OUTPUT);
pinMode(CloseDoorPin, OUTPUT);
pinMode(OpenDoorPin, OUTPUT);
}
void loop() {
systemState = 0;
photocellVal = analogRead(photocellPin); // read inputs
if (photocellVal > daythreshold)
{
daylight = true;
}
if (photocellVal < darkthreshold)
{
daylight = false;
}
topSwitchState = digitalRead(topSwitchPin);
bottomSwitchState = digitalRead(bottomSwitchPin);
// read system status
if (daylight == true && topSwitchState == true)
{
systemState = 0; // motor stop
}
if (daylight == true && topSwitchState == false)
{
systemState = 1; // drive door up
}
if (daylight == false && bottomSwitchState == true)
{
systemState = 0; // motor stop
}
if (daylight == false && bottomSwitchState == false)
{
systemState = 2; // drive door down
}
DispVals();
switch (systemState) {
case 0: // motor stop
digitalWrite(CloseDoorPin, LOW);
digitalWrite(OpenDoorPin, LOW);
analogWrite(EnMotorPin, 0);
break;
case 1: //drive door up
digitalWrite(CloseDoorPin, LOW);
digitalWrite(OpenDoorPin, HIGH);
analogWrite(EnMotorPin, SPEED);
break;
case 2: // drive door down
digitalWrite(CloseDoorPin, HIGH);
digitalWrite(OpenDoorPin, LOW);
analogWrite(EnMotorPin, SPEED);
break;
default:
digitalWrite(CloseDoorPin, LOW);
digitalWrite(OpenDoorPin, LOW);
analogWrite(EnMotorPin, 0);
break;
}
}
void DispVals()
{
Serial.print("topSW ");
Serial.print(topSwitchState);
Serial.print("\t");
Serial.print("botSW ");
Serial.print(bottomSwitchState);
Serial.print("\t");
Serial.print("PhotoVal ");
Serial.print(photocellVal);
Serial.print("\t");
Serial.print("daylight ");
Serial.print(daylight);
Serial.print("\t");
Serial.print("systemState ");
Serial.print(systemState);
Serial.print("\t");
if (systemState == 0)
{
Serial.println("\t door stopped");
}
if (systemState == 1)
{
Serial.println("\t door driving up");
}
if (systemState == 2)
{
Serial.println("\t door driving down");
}
}
Can someone please have a look at it and advise where it’s going wrong.
It’s pretty simple I want the door to open at first light and close when it’s dark.
Thanks in advance
Tony

