Auto Chicken Door

Hello all.

I am very new to the whole Arduino thing...and very new may be an under statement. I purchased an Arduino Uno and an Arduino motor shield. I also picked up a used window regulator motor out of a car,(Runs on 12 volts.) After I got the motor to spin both ways I tried to add a photocell. The idea is I want the motor to spin one direction half hour after dark, and then the other direction half hour after sunrise. The problem is I can't for the life of me figure out how to do it. I've watched a number of tutorials, read some posts, tried some random ideas, and for whatever reason I can not do it. If someone could set me up with a code for this, and possibly an explanation on how/why it works it would save me from pulling the rest of my hair out. I've also seen people post their codes, but I figured I would save myself the embarrassment and not post what I have...or think I have.

If you’re looking for someone to write the code for you, then you should be making a post in the Gigs and Collabaration section.

Otherwise, What have you tried?

Go ahead and post that code! I'm just a month into Arduino and my first question on the forum was resolved after I posted my code. Posting your code is probably the best thing you can do as others can then help you to figure out what is right, what isn't right, and maybe where you need to go to make it all work. :)

Reading material.

http://www.google.com/search?hl=en&as_q=coop&as_epq=&as_oq=&as_eq=&as_nlo=&as_nhi=&lr=&cr=&as_qdr=all&as_sitesearch=http%3A%2F%2Farduino.cc%2Fforum%2Findex.&as_occt=any&safe=images&tbs=&as_filetype=&as_rights=

For timing stuff, download & take a look at the Time, TimeAlarms and DS1307RTC libraries. Several examples are found in these libraries. http://playground.arduino.cc/Code/time

int photocellPin = A5; // select the input pin for the photocell
int sensorValue = 0; // variable to store the value coming from the photocell
int motorPin = 12;  //

void setup() {
  Serial.begin(9600); //set baud rate to 9600 on the Arduino
  pinMode(photocellPin,INPUT);
  //Setup Channel A
  pinMode(motorPin, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin
  
}

void loop(){
  sensorValue = analogRead(photocellPin);
  Serial.println(sensorValue);  //print the value to serial monitor
  
  if (sensorValue < 300) //if there is darkness then turn the motor on
  {
  //forward @ full speed
  digitalWrite(motorPin, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins the motor on Channel A at full speed
  
  delay (3000);
  
  digitalWrite(9, HIGH); //Eengage the Brake for Channel A

This is where I get lost. Whatever I put here seems to have no effect, or I’m doing something wrong.I can’t get it to spin the other way when there is light.

delay(1000);
  
  //backward @ full speed
  digitalWrite(motorPin, LOW); //Establishes backward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins the motor on Channel A at half speed
  
  delay(3000);
  
  digitalWrite(9, HIGH); //Eengage the Brake for Channel A
  
  delay(1000);
  
}

}

Moderator edit: CODE TAGS. Did you read the sticky thread at the top of this section? Why not?

What you need to do is to constantly check the light level and look for the transition from light to dark, and dark to light. To do this, you would define analog values for the transitions:

const int darkLevelLimit = 300;

You would then check if a transition has occurred:

int currentLightLevel = digitalRead(ldrSensorPin);
if ( (currentLightLevel < darkLevelLimit) && (lastLightLevel >= darkLevelLimit) )
{
  // We've gone from light to dark
}

You then have to update the previous light level variable (which should be declared as global or static):

lastLightLevel = currentLightLevel;

You might want to add a sensor or two to the door itself to ensure it is clear before closing.

You might want to add a sensor or two to the door itself to ensure it is clear before closing.

Nah. Smack the chickens if they won't get out of the way.

Be practical, it might burn out the motor trying to close a jammed door.

it might burn out the motor trying to close a jammed door.

Then, you need to add current sensors, to detect a stalled motor. And warning lights and sirens that the door is going to open and close. And e-mail and text messages that the door has been opened or closed. And, don't forget to twit that, too. Oh, and COSM probably needs to be able to show the light level and current consumption.

@PaulS
You must be really bored rite now to be trolling?

PaulS:

it might burn out the motor trying to close a jammed door.

Then, you need to add current sensors, to detect a stalled motor. And warning lights and sirens that the door is going to open and close. And e-mail and text messages that the door has been opened or closed. And, don't forget to twit that, too. Oh, and COSM probably needs to be able to show the light level and current consumption.

Only if you're billing the government on the job. Then add a small well paid private-sector staff to deal with all those messages and make telephone book thick reports, plus armed guards.

But for a farmer who doesn't have a barrel of pork to pay it all with, it'd be cheaper in chickens, motors and possibly damage to his own arm to set up an IR led and sensor or two.

But for a farmer who doesn't have a barrel of pork to pay it all with, it'd be cheaper in chickens, motors and possibly damage to his own arm to set up an IR led and sensor or two.

Oh, geez. Can't anybody have any fun here?

I dunno. I just did. ]:D

I dunno. I just did.

ROTFLMAO

@Arrch

I tried to input the code you gave me, and couldn’t get it to work. I’m sure it’s something I’m doing. I did get it to work with this code, switching directions with light and dark.

int photocellPin = A5; // select the input pin for the photocell
int sensorValue = 0; // variable to store the value coming from the photocell
int motorPin = 12; //
int limitSwitch = A4; //stop motor when reached

void setup() {
Serial.begin(9600); //set baud rate to 9600 on the Arduino
pinMode(photocellPin,INPUT);
//Setup Channel A
pinMode(motorPin, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin

}

void loop(){
sensorValue = analogRead(photocellPin);
Serial.println(sensorValue); //print the value to serial monitor

if (sensorValue < 300) //if there is darkness then turn the motor on
if (limitSwitch > 512) //
{
//forward @ full speed
digitalWrite(motorPin, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed

digitalWrite(9, HIGH); //Eengage the Brake for Channel A
}
{
if (sensorValue >300) //if there is light then turn the motor on
digitalWrite(motorPin, LOW); //Establishes Reverse direction for channel A
digitalWrite(9, LOW); //Disengage the brake for channel A
analogWrite(3,255); //Spins the motor on channel A at full speed

delay (1000);

digitalWrite(9, HIGH);

}
}

Now my problem is I can’t get the motor to stop…at all. I only want it to run for 10 seconds. Also want 30 minutes of light or dark before the motor turns on. But that seems like a long ways away from where I am now. Any help would be appreciated. Thanks!!

GoForSmoke: Be practical, it might burn out the motor trying to close a jammed door.

Current sensor. Or chicken sensor. :P

Where do you read the limit switch?

If it's a digital read then it will only ever be 0 or 1, 512 is a bit high and I have yet to see an analog limit switch.

Once you understand the Blink Without Delay sketch in you IDE-included examples (under 2. Digital) you should be able to make things happen over time in your sketch.

berndtc426:
I tried to input the code you gave me, and couldn’t get it to work. I’m sure it’s something I’m doing. I did get it to work with this code, switching directions with light and dark.

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the # button above the posting area.

I can’t make much sense of your intentions here. How about a bit of indenting (after putting in the code tags)?

For example:

  if (sensorValue < 300) //if there is darkness then turn the motor on
  if (limitSwitch > 512) //
  {
  //forward @ full speed

How much code is intended to be affected by " if (sensorValue < 300) "?