06 October 2012

Knight rider

We're building a new chicken coop for Lemontree's hens, and we don't like getting up at the crack of dawn to go let them out of the coop every morning (if they're not locked up, literally with a padlock, racoons eat them). So, I'm making an automatic door.

If you're read this blog you may recall I've made other electronic devices like DRL/stop/turnsignal integrators for motorcycles and such. However this time we wanted more sophistication. Think about it -- a timer based solution will only open the door at the right time twice a year, because sunrise and sunset times (what chickens work off of) change every day. A light sensor could work, except our chickens like to wake up about 1/2 hour before dawn and go to bed about an hour after sunset.  Besides, a light sensor could be fooled by a cloudy day, full moon, or neighbor's motion lamp.

So how to we make a door open half an hour before sunrise, and close an hour after sunset no matter what? Well there's a mathematical equation to figure the sunrise/sunset times exactly, so we just have to calculate it every day. That requires... a computer.

Putting a computer in a chicken coop is silly (well, I guess the hens could Tweet using Twitter when they want out, but they have a hard time using a mouse without opposable thumbs). No, this is why microcontrollers were invented.

A microcontroller is about as powerful as state-of-the-art PCs in the early 80s. The one I selected, the Atmel Atmega328p, can run up to 20MHz. It's only got 2K (as in, 2,048 BYTES) of RAM, though, and 32K of program storage. The good news is, a chicken coop door program takes maybe 11K of program storage and maybe a few dozen bytes of RAM, so it's perfect. In fact I'll even be running it at a slower 8MHz to save power, which still takes only a fraction of a second to calculate sunrise/sunset times.

The kind folks at Arduino have written some nice developer software for the Atmega chips, so I don't have to learn a bunch of arcane incantations. In fact, I wrote a whole program last night. This isn't the chicken coop door program, it's just something I was playing around with. It uses a "shift register" chip -- it has 8 outputs, so you send it a series of bits (1s and 0s) and it sends them to it's 8 output pins in sequence.

Here's the program:

#include "SPI.h"

int dtime = 60;
byte direction = 0;

void setup() {               
  SPI.begin();
}

void loop() {

  SPI.transfer(B10000000);
  delay(dtime);
  SPI.transfer(B01000000);
  delay(dtime);
  SPI.transfer(B00100000);
  delay(dtime);
  SPI.transfer(B00010000);
  delay(dtime);
  SPI.transfer(B00001000);
  delay(dtime);
  SPI.transfer(B00000100);
  delay(dtime);
  SPI.transfer(B00000010);
  delay(dtime);
  SPI.transfer(B00000001);
  delay(dtime);
  SPI.transfer(B00000010);
  delay(dtime);
  SPI.transfer(B00000100);
  delay(dtime);
  SPI.transfer(B00001000);
  delay(dtime);
  SPI.transfer(B00010000);
  delay(dtime);
  SPI.transfer(B00100000);
  delay(dtime);
  SPI.transfer(B01000000);
  delay(dtime);
  if (direction == 0) {
    dtime = dtime -10;
    if (dtime < 1) {
      direction = 1;
    }
  }
  else {
    dtime = dtime + 10;
      if (dtime > 59) {
      direction = 0;
    }
  }
}


If you look at the position of the "1"s in the above SPI.Transfers, you can guess that I've just made a Knight Rider (or Battlestar Galactica Cylon) back-and-forth animation. If that's what you guessed, you're right! I also decided to make it speed up and slow down, just for fun.



(Those of you reading by email may have to go to the website to view it.)

We have serious Project Overload on the new house right now, we have a plumber coming Monday, the roofers coming in a week or two, gutters after that, furnace end of October (yes it's already cold and we're freezing), Windows mid-November, and we're in the process of painting right now. We also put up a bunch of new siding yesterday, and I got to use my new pneumatic palm nailer (those are a blast). New chicken coop and automatic door have to fit into the schedule somewhere. So, more on the door later.

No comments: