Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // libraries
- #include <SimpleTimer.h> // load the SimpleTimer library to make timers, instead of delays & too many millis statements
- // print debug messages or not to serial
- const boolean SerialDisplay = true; //true or false
- /*
- * Copyright 2016, David Naves (http://daveworks.net, http://davenaves.com)
- *
- * 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, see
- */
- /*
- * 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 assignments
- // temperature chip i/o
- const int photocellPin = A0; // photocell connected to analog 0
- const int enableCoopDoorMotorB = 4; // enable motor b - pin 4
- const int directionCloseCoopDoorMotorB = 2; // direction close motor b - pin 2
- const int directionOpenCoopDoorMotorB = 3; // direction open motor b - pin 3
- const int bottomSwitchPin = 8; // bottom switch is connected to pin 8
- const int topSwitchPin = 9; // top switch is connected to pin 9
- const int relayInteriorLight = 12 ; // interior lights relay set to digital pin 12
- // variables
- // 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
- // SimpleTimer objects
- SimpleTimer coopPhotoCellTimer;
- // debounce delay
- long lastDebounceTime = 0;
- long debounceDelay = 100;
- // interior lights twighlight delay
- long lastTwilightTime = 0;
- long TwilightDelay = 300000; // 5 minutes
- // ************************************** the setup **************************************
- void setup(void) {
- Serial.begin(9600); // initialize serial port hardware
- // welcome message
- if(SerialDisplay){
- Serial.println(" Processes running:");
- Serial.println(" Timer readPhotoCell every 10 minutes - light levels: open or close door");
- }
- // 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
- // interior lights relay
- pinMode(relayInteriorLight, OUTPUT);
- digitalWrite(relayInteriorLight, HIGH);
- // timed actions setup
- coopPhotoCellTimer.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 coopPhotoCellTimer set in setup
- photocellReading = analogRead(photocellPin);
- if(SerialDisplay){
- Serial.print(" Photocel Analog Reading = ");
- Serial.println(photocellReading);
- }
- // set photocel threshholds
- if (photocellReading >= 0 && photocellReading <= 445) {
- photocellReadingLevel = '1';
- if(SerialDisplay){
- Serial.print(" Photocel Reading Level:");
- Serial.println(" - Dark");
- }
- }
- else if (photocellReading >= 450 && photocellReading <= 595){
- photocellReadingLevel = '2';
- if(SerialDisplay){
- Serial.print(" Photocel Reading Level:");
- Serial.println(" - Twilight");
- }
- }
- else if (photocellReading >= 600 ) {
- photocellReadingLevel = '3';
- if(SerialDisplay){
- 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);
- if ((millis() - lastDebounceTime) > debounceDelay) { // delay 10ms for consistent readings
- bottomSwitchPinVal2 = digitalRead(bottomSwitchPin); // read input value again to check or bounce
- if (bottomSwitchPinVal == bottomSwitchPinVal2) { // make sure we have 2 consistant readings
- if (bottomSwitchPinVal != bottomSwitchState) { // the switch state has changed!
- bottomSwitchState = bottomSwitchPinVal;
- }
- if(SerialDisplay){
- 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);
- if ((millis() - lastDebounceTime) > debounceDelay) { // delay 10ms for consistent readings
- topSwitchPinVal2 = digitalRead(topSwitchPin); // read input value again to check or bounce
- if (topSwitchPinVal == topSwitchPinVal2) { // make sure we have 2 consistant readings
- if (topSwitchPinVal != topSwitchState) { // the button state has changed!
- topSwitchState = topSwitchPinVal;
- }
- if(SerialDisplay){
- 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();
- if(SerialDisplay){
- 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();
- if(SerialDisplay){
- Serial.print(" Coop Door open - danger!");
- }
- }
- }
- // turn on interior lights at dusk and turn off after door shuts
- void doCoopInteriorLightDusk() {
- if ((millis() - lastTwilightTime) > TwilightDelay) { // delay 5 mins
- readPhotoCell();
- bottomSwitchPinVal = digitalRead(bottomSwitchPin);
- if (bottomSwitchPinVal == 1 && photocellReading >= 4 && photocellReading <= 445) { // if bottom reed switch circuit is open and it's twilight
- digitalWrite (relayInteriorLight, HIGH);
- if (SerialDisplay) {
- Serial.println(" Interior Light: On");
- }
- }
- else if (bottomSwitchPinVal == 0) {
- digitalWrite (relayInteriorLight, LOW);
- if (SerialDisplay) {
- Serial.println(" Interior Light: Off");
- }
- }
- }
- }
- // do the coop door
- 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() {
- coopPhotoCellTimer.run(); // timer for readPhotoCell
- doCoopDoor();
- doCoopInteriorLightDusk();
- //delay(2000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement