Chicken coop 14 days later

Yes I know I should have used a state machine design, but it appears to work so far, and I think I did good for only having the thing for 14 days

created Feb 2010
 by Jake Dixon
 all parts from digikey/my parts drawer
 //Parts list
 P#               Q         Name            
 67-1106-nd       10      test led
 p20kbact-nd      1       20K ohm
 cm140kfct-nd     1      140K ohm (not used)
                          5v relay DPDT (used for door actuator)
 374-1051-nd      3       5v/120v relay spst   
 317-1305-nd      1       thermal resistor5-100k ohm
 p959-nd          10      330uf caps (in parallel with relays)

// set pin numbers:
int PhotoPin = 1;        // photo analog pin
int TempPin = 0;         // temp analog sensor
int FeedPin = 12;         // dispenses feed
int FanPin = 7;          // exhaust fan ( cheap wal-mart plug in fan)
int HeatPin = 2;         // heat lamp ( 100W light bulb)
int LightPin = 4;        // night light (40W Lightbulb)
int ButtonPin = 8;      // pushbutton
int DoorPin = 13;        //door actuator realay

// variables will change:
int ButtonState = 0;              // variable for reading the push button status
int DoorState = 0;                //variable for reading the door state
int Bright = 0;                   // variable for photo pin
int Temp = 0;                     // variable for temp cold
long Flip = 199900;                    // Delays routines and allows manual operation
int Flop = LOW;                    // Not sure yet but I may need it

void setup() {

  pinMode(FeedPin, OUTPUT);
  pinMode(FanPin, OUTPUT);
  pinMode(HeatPin, OUTPUT);
  pinMode(LightPin, OUTPUT);
  pinMode(DoorPin, OUTPUT);
  pinMode(PhotoPin, INPUT);
  pinMode(TempPin, INPUT);
  pinMode(ButtonPin, INPUT);

  digitalWrite(DoorPin, LOW);



void loop()
  Flip ++;
  // ******** Define int ********           //aka "what's up"



  // ****** Manual Operation  ******
  if (ButtonState == HIGH)                  // the button is pushed
    if(DoorState == LOW)                    // the door is closed
      Serial.println("******Door Opening******");
      digitalWrite(DoorPin, HIGH);          // open the door
      delay (5000);                         // how long it takes to open
    if (DoorState == HIGH)                  // the door is open
      Serial.println("******Door Closing******");
      digitalWrite(DoorPin, LOW);           // close the door
      delay (5000);                         // how long it takes to close

  // ****** Morning Routine  ******
  if (Flip > 2000000){                       //disables a change from night to day and vice verse for about 2-3 hours
    if(Bright < 672){                        //its dark out side 
     if (Flop==LOW){                         //its not already in "morning"
        digitalWrite(DoorPin, HIGH);         //open the door
        Serial.println("***********GOOD MORNING***********");
        Flip = 0;
        Flop= HIGH;                          //enable night mode

  // ********Evening Routine********
  if (Flip >2000000) {
    if (Bright > 800){                       // if it's dark out and night is enabled
      if(Flop == HIGH){
        Serial.println("******Good Evening******");
        digitalWrite(DoorPin, HIGH);         // open the door
        Serial.println("step 1 go");
        digitalWrite(LightPin, HIGH);        // turn on the light
        Serial.println("step 2 go");
        delay (5000);                        // wait 5 seconds
        Serial.println("step 3 go");
        digitalWrite(FeedPin, HIGH);         // turn on the dinner bell/feeder on
        Serial.println("step 4 go");
        delay (20000);                       // wait 20 seconds
        Serial.println("step 5 go");
        digitalWrite(FeedPin, LOW);          // total feed/buzzer time 20 seconds
        Serial.println("step 6 go");
        delay (300000);                      // wait 5 min for all the chickens to get home
        Serial.println("step 7 go");
        digitalWrite (DoorPin, LOW);         // close the door
        Serial.println("step 8 go");
        delay (900000);                      // wait for everything to get settled
        Serial.println("step 9 go");
        digitalWrite(LightPin, LOW);         // sleep tight
        Serial.println("******good night******");
        Flip = 0;                            //reset timer
        Flop=LOW;                            //disable evening, enable morning
  if (Temp < 380)
    digitalWrite(FanPin, HIGH);
    Serial.println("******Fan on******");  
  if (Temp > 390)
    digitalWrite(FanPin, LOW);
  if (Temp > 800){
    Serial.println("******heater on******");
    digitalWrite(HeatPin, HIGH);
  if (Temp < 780)

    digitalWrite(HeatPin, LOW);

how did I test the operation you ask?
I put leds on the relays, so when it energizes it lights the led.
I also used a desk lamp for heat a cooler to simulate the frost, and the photo sensor is in the window for light/dark.
so far it works, well see how it does when I finish modifying the shed for chickens! :slight_smile:

If you're using relays to switch mains voltage some diodes to short out the back EMF from the relays wouldn't go amiss. Your arduino will thank you for it :)

I put some large caps in parallel with them (300uf), do I really need a diode also?

large capacitors could be bad because the arduino will have to charge them up, which could use a lot more than 40ma that its supposed to have loading a pin.

some relays have a diode built in as well.

After my new understanding of "blink without delay" I think I may have found a way to solve my biggest flaw: if a chicken doesn't make it in time and I have to go out and manually open the door, I could potentially be stuck outside in the cold for up to 20 minuets until evening routine completes.

I'm going to try this when I get out of work.... any one see any mistakes or reasons it won't work?

  if (Flip >2000000) {                         // if it's been more than about 2 hours
    if (Bright > 800){                         // if it's dark out and night is enabled
      if(Flop == HIGH){                        //if day already ran
        NT++;                                  //start night timer
        Serial.println("******Good Evening******");
        if(NE = LOW){                          // night toggle low
          digitalWrite(DoorPin, HIGH);         // open the door
          digitalWrite(LightPin, HIGH);        // turn on the light
          digitalWrite(FeedPin, HIGH);         // turn on the dinner bell/feeder
          NE=HIGH;                             // set toggle high
          Serial.println("step 1 go");
        if (NT >550){                          // wait about 20 seconds
          digitalWrite(FeedPin, LOW);          
          Serial.println("step 2 go");
        if (NT >30000){                        // wait 5 min for all the chickens to get home
          digitalWrite (DoorPin, LOW);         // close the door
          Serial.println("step 3 go");
        if (NT >33000){                        // wait for everything to get settled
          Serial.println("Last step go!!");
          digitalWrite(LightPin, LOW);         // sleep tight
          Flip = 0;                            // reset timer
          Flop=LOW;                            // disable evening, enable morning
          Serial.println("******good night******");