newbie FIRST project- automatic chicken door
danharr
Posts: 10
I bought a bunch of stuff from Parallax but somehow never managed to find a use for it, until now...
My wife got some chicks, I made a nice house for them in the backyard. I decided to make an automatic door opener/closer, using a Firgelli automations linear actuator. The idea is to close the chicken coop door at night to protect the birds from predators (EVERYBODY likes chicken!), and open it again at dawn hopefully long before I wake up. I wrote the following code, and while it didn't work last night (I think I set the light sensor to be too insensitive to light), I'm hoping it will work tonight. I'm confused about "goto" vs "gosub" and I'm wondering if my use of these commands makes sense.
I also want to add a "chicken squeezer" routine using the Ping- so the girls don't get squished by the door as it closes. Is the Ping the way to go here, or should I use an IR emitter/receiver pair like they had in stores to let the proprietors know when somebody came in the door? I want to make sure that the door stays open if a chicken is in the way.
I can/will add some long pauses to wait for 30-40 minutes after dark before the door closes. Shorter pauses are in there now. The birds know to go inside when it gets dark, but I want to be sure all three are in there before the door shuts. I figure foxes and raccoons won't come around until midnight -3am.
Any suggestions would be appreciated- this is my first project, probably full of issues...
' {$STAMP BS2}
' {$PBASIC 2.5}
time VAR Word 'this sets rc time variable
morn VAR Nib 'morning counter so it only runs morning routine once per day
evening VAR Nib 'evening counter same reason
DO
HIGH 5
PAUSE 100
RCTIME 5, 1, time 'sets up photo resistor and capacitor on pin 5 for light sensing
DEBUG CLS, HOME, ? time, " " ' shows rc time- higher number = lower light
IF time > 200 THEN GOSUB night
IF time < 200 THEN GOSUB morning
LOOP
night:
DEBUG "night", CR
IF evening>0 THEN GOTO rest ' if evening counter has already run once- don't run again
PAUSE 10000
DEBUG "warning... CLOSING", CR
HIGH 1 'closes relay shuts chicken coop door
PAUSE 10000
LOW 1 'turns off relay -door is shut
evening=evening+1 'prevents running more than once a day
morn=0 'resets morning counter so it can run
RETURN
morning:
DEBUG "day", CR
IF morn>0 THEN GOTO rest 'morning counter has run- don't run again
PAUSE 2000
HIGH 2 'open door
PAUSE 10000 'relay stays on while door opens
LOW 2 'stops relay -door is open
morn = morn +1 'sets counter so sequence runs just once a day
evening=0 'resets night relay counter for night sequence
RETURN
rest: 'want to use a low current resting cycle for an hour or so at a time
DEBUG "resting", CR
PAUSE 10000
PAUSE 10000
RETURN
My wife got some chicks, I made a nice house for them in the backyard. I decided to make an automatic door opener/closer, using a Firgelli automations linear actuator. The idea is to close the chicken coop door at night to protect the birds from predators (EVERYBODY likes chicken!), and open it again at dawn hopefully long before I wake up. I wrote the following code, and while it didn't work last night (I think I set the light sensor to be too insensitive to light), I'm hoping it will work tonight. I'm confused about "goto" vs "gosub" and I'm wondering if my use of these commands makes sense.
I also want to add a "chicken squeezer" routine using the Ping- so the girls don't get squished by the door as it closes. Is the Ping the way to go here, or should I use an IR emitter/receiver pair like they had in stores to let the proprietors know when somebody came in the door? I want to make sure that the door stays open if a chicken is in the way.
I can/will add some long pauses to wait for 30-40 minutes after dark before the door closes. Shorter pauses are in there now. The birds know to go inside when it gets dark, but I want to be sure all three are in there before the door shuts. I figure foxes and raccoons won't come around until midnight -3am.
Any suggestions would be appreciated- this is my first project, probably full of issues...
' {$STAMP BS2}
' {$PBASIC 2.5}
time VAR Word 'this sets rc time variable
morn VAR Nib 'morning counter so it only runs morning routine once per day
evening VAR Nib 'evening counter same reason
DO
HIGH 5
PAUSE 100
RCTIME 5, 1, time 'sets up photo resistor and capacitor on pin 5 for light sensing
DEBUG CLS, HOME, ? time, " " ' shows rc time- higher number = lower light
IF time > 200 THEN GOSUB night
IF time < 200 THEN GOSUB morning
LOOP
night:
DEBUG "night", CR
IF evening>0 THEN GOTO rest ' if evening counter has already run once- don't run again
PAUSE 10000
DEBUG "warning... CLOSING", CR
HIGH 1 'closes relay shuts chicken coop door
PAUSE 10000
LOW 1 'turns off relay -door is shut
evening=evening+1 'prevents running more than once a day
morn=0 'resets morning counter so it can run
RETURN
morning:
DEBUG "day", CR
IF morn>0 THEN GOTO rest 'morning counter has run- don't run again
PAUSE 2000
HIGH 2 'open door
PAUSE 10000 'relay stays on while door opens
LOW 2 'stops relay -door is open
morn = morn +1 'sets counter so sequence runs just once a day
evening=0 'resets night relay counter for night sequence
RETURN
rest: 'want to use a low current resting cycle for an hour or so at a time
DEBUG "resting", CR
PAUSE 10000
PAUSE 10000
RETURN
Comments
When you use a GOSUB, the subroutine will have a "RETURN" at the end of it which tells it to come back to the code which called it. In other words, do that, then come back here to continue with the code.
GOTO means to go do that, and don't come back. Not sure what happens to the "RETURN" statement when you use a GOTO. If I use a GOTO, the routine which it calls may have another GOTO at then end of it to tell it where to go when it is done, not a "RETURN".
Rich H
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The Simple Servo Tester, a kit from Gadget Gangster.
http://www.youtube.com/watch?v=nuqlXCa03g0
http://www.youtube.com/watch?v=ofvi2Ls-fzk
Don't underestimate the strength & abilities of· raccoons & skunks. Wiley critters! Built a good locking mechanism.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·"If you build it, they will come."
Rule # 2· Always Check to see if you can make it fail if you can not make it fail then you are OK
·You could use an Optic sensor pair and you which way the chicken are going in or out how ever
·I will have to try to see how far apart you can have them and they ·still work
It·would also help if you an few switches to let you know if your door is open or closed could be very important to know
You could ·write your code in such a way that if the door is close or open then it would just loop and wait until the state changed from light to dark or dark to light
As far as the 30-40 minutes after dark before the door closes
I would do something like this
' {$STAMP BS2}
' {$PBASIC 2.5}
night··············· · VAR··· ·Word
time················· ·VAR···· Word
closed_switch···· ·VAR··· ·IN0
open_switch······ ·VAR··· ·IN3
DO
FOR night = 0 TO 365·············· ' The Value of 365 can be changed to get the time that you want
NEXT
IF time > 200 THEN··············· ·' Do not be temped to put this like IF time > 200· AND· IN0 = 0 may not work the way you want it to work
IF closed_switch·= 0 THEN·······'·This would your switch for door closed ·
···
PAUSE 500
ELSE
IF night = 364 THEN··············· ' The value need to be one less than in the FOR· value
HIGH 2
ENDIF
ENDIF
ENDIF
LOOP
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·Now wanting to learn Spin· Thanks for any··that you may have and all of your time finding them
·
·
·
·
Sam
Post Edited (sam_sam_sam) : 5/11/2010 1:02:33 AM GMT
They are great pets- we hope to get eggs from them in the late summer...
Thanks, Sam for the tips- I don't understand them, but I'm going to take out my syntax manual and study them! Does the "night" variable use up time just as a for/next loop, running 365 times until it ends the loop? I'm going to try to figure it out...
Later in the story, he builds 4 different robots from tin cans, all with different functions. Magical things happen when they all get hit by lightning...
And just like the Professor on Gilligan's Island, Andy the mechanical genius never quite fixes the boat...
A great read for your youngsters or you! By Carol Ryrie Brink, ~1966. Occasionally on Ebay for $20-30.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·"If you build it, they will come."
night:
DO
FOR night = 0 TO 365·············· ' The Value of 365 can be changed to get the time that you want
NEXT
IF time > 200 THEN··············· ·' Do not be temped to put this like IF time > 200· AND· IN0 = 0 may not work the way you want it to work
IF closed_switch·= 0 THEN·······'·This would your switch for door closed ·
···
PAUSE 500
ELSE
This routine look at [noparse][[/noparse]time] and if·it·= (Dark) and then looks at·[noparse][[/noparse]closed_switch] and if it·= (Open) and both are TRUE THEN runs PAUSE 500
IF night = 364 THEN··············· ' The value need to be one less than in the FOR· value
Once the value reaches 364 THEN it runs HIGH 1 which then would close your door
HIGH 1
____________________________________________________________________________________________________________________
·I can/will add some long pauses to wait for 30-40 minutes after dark before the door closes
One Note It is not a good· ·to use a PAUSE command for that long· because your doing nothing but pausing
With the routine above you are looking·about every 1·/2 second at your photo cell to see if there is a change or not ( or ) or ·(and )·could count the amout of chicken that have come in the coop
This is·what I would use for what you want to do what have above
____________________________________________________________________________________________________________________
night:
DEBUG "night", CR
IF evening>0 THEN GOTO rest ' if evening counter has already run once- don't run again
PAUSE 10000
DEBUG "warning... CLOSING", CR
HIGH 1 'closes relay shuts chicken coop door
PAUSE 10000
LOW 1 'turns off relay -door is shut
evening=evening+1 'prevents running more than once a day
morn=0 'resets morning counter so it can run
RETURN
__________________________________________________________________________________________________________________
I hope this helps
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·Now wanting to learn Spin· Thanks for any··that you may have and all of your time finding them
·
·
·
·
Sam
Post Edited (sam_sam_sam) : 5/11/2010 3:05:49 AM GMT
Rich H
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The Simple Servo Tester, a kit from Gadget Gangster.
Post Edited (W9GFO) : 5/11/2010 5:32:27 AM GMT
This is great info- when I get home from work, I'm going to check these ideas out and report back... The door didn't close again last night, so I'm thinking my current setup still needs work. It works fine at my desk but not outside. I'm thinking the ambient outdoor light at night is higher than in the workshop.
Even the book mentioned sounds good for my 3 1/2 year old- the combined weight of the chickens closing the door... hmmmm
I hired my dog to read the monitor and bark out the warning to the chickens-
You'd think this would be expensive, but my dog works for scratch...
I pulled the fuse, and this morning removed the device. Like Sam says, I do believe the long pauses are unreliable-
I'm now trying Rich's example on my workbench- it has some really elegant code- I particularly like the idea of averaging the data coming from the light sensor. Using the RC time constant to measure daylight does seem unreliable, at least the way I was doing it. I have it set up with LED's showing activity and will definitely set up a Ping or IR pair to verify the door isn't blocked before it closes.
Thanks for all your help! I'll keep you updated
-Dan
Here is a second version of the same code how ever it dose not work the same try both
The line in blue is what is different in this code
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·Now wanting to learn Spin· Thanks for any··that you may have and all of your time finding them
·
·
·
·
Sam
Post Edited (sam_sam_sam) : 5/12/2010 1:01:51 PM GMT
Are you saying that the LED works as a light sensing photo cell?
But, I did read the comments and had a couple of quick thoughts..(don't crucify me if I missed them being addressed I'll study this thread later tonite...I still wouldn't mind knowing if it was addresssed tho.)
1--"Light/Dark & storms" or "Soft Set Clock"
2--Door Safety Sensors
3--RFid dark entry (curfew permissions)
4--Inside the house override
"Light/Dark & storms" or "Soft Set Clock"
If sensing light/dark...what happens for bad storms?· Since you're not using a Real time clock you COULD let a Word keep track of elapsed minutes someway.· Since time isonly going to be an approximation anyway so you could set a group of buttons that tell the code to set the word to say 7am/noon/4pm/6pm.· My train of thought is that if somone pressed the button (at the appropriate time)·and got a confirmation light then you know the unit has reset it's "clock" to that time.· Then as long as you daily or, everyother daily press the right button (say when you leave for work, when the kids get home from school, or you get home from work. [noparse][[/noparse]
Redundancy..kids get a chace to be responsible...while you can still do it in am/pm in case they misssed.].·Much easier to track day night.
Say·you paused 5 seconds thru each loop then track that with a byte to approximate a minute then use a word to track total minutes (Off the top of my head, that should cover 24 hours.) as long as you use the programmed·"Change time to X:00 (ie 7am/noon/4pm/6pm)" buttons every so often.....you could make the time check a more accurate before the unit starts looking for darkness
ONE caveat, the stamps not going to be responsive to door signals duing 5 sec pauses.
I would use pause·285 (300·- estimated interpreter delays)·& count it to 200 in the byte counter to approxmate a minute.· (oh, tweak it in testing!).· Allowing more frequent looping.
Door Safety Sensors
I would·also install ·elevator style door sensors. I'm safety paranoid, so I would use at least 4 break beam i/r sensors & a pressure sensor. (adjusting the door closer to stop before pressure sensor actuates. One of my favorites is a small·pipe insulation tube (foam) with strips of foil glued to the inside. set the door to reverse action during close if either system signals. (But·only·during closing.)·The stamp can check door closed status with a magnet switch. (Just me i didn't see how you do it now.).
RFid dark entry (curfew permissions)
Maybe·a floor mounted RFid sensor in the middle of the gantry (sealed in waterproofing plastic).· could pickup the chicks leg bracelet chips?·That could provide a·1 minute door open signal to allow admittance.· (assuming·one of the others won't want to go wandering.)·If the chicks get smart enough.....they could create a revolving door policy. (possibly over the top disclaimer)So, you could make the ramp a fenced tunnel & use a ir system to track an entry (closing an outer door) & wait for a valid chip response. (What would be the odds it would be a "tagged" piece of wildlife that comes after the henhouse...these days?)· Keep the outer door closed *& sound an alarm (in the house) if no RFid tag is found.·unless you're not going to be monitoring that often then, it would be safer to reopen the outer door after a few·minutes.
Inside the house override
Nothing fancy. wired signals that allow you to force the door into open/close mode as you require.
AND/OR Buy a garage door remote and hook it up as you please.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Before you criticize someone, walk a mile in his shoes. That way if he gets angry, he'll be a mile away and barefoot. - unknown
It works kind like photo switch meaning that the code dose the work to make an led act like a switch
I will see if I can find the post where it talk about how an LED can sense light changes with a litte help
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·Now wanting to learn Spin· Thanks for any··that you may have and all of your time finding them
·
·
·
·
Sam
I tried Sam's programs but can't figure how the LED acts as a light sensor- will still research this as the photocell/cap isn't that reliable...
I've been trying Rich's program on the workbench with really promising results so far. Thanks Rich!!! Averaging the photocell helps A LOT! I'm thinking about averaging it at two different times during the late afternoon/ evening (to check for storm darkness). Then, waiting for an hour (or two) and finally closing the door but only after getting the OK from an IR safety beam as described by Sumdawgy. I don't mind if the door closes at 10PM, just so it shuts, especially in the cold winter months. Our run is pretty secure now so I'm not so worried about predators right now...
The birds sleeping in the doors' path kinda freaked me out. I prefer round to flattened birds! I've also been thinking about inside-the-house control, but hadn't thought of the garage door opener idea. Maybe a house alarm remote, as these are cheap too... Going to put it back in service this weekend after I make the IR beam safety trigger...
I have tried numerous combinations of photo resistor, photo transistor and capacitor with no reliable results using RCTIME. I'll get a reading of 1 or 2 next to my worklight and two minutes later get a reading in the hundreds from exactly the same position. Even averaging out the input over ten or a hundred samples is unreliable. I thought about getting an A to D converter or a different type of light sensor, but I've now decided to use an op amp, like the 741, working as a comparator. I'll feed the photo resistor in a ladder into the positive input of the op-amp, and run the output into a stamp input. I will (hopefully) be able to give the STAMP a 5 volt and near zero volt input at sunrise and sunset. Any concerns I should have interfacing the 741 with a stamp input?
Very disappointing about RCTIME- which seems to be the only way to put analog directly into a stamp.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·"If you build it, they will come."
I'm going to try this comparator idea next- I think going digital into the stamp must be more reliable.
I just can't understand why exactly the same distance from a light source would vary by a value of 200 or more from one moment to the next. As you say, I will have to experiment more with RCTIME and different caps if my comparator idea fails.
But maybe move on from the 'building a photocell' idea and just buy one from your local hardware store. They make one that is used on outdoor lighting and controls 120 directly. Just use a 120 volt relay from the output of the photocell and have that control a stamp pin. They have built in delay of about 5 minutes, and only cost around $10.
You might be able to even open it up and change the timing resistor value to make the time delay longer and bypass the stamp all together... Any way you can make the door go very slow, (up to a minute to close) so if a little one is in the way, he would have time to move?
Just an idea...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·"If you build it, they will come."
Yeah that's why the Stamp needs to track AM/PM & a rough time. Cloudy days could cause premature closing.
I came across an old manual alarm clock I used in service.· They still sell these things as kits & in stores fairly cheaply.·
The clock has an alarm-output switch that can be wired to the stamp to update the estimated clock time.· They usually activate for 30-60minutes every 12 hours.· To Set/Reset the stamp there should be some sort of AM/PM indicator & a button to toggle AM/PM.· After that, set the clock's alarm to whatever time the stamp expects and....Concievably, it should run for months.
Although I'd recomend a more aggresive maintenace schedule [noparse]:)[/noparse]
PS. It occurred to me that setting the clock could trigger an alarm signal ...Have to workaround.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Before you criticize someone, walk a mile in his shoes. That way if he gets angry, he'll be a mile away and barefoot. - unknown
Post Edited (sumdawgy) : 5/18/2010 2:48:48 PM GMT