Stap 10: De Arduino Code voor de deur van automatische Chicken Coop
Opmerking: voor een mooiere weergave van de beelden, delen, schema's en code, bezoek:
http://davenaves.com/blog/interests-projects/Chickens/Chicken-Coop/Arduino-Chicken-door/
Insider info
Hopelijk u sommige om tijd te besparen, ik laat je op de truc welk tenslotte got deze deur om te werken met de lichtniveaus, debouncing van de schakelopties en * de kippen *. (zoals u in de code ziet) Controleer het licht niveaus om de 10 minuten om te voorkomen dat de lezingen stuiteren heen en weer tussen donker/twilight/licht tijdens deze dawn/dusk minuten. Vervolgens, wanneer "dark" is bereikt (voor mij koos ik > = 0 & & < = 3 gebaseerd die op toen mijn kippen eigenlijk ging & bleef in de kippenren) inschakelen motor dir naar beneden > debounce de switches > stoppen. Vervolgens het tegenovergestelde te doen voor de ochtend. Ik ben zeker dat er andere, misschien meer efficiënte methoden, maar deze code heeft geweest lopende flawlessly voor een tijdje nu en ik voel me zeker genoeg om uit te gaan 's nachts zonder zorgen te maken over roofdieren. Hoewel ik nog steeds een of andere manier een reden om te controleren de ChickenCam van tijd tot tijd te vinden. (op dit moment wachten voor mijn nieuwe servomotoren en nacht visie webcam te komen in de mail)
// libraries <p>// libraries<br> </p><p>#include < SimpleTimer.h > // load the SimpleTimer library to make timers, instead of delays & too many millis statements<br> #include < OneWire.h > // load the onewire library</p>/* (before running the sketch, remove spaces before and after the above ">" "<" characters - the instructables editor wouldn't publish the includes unless i added those spaces) */ /* * Copyright 2013, David Naves (http://daveworks.net, <a rel="nofollow"> http://davenaves.com) </a> * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 3 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301, USA. */ /* * I'm hoping that if you use/modify this code, you will share your * coop project with me and the world (pictures, whatever) * I'm big on sharing. * Cheers, * //D */ //pins const int photocellPin = A0; // photocell connected to analog 0 const int enableCoopDoorMotorB = 7; // enable motor b - pin 7 const int directionCloseCoopDoorMotorB = 8; // direction close motor b - pin 8 const int directionOpenCoopDoorMotorB = 9; // direction open motor b - pin 9 const int bottomSwitchPin = 26; // bottom switch is connected to pin 26 const int topSwitchPin = 27; // top switch is connected to pin 27 // vars // photocell int photocellReading; // analog reading of the photocel int photocellReadingLevel; // photocel reading levels (dark, twilight, light) // reed switches top and bottom of coop door // top switch int topSwitchPinVal; // top switch var for reading the pin status int topSwitchPinVal2; // top switch var for reading the pin delay/debounce status int topSwitchState; // top switch var for to hold the switch state // bottom switch int bottomSwitchPinVal; // bottom switch var for reading the pin status int bottomSwitchPinVal2; // bottom switch var for reading the pin delay/debounce status int bottomSwitchState; // bottom switch var for to hold the switch state // just need 1 SimpleTimer object SimpleTimer coopTimer; // ************************************** the setup ************************************** void setup(void) { Serial.begin(9600); // welcome message Serial.println(" Checking doCoopDoor: every 10 minutes for light levels to open or close door"); Serial.println(); // coop door // coop door motor pinMode (enableCoopDoorMotorB, OUTPUT); // enable motor pin = output pinMode (directionCloseCoopDoorMotorB, OUTPUT); // motor close direction pin = output pinMode (directionOpenCoopDoorMotorB, OUTPUT); // motor open direction pin = output // coop door switches // bottom switch pinMode(bottomSwitchPin, INPUT); // set bottom switch pin as input digitalWrite(bottomSwitchPin, HIGH); // activate bottom switch resistor // top switch pinMode(topSwitchPin, INPUT); // set top switch pin as input digitalWrite(topSwitchPin, HIGH); // activate top switch resistor // timed actions setup coopTimer.setInterval(600000, readPhotoCell); // read the photocell every 10 minutes } // functions // operate the coop door // photocel to read levels of exterior light void readPhotoCell() { // function to be called repeatedly - per cooptimer set in setup photocellReading = analogRead(photocellPin); Serial.print(" Photocel Analog Reading = "); Serial.println(photocellReading); // set photocel threshholds if (photocellReading >= 0 && photocellReading <= 3) { photocellReadingLevel = '1'; Serial.print(" Photocel Reading Level:"); Serial.println(" - Dark"); } else if (photocellReading >= 4 && photocellReading <= 120){ photocellReadingLevel = '2'; Serial.print(" Photocel Reading Level:"); Serial.println(" - Twilight"); } else if (photocellReading >= 125 ) { photocellReadingLevel = '3'; Serial.print(" Photocel Reading Level:"); Serial.println(" - Light"); } } //debounce bottom reed switch void debounceBottomReedSwitch() { //debounce bottom reed switch bottomSwitchPinVal = digitalRead(bottomSwitchPin); // read input value and store it in val delay(10); bottomSwitchPinVal2 = digitalRead(bottomSwitchPin); // read input value again to check or bounce if (bottomSwitchPinVal == bottomSwitchPinVal2) { // make sure we got 2 consistant readings! if (bottomSwitchPinVal != bottomSwitchState) { // the switch state has changed! bottomSwitchState = bottomSwitchPinVal; } Serial.print (" Bottom Switch Value: "); // display "Bottom Switch Value:" Serial.println(digitalRead(bottomSwitchPin)); // display current value of bottom switch; } } // debounce top reed switch void debounceTopReedSwitch() { topSwitchPinVal = digitalRead(topSwitchPin); // read input value and store it in val delay(10); topSwitchPinVal2 = digitalRead(topSwitchPin); // read input value again to check or bounce if (topSwitchPinVal == topSwitchPinVal2) { // make sure we got 2 consistant readings! if (topSwitchPinVal != topSwitchState) { // the button state has changed! topSwitchState = topSwitchPinVal; } Serial.print (" Top Switch Value: "); // display "Bottom Switch Value:" Serial.println(digitalRead(topSwitchPin)); // display current value of bottom switch; } } // stop the coop door motor void stopCoopDoorMotorB(){ digitalWrite (directionCloseCoopDoorMotorB, LOW); // turn off motor close direction digitalWrite (directionOpenCoopDoorMotorB, LOW); // turn on motor open direction analogWrite (enableCoopDoorMotorB, 0); // enable motor, 0 speed } // close the coop door motor (motor dir close = clockwise) void closeCoopDoorMotorB() { digitalWrite (directionCloseCoopDoorMotorB, HIGH); // turn on motor close direction digitalWrite (directionOpenCoopDoorMotorB, LOW); // turn off motor open direction analogWrite (enableCoopDoorMotorB, 255); // enable motor, full speed if (bottomSwitchPinVal == 0) { // if bottom reed switch circuit is closed stopCoopDoorMotorB(); Serial.print(" Coop Door Closed - no danger"); } } // open the coop door (motor dir open = counter-clockwise) void openCoopDoorMotorB() { digitalWrite(directionCloseCoopDoorMotorB, LOW); // turn off motor close direction digitalWrite(directionOpenCoopDoorMotorB, HIGH); // turn on motor open direction analogWrite(enableCoopDoorMotorB, 255); // enable motor, full speed if (topSwitchPinVal == 0) { // if top reed switch circuit is closed stopCoopDoorMotorB(); Serial.print(" Coop Door open - danger!"); } } void doCoopDoor(){ if (photocellReadingLevel == '1') { // if it's dark if (photocellReadingLevel != '2') { // if it's not twilight if (photocellReadingLevel != '3') { // if it's not light debounceTopReedSwitch(); // read and debounce the switches debounceBottomReedSwitch(); closeCoopDoorMotorB(); // close the door } } } if (photocellReadingLevel == '3') { // if it's light if (photocellReadingLevel != '2') { // if it's not twilight if (photocellReadingLevel != '1') { // if it's not dark debounceTopReedSwitch(); // read and debounce the switches debounceBottomReedSwitch(); openCoopDoorMotorB(); // Open the door } } } } // ************************************** the loop ************************************** void loop() { // polling occurs coopTimer.run(); doCoopDoor(); }
Geleerde lessen
Wat ik heb geleerd over de deur, Arduino, licht en bouw:
- Beste om te controleren de lichtniveaus elke 10 minuten om te voorkomen dat de lezingen stuiteren heen en weer tussen donker/twilight/licht tijdens deze dawn/dusk minuten
- Test uw deur met je kippen te zien als om het even welk van hen hangen willen buiten na uren
- De werkelijke licht waarden buiten testen is zeer belangrijke (veel variabelen betrokken: licht van huis van de buurman, wolken, intern/extern coop lichten enz.)
- Debouncing van uw switches binnen uw code Arduino is belangrijk (deur zal springen rond en niet als elektronische lezingen sterk door de milliseconde variëren)
- Bereiken voor hulp bij de Forums van de Arduino voordat trekken uit je haar. (mooi, doe je huiswerk en zeer concrete vragen)
- Mij gewijzigde vanuit micro-schakelaars voor reed-switches (magneten) omdat ik niet wil dat de mechanica van de micro-schakelaars niet na verloop van tijd
Wat ik heb geleerd over de kippen:
- Houden op een licht binnen de coop kunt houden kippen buiten langer (ik denk b/c het ambient licht schijnt buiten) en dat is belangrijk als het gaat om het automatiseren van deze deur, dus ze zal niet krijgen per ongeluk vergrendeld.
- Ze kunnen springen en vliegen (hoog en ver)
- Ze houden ervan om stok in veiligheid in de nacht, maar wil niets meer dan OUT krijg ik zo snel als het licht uit is