Chicken house door controller (beta)



  • Hi guys. I have just finished a chickenhouse and am in the throes of automating the door. It will be controlled by domoticz and I plan to open 1/2 hour after sunrise and close 1/2 hour after sunset. The door will be driven by an electric antenna from the car wreckers, 12V battery and solar panel/controller.
    There are two reed switches to tell position of the door plus a button to manually open/close or reset a jam (for increased WAF).
    I have included in the sketch a bit to jiggle the door if it gets stuck, and turn off the actuator and send an alert to domoticz if the jam cannot be freed.
    I have yet to try it out as am waiting on the usual final component I only realised I would need once I started building (buck converter).
    Anyway code is below. mfalkvidd has kindly looked at it for me already. I believe it should work but am open to critiquing on any part of my coding including conventions.
    Thanks,
    Matt

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * 
     * 
     * DESCRIPTION
     * Chicken door controller
     */ 
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    #define MY_RADIO_NRF24
    
    #include <MySensors.h>
    #include <Bounce2.h>
    
    
    #define MOTORSW     3  // Motor activation pin
    #define MOTORPWR    4  // Motor power lead pin
    #define REED_OPEN   5  // Reed switch pin, pulled low when door is open
    #define REED_CLOSED 6  // Reed switch pin, pulled low when door is closed
    #define MANUALSW    7  // Momentary switch to control door
    bool DoorState;        // 0 closed, 1 open
    bool PrevState;        // Used to check for stated change
    byte jammed=0;
    unsigned long timer;   // Counter to check door closes in time ie isn't jammed.
    Bounce debouncer = Bounce(); 
    
    MyMessage msg(2,V_TRIPPED); 
    
    
    void setup() 
    {
        pinMode(MOTORSW,     OUTPUT);   
        pinMode(MOTORPWR,    OUTPUT);
        pinMode(REED_OPEN,   INPUT_PULLUP);   // Using internal pullup. When read switch is closed, will pull this pin to ground.
        pinMode(REED_CLOSED, INPUT_PULLUP);   // Ditto
        pinMode(MANUALSW, INPUT_PULLUP);      // Turn on pullup for switch, will be pulled low when pushed
        DoorState = loadState(1);
        PrevState = DoorState;
        debouncer.attach(MANUALSW);
        debouncer.interval(50);
    }
    
    void presentation()  
    {   
      
      sendSketchInfo("ChickenDoor", "1.0");  // Send the sketch version information to the gateway and Controller
      present(1, S_BINARY);  // Register door actuator to Gateway
      present(2, S_DOOR); // This will go on if door is jammed
      
    }
    
    
    void loop() {
      debouncer.update();
      int value = debouncer.read();
      if (value == LOW) {
        DoorState = !DoorState;
        jammed==0;   // Reset the jam
        send(msg.set(LOW));
      }
    
      if ((DoorState != PrevState) && (jammed < 2)) {       //Something has changed and door isn't jammed
        
        switch(DoorState) {
           case 0: // Close Door
            timer = millis();
            while( digitalRead(REED_CLOSED)==HIGH) {  //Drive actuator until door closes then stop
               digitalWrite(MOTORPWR, HIGH);   //Turn on the actuator
               wait(50);   // Actuator wakes up
               digitalWrite(MOTORSW, HIGH);  //Close the door
               if (millis() - timer > 5000) {   //door is jammed
                jammed++;
                jiggle(); //open and close again
                break;
               } 
               }
               digitalWrite(MOTORSW, LOW); //Door is closed, turn off actuator switch
               digitalWrite(MOTORPWR, LOW); //Door is closed, turn off actuator
               PrevState = DoorState;
               jammed=0;
            break;
           case 1: // Open Door
            timer = millis();
            while( digitalRead(REED_OPEN)==HIGH) {  //Drive actuator until door opens then stop
               digitalWrite(MOTORPWR, HIGH);   //Turn on the actuator
               wait(50);   // Actuator wakes up
               digitalWrite(MOTORSW, HIGH); //Wake up the actuator
               wait(100);
               digitalWrite(MOTORSW, LOW); //Close the door
               if (millis() - timer > 5000) {   //door is jammed
                jammed++;
                jiggle();
                break;
                    } 
               }
               digitalWrite(MOTORSW, LOW); //Door is closed, turn off actuator switch
               digitalWrite(MOTORPWR, LOW); //Door is closed, turn off actuator
               PrevState = DoorState;
               jammed=0;
              break;
           }      
        }
        else if (jammed >= 2) {
          send(msg.set(HIGH));           
        }
     }
    
    void receive(const MyMessage &message) {
      
      if (message.type==V_STATUS) {                     // We only expect one type of message from controller. But we better check anyway.
         DoorState = message.getBool();
         saveState(1, message.getBool());               // Store state in eeprom
         Serial.print("Incoming change for sensor:");   // Write some debug info
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       } 
    }
    
    void jiggle()  //J j j jiggle it a bit (4 times)
    { for(int x = 0; x < 4 ; x++) {
      digitalWrite(MOTORPWR, HIGH);
      digitalWrite(MOTORSW, HIGH);
      wait(500);
      digitalWrite(MOTORSW, LOW);
    }
     digitalWrite(MOTORPWR, LOW);
     digitalWrite(MOTORSW, LOW);
      }
    

 

307
Online

7.8k
Users

8.7k
Topics

92.9k
Posts