Laziness or Chicken Coop Automation Part 2: Door Control Algorithm

Don’t understand the title? Read “It’s Hard Work Being Lazy or Chicken Coop Automation Part 1: Door Hardware” to elucidate the title and offer some background.

How hard can it be to control a chicken coop door with a microcontroller and DC motor? Just send a voltage to the door’s motive source to open it and reverse the voltage to close it. Right? You’d think so before delving in, but things quickly get more complex.

To explain, let’s use an example to show how the complexity grows :

  • You have a DC motor you want to use for control of a chicken coop door. you also have a microcontroller with plenty of output pins, each of which can be programmed to 5 volts or ground. Great. Wire each of the two wires from the motor to an output pin. Program one pint to be +5 V, the other to be ground, and the motor rotates. Reverse the +5 V and the ground, and the motor reverses. Now put a load on the motor. The motor quits rotating and and the microcontroller releases its smoke. It couldn’t supply enough current.
  • Put the smoke back into the microcontroller and get a proper power supply. Hook it up to the motor, put a load on the motor, power it up, and the motor rotates. Unfortunately, you are only able to rotate the motor one way. You need a way to reverse the motor polarity.
  • Now get an H-Bridge or series of relays to revere the polarity to the motor. Wire it up, program the microcontroller to control the H-Bridge or relays, and you have a reversing DC motor.
  • You hook everything up to automate the chicken coop, but now the microcontroller needs to know when the door reaches the end of its travel. You need sensors. Limit switches? Eddy current sensors? Reed switches and magnets?
  • Limit switches installed. Now, how does the microcontroller know when to open or close the door? Light Sensor? Timer? If using a timer, what about the changes in sunrise and sunset?
  • Do you need any manual external controls to give commands to the microcontroller? Like buttons?

Once you get all of that sorted, you also need to think about the algorithm the microcontroller will use to make its decisions. Most of the things listed above relate to specific motive sources and configurations, so I will not go into details. Each of my coops are set up totally differently, and what applies to one does not apply to the other. The control algorithm is common between the two, and I will elaborate on that.

Algorithm Considerations:

I wanted a couple features integrated into the control methodology:

  1. External inputs to adjust opening modes (do not open in the morning, open now, close now, etc)
  2. Decisions can be made mid movement (you can command the door to close while it is opening or vice versa)
  3. Controlled by a timer
  4. Account for changes of season
  5. Account for daylight savings

The first feature was to override the automatic controls. For instance: the yard crew is coming the next day and I want the chickens locked in their desolate little cells.

The second feature was mainly needed due to the speed of the actuator. One of the doors moves very v-e-r-y   s–l–o–w–l–y, and I didn’t want to wait for the door to move all the way before making another command. It would be like a windows update… me standing there helplessly slack jawed and gawking at something with no apparent progress. I would like to mention that even moving at a snails pace, the coop managed to capture the pictured cat. I came home one night heard one of the chickens meowing, and found the cat in there.

As far is the third feature goes, I thought about using a light sensor versus a timer. I opted for the timer because I didn’t know how well a light sensor would handle inclement weather and eclipses… I didn’t want a hen squished in the door when she was trying to rush out with her pinhole projector.

I wanted the fourth feature to account for the changes in sunrise and sunset between seasons.

I intentionally did not include the fifth feature because daylight savings is an annoyance, the microcontroller doesn’t know what it is, it stresses out the chickens, and I don’t think it should exist. In fact, it didn’t enter into consideration. I made reference to daylight savings to tell a quick story. I once knew of a man convinced daylight savings causes it to cool in the winter because it takes an hour of afternoon sun away. “That hour of sunlight is just added to the morning,” one might protest. He had already thought about that. “The hour is taken from the hot part of the day and added to the cool part of the day.” Sounds legit.

Control Algorithm: State Machine

The initial consideration for controlling the given example might be to simply apply voltage to the DC motor and wait for the door to trigger the limit switch at the end of its travel. However, what if a particularly stupid chicken is in the way and the door cannot finish its travel? Do you keep applying power and burn up the motor? What if the motor overheats, catches fire, and the other chickens attempt to save the burning, squished chicken? Do you want to risk losing the whole flock? I think not.

Taking all of the mentioned features into account, the algorithm I used was that of a state machine.

This state machine essentially:

  • Reads all inputs (user input, current time vs sunrise/sunset, reached limit of travel, timed out)
  • Interprets inputs to determine the desired state
  • Looks at the current state(door position or door movement)
  • Commands new state (or leaves it alone)
  • Repeats

Rather than telling the motor to do something and waiting for it to happen, the microcontroller continually polls its inputs, makes a decision, makes a command based on the current state, and goes back to polling its inputs. It does not make a command and lock everything up while it waits for verification. It goes back to work looking for inputs and making decisions. Practically, the processor can take new input,  process new information, make commands, and squish a chicken at the same time.

Calculating Sunrise and Sunset:

One of the “inputs” mentioned above is not what one might think of as an input. The processor calculates sunrise and sunset times on a daily basis and uses them to decide when to open and close the chicken squisher. The internet machine told me that sunrise and sunset times roughly follow a sine wave. Therefore, the algorithm estimates sunrise and sunset using a sine wave with periods, amplitudes, and offsets to match the peaks at the summer and winter solstices. I am including the snippet code so nobody else has to look for it. I hope that my comments are clear to others.

If you use this, you will have to match the times to your particular latitude, longitude, and time zone. If it is not clear, feel free to comment and I can provide more info.

It’s Hard Work Being Lazy or Chicken Coop Automation Part 1: Door Hardware

That title might be a little deceptive. Let me elaborate. I will work hard on something now anticipating it will allow for laziness in the future. “Laziness in the future” allows time to work hard on something else in the future, so I can be lazy about that in the more distant future… which allows me to be even more lazy about even more things in the even more distant future. I guess I am just doing my best to maximize my laziness.

All of that leads into an update on my first post. It was about a chicken coop and my reluctance to got out and open it every morning and close it every night. It has been in operation for years now, I’ve automated another, and I want to provide an update. Maybe it will help someone else to maximize their laziness.

 

BACKGROUND

My original coop is a semi-portable “chicken tractor” designed to be moved about the yard on wheels. In theory, I could periodically move the tractor and fertilize different sections of the yard. In practice, it obliterated all plant life anywhere near its presence. While it failed as a fertilizer, it succeeded that much more in my maximization of laziness! I no longer need to move it about the yard.

I bought the chicken tractor on Craigslist, so the coop door (visible here and here) was a 12″ X 12″ piece of plastic that slides horizontally to open and close.

My second coop was built around laziness with a door that opens and closes by sliding vertically. It also has features allowing it to be cleaned in a few minutes, supplying weeks of food and water, and can be shut in if it is cold. However, my FAVORITE feature is the slick ramp out of the door. If I “accidentally” increase the slope by removing the bricks at the bottom, the chickens can’t get enough traction and fall right down it.

Door Automation Hardware:

I am not going to provide details on the watering and feeding system in this post. I decided not to actively control those because mechanical systems were too simple and reliable. I am also not going to go into details due to space, boredom of the reader, and my laziness. Feel free to comment with questions and I will elaborate.

The chicken tractor door was not designed with electric controls in mind, so it required some jackassery. Its motive force is supplied by an electric screwdriver. I removed the guts and wired it directly to a relay that is controlled via an Arduino microcontroller. It has an angle attachment from a drill that rotates the drive axis by 90 degrees. It also has micro switches to detect when the movement of the door has reached its limits.

The chicken coop door uses a universal power antenna mechanism for the automotive industry but controlled by an Arduino microcontroller. Using an antenna was not my idea, there are multiple examples on the internet so I don’t know who to give credit.

The power antenna took some adaptation due to the door’s weight. When winter brought colder temperatures, an internal clutch mechanism would slip rather than lifting the door. I disassembled the antenna and found I could modify the clutch mechanism to increase its torque output. Things were great until summer brought warmth and the modified clutch transferred too much torque, stripping the internal gears.

Rather than continually adjusting the clutch mechanism based on the weather forecast (minimizing laziness), I opted to make a counterweight (maximizing laziness). It consisted of a couple clothesline pulleys, an Oxford comma, some string, and couple pieces of 2×4. Notice their odd shape. I cut off pieces off to balance their weight with the door (with the drive mechanism unattached).

After putting all of the work into these coops, I began to wonder if it was worth it. Do the chickens even need a door that closes at night? If they’ve been around for thousand and thousands of years, shouldn’t they be able to fend for themselves? The answer is no, they are unable to fend for themselves. One night there was a glitch and the door didn’t close. When I went out to check on them, I found a headless chicken laying on the floor. I have since remedied the glitch, and the offender has been dispatched to a better (maybe worse) place.

This has gotten longer than intended, so I have provided some information on the control algorithm in this link.

Overall, both of the coop’s door mechanisms have afforded me the opportunity to be quite lazy indeed! So lazy that I can’t even bring myself to finish this senten