Hi all,
Hi, I found a sketch online (github offcourse) for a chickencoop door based on sunset/sunrise. The previous one I've build was with an LDR but not accurate enough in some circumstances.
I've hooked up everything together, RTC clock working and running and the serial monitor prints text "door should be open" or "door should be down", so comparison works.
When the door is open (and should be open) the serial monitor prints "door opened all the way" "door stopped".
when I lower the door a bit by hand it starts raising it until reedswitch is activated again, so this also works.
The voids for door movements or also fine.
But... I cant figure out how I can lower the door when its sunset. It's also not programmed in this sketch.
Who can put me in the right direction? I'm getting a headache. I assume it's stupid simple but it's still quite new for me.
Many thanks,
#include <math.h>
#include <Dusk2Dawn.h>
#include <EEPROM.h>
#include <Wire.h>
//#include <OneWire.h>
//#include <DallasTemperature.h>
#include "RTClib.h"
//Define what pins things are hooked up to
const int doorUpSensorPin = 3; // reedswitch door open
const int doorDownSensorPin = 2; // reedswitch door down
const int doorDownMotorPin = 10; // wind motor down
const int DoorUpMotorPin = 9; // wind motor up
const int builtInLED = 13; // LED idicating door closed (reedswitch on pin 2)
int currentMins;
int sunrise;
int sunset;
DateTime now;
//Are We Debugging?
const bool debug = true;
//Define RTC
RTC_DS1307 rtc;
//define the location / timezone for dusk2dawn (this is Kessel, BE)
Dusk2Dawn Kessel(43.789369, -96.927453, -6);
// the setup function runs once when you press reset or power the board
void setup() {
//Set the mode the pins will operate in. In this case on/off as outputs
pinMode(DoorUpMotorPin, OUTPUT);
pinMode(doorDownMotorPin, OUTPUT);
//Set Serial for Debugging
if (debug) {
Serial.begin(9600);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
rtc.begin();
// //Lets just set the date/time every time we upload a debug build, since we know the RTC module is good with a good battery.
if (debug)
{
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
//switches setup
pinMode(doorUpSensorPin, INPUT);
pinMode(doorDownSensorPin, INPUT);
pinMode(builtInLED, OUTPUT);
Serial.println("FinishedSetup");
if (digitalRead(doorUpSensorPin) == HIGH)
{
//door is open all the way
}
}
// the loop function runs over and over again until power down or reset
void loop() {
delay(2000);
if (debug){
Serial.println();
Serial.println("LoopHead");
}
//Get the Date/Time from the RTC
now = rtc.now();
//Get Sunrise/Sunset for the current year/month/day as INT's that equate to minutes from midnight that he sunrise/sunset will occur (THE TRUE is passing in Daylight Savings Time!)
sunrise = Kessel.sunrise(now.year(), now.month(), now.day(), true);
sunset = Kessel.sunset(now.year(), now.month(), now.day(), true);
if (debug) {
Serial.println();
Serial.print(now.year());
Serial.print('/');
Serial.print(now.month());
Serial.print('/');
Serial.print(now.day());
Serial.print(" - ");
Serial.print(now.hour());
Serial.print(':');
Serial.print(now.minute());
Serial.print(':');
Serial.print(now.second());
}
//Lets get add the "now" Minutes and "now" hours*60 to see how many minutes from midnight we are
currentMins = ((now.hour()) * 60) + (now.minute());
Serial.println(currentMins);
//lets start comparisons, if the door should be up....
if (sunrise < currentMins && currentMins < sunset)
{
Serial.println("Door should be up");
}
else
{
Serial.println("Door should be down");
}
int DoorUp = digitalRead(doorUpSensorPin);
int DooDown = digitalRead(doorDownSensorPin);
/* This is just temporary switch debug code*/
if (DoorUp == HIGH)
{
Serial.println("Door Opened All The Way");
stopDoor();
digitalWrite(builtInLED, LOW);
}
else
{
digitalWrite(builtInLED, HIGH);
("Door Not Open all the way");
raiseDoor();
}
}
//Wind the Door Up
void raiseDoor() {
digitalWrite(DoorUpMotorPin, HIGH);
digitalWrite(doorDownMotorPin, LOW);
if (debug) {
Serial.println("Door Raising");
}
}
//Wind The Door Down
void lowerDoor() {
digitalWrite(doorDownMotorPin, HIGH);
digitalWrite(DoorUpMotorPin, LOW);
if (debug) {
Serial.println("Door Lowering");
}
}
//Stop the Door
void stopDoor() {
digitalWrite(DoorUpMotorPin, LOW);
digitalWrite(doorDownMotorPin, LOW);
if (debug) {
Serial.println("Door Stop");
}
}