Hi There,
Please remember I am a beginner so a lot of the above is beyond me.
So what I can do is list all the components I used and the code (see below).
Then if you would be so kind as to look it over and see if there may is an issue
with the configuration and make any possible suggestion to get it working.
At present I have a chicken coop ready and waiting!
Components -
Arduino Uno - Clone
Dual H Bridge Stepper Motor Drive Controller Board Module For Arduino L298N - L298N Stepper Motor Drive Controller Board Dual H Bridge for Arduino @ for sale online | eBay
DC 12.0V 45rpm 15.5mm Gear Motor Chihaimotor Silver - http://www.ebay.com.au/itm/132057391891?_trksid=p2057872.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT
125V-250V Micro Roller Lever Arm Limit Switch - http://www.ebay.com.au/itm/222270162986?_trksid=p2057872.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT
Power source for testing
Thanks
Tony
//Locking, Automatic Chicken Coop Door
//By Seth Johnson Land To House llc 2016
//This section is for initializing things:
// Initialize the motor values to the pins 8, 9, 10.
//Pins 8 and 9 send high or low to the motor controller.
//pin 10 enables motor one on the motor controller.
int enA = 10;
int in1 = 9;
int in2 = 8;
//Initialize "lightSensor" as the value from pin A0 and read in the value. This is the photoresistor.
int lightSensor = analogRead(A0);
//The lightVal will hold the value of lightsensor in this variable
int lightVal = 0;
//These are the pins for the reed switches
// reed1Pin is the lower switch on the door. This is digital pin 2
int reed1Pin = 2;
//reed2Pin is the top switch on the door. This is digital pin 4
int reed2Pin = 4;
//These are the variables that hold the state of the reed switches
int switchState1 = 0;
int switchState2 = 0;
//This only runs once.
void setup()
{
// set the motor control pins as outputs. This means pins 8, 9, 10 are outputs to the l298n motor controller.
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
//read the state of switch 1 (the bottom one) and place it in switchState1
switchState1 = digitalRead(reed1Pin);
//read the state of switch 2 (the top one) and place it in switchState2
switchState2 = digitalRead(reed2Pin);
//this is important to make sure that the door starts up when the program gains power.
//if switchState2 is not high then go to the while statement
if (switchState2 != HIGH)
{
// this function will run the motor down as long as switch 1 has not been triggered
while (switchState2 != HIGH)
{
// turn on motor and set speed to 255
analogWrite(enA, 255);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
//read the state of the switch again to make sure that it has not been triggered
switchState1 = digitalRead(reed1Pin);
//read the state of switch 2 (the top one) and place it in switchState2
switchState2 = digitalRead(reed2Pin);
}
// once switchstate2 has been triggered turn off the motor
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}
}
//this runs over and over
void loop()
{
//read the light sensor and place it in lightval
lightVal = analogRead(lightSensor);
//read the state of switch 1 (the bottom one) and place it in switchState1
switchState1 = digitalRead(reed1Pin);
//read the state of switch 2 (the top one) and place it in switchState2
switchState2 = digitalRead(reed2Pin);
//the lightSensor is read and placed into lightVal. if its less than 200 and switchState2 is
//equal to high then go to the motor down code. But if the light is greater than 200 and the switchstate1
//is equal to high then call motor up code
if (switchState2 = HIGH && lightVal < 200)
{
delay(2000);
motordown();
}
else if (switchState1 = HIGH && lightVal > 200)
{
delay(2000);
motorup();
}
}
void motordown()
{
//Read the state of switch 1 (the Bottom one) and place it in switchState1
switchState1 = digitalRead(reed1Pin);
//read the state of switch 2 (the top one) and place it in switchState2
switchState2 = digitalRead(reed2Pin);
//If switchState2 is high and the light is dark then continue
if (switchState2 = HIGH && lightVal < 200)
//wait 2 seconds
delay(2000);
//read the state of switch 1 (the bottom one) and place it in switchState1
switchState1 = digitalRead(reed1Pin);
//read the state of switch 2 (the top one) and place it in switchState2
switchState2 = digitalRead(reed2Pin);
while (switchState1 != HIGH) {
// turn on motor and set speed to 255
analogWrite(enA, 255);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
//read the state of switch 2 (the top one) and place it in switchState2
switchState1 = digitalRead(reed1Pin);
//read the state of switch 2 (the top one) and place it in switchState2
switchState2 = digitalRead(reed2Pin);
}
//wait 1 second before turning off the motor to let the locks engage at the bottom
delay(1000);
// now turn off motor
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}
void motorup()
{
//read the state of switch 1 (the bottom one) and place it in switchState2
switchState1 = digitalRead(reed1Pin);
//read the state of switch 2 (the top one) and place it in switchState2
switchState2 = digitalRead(reed2Pin);
//if switchState1 is high and the light is bright then continue
if (switchState1 = HIGH && lightVal > 200)
{
//read the state of switch 1 (the bottom one) and place it in switchState1
switchState1 = digitalRead(reed1Pin);
//read the state of switch 2 (the top one) and place it in switchState2
switchState2 = digitalRead(reed2Pin);
//delay 2 seconds
delay(2000);
//while switchState2 is not high turn on the motor up
while (switchState2 != HIGH)
{
// this function will run the motor as long as switch 2 has not been triggered
// turn on motor and set speed to 255
analogWrite(enA, 255);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
//read the state of switch 1 (the bottom one) and place it in switchState2
switchState1 = digitalRead(reed1Pin);
//read the state of switch 2 (the top one) and place it in switchState2
switchState2 = digitalRead(reed2Pin);
}
// now turn off motor
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}
}