Chicken Door Opener in Hackspace Magazine

I’m pleased to say that the Arduino controlled chicken door opener has been featured in issue 3 of Hackspace Magazine, and some people have been asking for more information about the project. If you’re interested in making your own chicken door opener, you can take a look at my source code and download the 3D model files for the case from here. If you have more questions, you can ask on the @shedlandia twitter account for details.

Downloads

3D Models

Source Code

Source Code

#include <Servo.h>
#include <LowPower.h>

bool door_open = HIGH; // low when door is closed, high when open
bool door_locked = LOW; // set high when door is under manual control to override sensor
bool debugging = LOW; // enable serial debug

// Servo and relay
Servo myservo;
const int servo_power = 12; // relay switches on power to the motor controller
const int servo_pin = 11;
int pos;

// Sensor power
const int sensor_power = 13;

// Light Dependent Resistor
const int ldr = A0;
int ldr_state;
int pot_state = 1000;

// Counters to delay door opening/closing
byte delay_counter = 0; // until door closes
const byte delay_threshold = 5; // ticks until count triggers (each tick = 2 seconds)

// Buttons on side
const int open_btn = 9;
const int close_btn = 8;
int open_btn_state;
int close_btn_state;

void setup() {
myservo.attach(servo_pin);
pinMode(sensor_power, OUTPUT);
pinMode(servo_power, OUTPUT);
pinMode(open_btn, INPUT_PULLUP);
pinMode(close_btn, INPUT_PULLUP);
if (debugging==HIGH) {
Serial.begin(9600);
}
delay_counter = delay_threshold;
}

void loop() {
if (debugging == HIGH){
Serial.flush();
}
//sleep for 2 seconds
LowPower.powerDown(SLEEP_2S, ADC_OFF, BOD_OFF);

//check which button is being pressed
open_btn_state = digitalRead(open_btn);
close_btn_state = digitalRead(close_btn);

if (open_btn_state == LOW and close_btn_state == LOW){
digitalWrite(sensor_power, HIGH);
pot_state=analogRead(ldr);
delay(1000);
digitalWrite(sensor_power, LOW);
} else if (open_btn_state == LOW and close_btn_state == HIGH){
door_locked = LOW;
openDoor();
} else if (close_btn_state == LOW and open_btn_state == HIGH){
door_locked = HIGH;
closeDoor();
}

//power the ldr and take a reading
digitalWrite(sensor_power, HIGH);
ldr_state=analogRead(ldr);
digitalWrite(sensor_power, LOW);

if (debugging == HIGH){
//report the states to serial port
reportStates();
}

//check threshold brightness and add or subtract the counter
if (ldr_state <= pot_state){
if (door_open == HIGH){
delay_counter -=1;
}else{
delay_counter = delay_threshold;
}
} else {
if (door_open == LOW){
delay_counter +=1;
}else{
delay_counter = delay_threshold;
}

}

//open or close the door when the counter gets to one end
if (delay_counter <= 0){
delay_counter = delay_threshold;
closeDoor();
} else if (delay_counter >= (delay_threshold*2)){
delay_counter = delay_threshold;
openDoor();
}
}

void closeDoor(){
if (debugging==HIGH){
Serial.print("Door Closing\n");
}
if (door_open == LOW){
return;
}

digitalWrite(servo_power,HIGH);
delay(200);

for (pos = 120; pos > 1;pos-=1){
myservo.write(pos);
delay(15);
}
digitalWrite(servo_power,LOW);
door_open = LOW;
}

void openDoor(){
if (debugging==HIGH){
Serial.print("Door Opening\n");
}
if (door_open == HIGH or door_locked == HIGH){
return;
}
digitalWrite(servo_power,HIGH);
delay(200);

for (pos = 0; pos < 120;pos+=1){
myservo.write(pos);
delay(15);
}
digitalWrite(servo_power,LOW);
door_open = HIGH;
}

void reportStates(){
Serial.print("open Btn: ");
Serial.print(open_btn_state);
Serial.print(" close Btn: ");
Serial.print(close_btn_state);
Serial.print(" LDR: ");
Serial.print(ldr_state);
Serial.print(":");
Serial.print(pot_state);
Serial.print(" Delay: ");
Serial.print(delay_counter);
Serial.print(" Lock: ");
Serial.print(door_locked);
Serial.print("\n");
}

Advertisment

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.