Following Chris's very useful Arduino class, I decided to do a small practice exercise, using the stepper motor I got from him. When I'm learning a new language, it always works better for me to try doing some real problem, not just some dumb textbook example.
So I decided to see if I could make an automatic chicken-door opener. We have backyard chickens, and they have to be locked in every evening and let out every morning, to prevent predators from doing midnight chicken-snacking. Commercial units run like $180-$400.
I had purchased a Danger Shield, which piggy-backs on the Arduino board and gives me a photocell and some linear potentiometers, so along with the servo motor I thought I had all the parts needed. And sure enough it didn't take long to get something working.
The stepper motor turns a disk of light cardboard (for now) with a wedge cut out of it. The sketch monitors the photocell and rotates the disk 180 degrees when the light level crosses a threshold. A second draft added the ability to read one pot to set the threshold level (don't want to close the door before the chickens have gone to roost) and to read another pot to control the speed of rotation (don't want to create a chicken guillotine).
Of course now I have the more interesting and (for a software guy) challenging part: scaling it up to real-world size and materials. The door has to be strong enough to keep raccoons out and robust enough to not jam when chicken feed and chicken poop and rain-splashed dirt build up on it. And a 3-foot diameter disk would be unwieldy in an average-sized coop. But as an exercise in coding and debugging the Arduino it was very useful, and a lot of fun...
Larry Walker
Code:
// Chicken-House Door automator
// by Larry Walker 11/12/10
// uses a servo motor to open/close a chicken door
// responds to photocell to open close at dawn/dusk
// reads potentiometer 1 to control speed of door open/close
// (to avoid creating a chicken guillotine!)
// reads potentiometer 2 to control trigger light-level
// (to avoid closing door too early (or during thunderstorms?))
#include <Servo.h>
#define LIGHT 3
#define SLIDER1 0
#define SLIDER2 1
const int SHUT = 0;
const int OPEN = 1;
Servo myservo; // create servo object to control a servo
int pos = 180; // variable to store the servo position
int light_val; // variable to store light level
int step_delay; // variable to store stepper delay value
int trigger_level; // variable to store trigger light-level
int desired_pos = SHUT; // desired door position
int curr_pos = SHUT; // current door position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
}
void loop()
{
// get the current light level
light_val = analogRead(LIGHT);
Serial.print("Light: ");
Serial.println(light_val);
// get the delay value
step_delay = map(analogRead(SLIDER1), 0, 1023, 1, 30);
Serial.print("Delay: ");
Serial.println(step_delay);
// get the trigger-level value
trigger_level = analogRead(SLIDER2);
Serial.print("Trigger: ");
Serial.println(trigger_level);
Serial.println();
// check the light level: light=open-door; dark=close-door
if (light_val < trigger_level) {
desired_pos = SHUT;
}
else
{
desired_pos = OPEN;
}
if (curr_pos != desired_pos) {
if (desired_pos == SHUT) {
// open the door by spinning it to 180
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(step_delay); // waits for the servo to reach the position
// allow for dynamic speed changes
step_delay = map(analogRead(SLIDER1), 0, 1023, 1, 30);
}
}
else
{
// close door by spinning it to 0
for(pos = 180; pos >= 1; pos -= 1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(step_delay); // waits for the servo to reach the position
// allow for dynamic speed changes
step_delay = map(analogRead(SLIDER1), 0, 1023, 1, 30);
}
}
curr_pos = desired_pos;
}
}
Boring still photo attached...
Less-boring movie:
http://walkerphotographix.com/vidclips/chicken_door.mov