For a school project I need to create something that roughly relates to my career choice. I decided to create an automatic chicken coop door that would use a photo resistor to trigger the opening/closing of the chicken coop door. My circuit has a series of relays controlling the polarity and power to the motor that will open/close the coops door. The issue I'm running into is with the switches that read whether or not the door is open/closed. I can't figure out how to tell the Arduino that the door is closed but it's OK to reopen door. What I mean is I only figure out how to tell the Arduino to turn off the motor when the switch closes. I can't figure out how to tell the Arduino it can reopen when it's light again while the switch is closed.
So my question is how do I code the Arduino so it does that?
~I looked into swithCase and it looks like it might solve the problem, but I don't know how to properly execute the code.~
Thanks any help is appreciated!
(I fixed the way I put the code into the question and created a schematic)
V6 has the code with the switch problem
int photocellPin = 0;
int upswitchReading;
int downswitchReading;
int photocellReading;
int powerDown = 11;
int powerUp = 9;
void setup(void) {
pinMode(powerDown, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(powerDown,OUTPUT);
Serial.begin(9600);
}
void loop(void) {
digitalWrite(powerDown, HIGH);
digitalWrite(powerUp, HIGH);
Serial.print("Up Stop reading = ");
upswitchReading = analogRead(2);
Serial.print(upswitchReading);
Serial.print("Down Stop reading = ");
downswitchReading = analogRead(4);
Serial.print(downswitchReading);
Serial.print(" Photocell reading = ");
photocellReading = analogRead(photocellPin);
Serial.print(photocellReading);
if (photocellReading < 10) {
Serial.println(" - Dark");
} else if (photocellReading < 200) {
Serial.println(" - Dim");
} else if (photocellReading < 500) {
Serial.println(" - Light");
} else if (photocellReading < 800) {
Serial.println(" - Bright");
} else {
Serial.println(" - Very bright");
}
if (photocellReading < 200)
{
digitalWrite (6,HIGH); //On Motor
digitalWrite (5,LOW); // Polarity of Motor (if pin 5 is LOW motor polarity is Positive)
}
else
{
digitalWrite (6,HIGH); //On Motor
digitalWrite (5,HIGH); // Polarity of Motor (if pin 5 if HIGH motor polarity is Negative)
}
if (downswitchReading > 1015)
digitalWrite (6,LOW); //Off Detection for Motor (Door Going Down)
if (upswitchReading > 1015)
digitalWrite (6,LOW); //Off Detection for Motor (Door Going Up)
delay(1000);
}
V7 has what I think a switchCase would look like. ( I get this error though = expected unqualified-id before 'switch')
int photocellPin = 0;
int upswitchReading;
int downswitchReading;
int photocellReading;
int powerDown = 11;
int powerUp = 9;
void setup(void) {
pinMode(powerDown, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(powerDown,OUTPUT);
Serial.begin(9600);
}
void loop(void) {
digitalWrite(powerDown, HIGH);
digitalWrite(powerUp, HIGH);
int photocellReading = analogRead(photocellPin / 3);
int readingReal = map(photocellReading,54,974,0,100);
Serial.println("Up Stop reading = ");
upswitchReading = analogRead(2);
Serial.println(upswitchReading);
Serial.println("Down Stop reading = ");
downswitchReading = analogRead(4);
Serial.println(downswitchReading);
Serial.println(" Photocell reading = ");
Serial.println(readingReal);
}
switch (readingReal)
{
case 0:
digitalWrite (6,LOW); //On Motor
digitalWrite (5,LOW); // Polarity of Motor (if pin 5 is LOW motor polarity is Positive)
break;
case 1:
digitalWrite (6,HIGH); //On Motor
digitalWrite (5,LOW); // Polarity of Motor (if pin 5 is LOW motor polarity is Positive)
if (upswitchReading > 1015)
digitalWrite (6,LOW); //Off Detection for Motor (Door Going Up)
break;
case 2:
digitalWrite (6,LOW); //On Motor
digitalWrite (5,LOW); // Polarity of Motor (if pin 5 is LOW motor polarity is Positive)
break;
case 3:
digitalWrite (6,HIGH); //On Motor
digitalWrite (5,LOW); // Polarity of Motor (if pin 5 is LOW motor polarity is Positive)
if (upswitchReading > 1015)
digitalWrite (6,LOW); //Off Detection for Motor (Door Going Up)
break;
default:
digitalWrite (6,LOW); //On Motor
digitalWrite (5,LOW); // Polarity of Motor (if pin 5 is LOW motor polarity is Positive)
delay(1000);
}
The jpg is a photo of my schematic There's supposed to be a ground connected to the polarity switching relay. I'm also going to add some transistors to control the relays. D11 is being used as a power source, I was told to instead run the switches from the input pin by using the command INPUT_PULLUP.
There is a second schematic that was created by a forum member that may be easier understand. (2017-10 ect.)