Chicken Coop Door Confusion

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.

~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!


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 has a screengrab of my schematic

V6_Automated_Chicken_Coop.ino (1.74 KB)

V7_Automated_Chicken_Coop.ino (1.79 KB)

Please post your code here to make it easier to copy/paste to an editor and use code tags when you post it.

Auto Format your code and you should see the cause of the error, Why is the switch/case not inside a function ?

Also, draw up a real schematic.

.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

The diagram you have supplied does not label any of the components or pin connections.
What are the relays you are using?

To indicate if the door is open or closed you will need what are called limit switches, as a school project, they can be simple microswitches as below.

Tom... :slight_smile: