I can not confirm your issue. I am not set up to completely test your code beyond the rtc functions, but this simplified version of your code indeed update the time.
10:36:49.868 -> FinishedSetup
10:36:54.886 ->
10:36:54.886 -> SERVO LOOP
10:36:54.886 ->
10:36:54.886 -> 2020/7/23 - 10:9:25609
10:36:56.903 ->
10:36:56.903 -> SERVO LOOP
10:36:56.903 ->
10:36:56.903 -> 2020/7/23 - 10:9:27609
10:36:58.938 ->
10:36:58.938 -> SERVO LOOP
10:36:58.938 ->
10:36:58.938 -> 2020/7/23 - 10:9:29609
10:37:00.965 ->
10:37:00.965 -> SERVO LOOP
10:37:00.965 ->
10:37:00.965 -> 2020/7/23 - 10:9:31609
10:37:02.991 ->
10:37:02.991 -> SERVO LOOP
10:37:02.991 ->
10:37:02.991 -> 2020/7/23 - 10:9:33609
#include <math.h>
#include <Dusk2Dawn.h>
#include <EEPROM.h>
#include <Wire.h>
#include <RTClib.h>
//Define what pins are used
const int doorUpSensorPin = 4; // reedswitch door open
const int doorDownSensorPin = 5; // reedswitch door down
const int doorDownMotorPin = 10; // wind motor down
const int DoorUpMotorPin = 9; // wind motor up
const int LEDRED = 12; // LED indicating door closed (for microswitch on pin 5)
const int LEDGREEN = 8; // LED indicating door OPEN ( for microswitch on pin 4)
const int transitLEDAmber = 7; // LED indicating door in TRANSIT ( neither of the microswitches active)
const byte CloseDoorManual = 3; // Close door by hand
int buttonstateclose = 0;
int currentMins;
int sunrise;
int sunset;
DateTime now;
//Are We Debugging?
const bool debug = true;
//Do we need to set/adjust RTC?
const bool setRTC = true;
//Define RTC
RTC_DS1307 rtc;
//define the location / timezone for dusk2dawn (this is yourcity, countrycode 2 digit)
// change yourcity an fill in coordinates and timezone
Dusk2Dawn Kapellen(51.31483014538207, 4.458376022992936, +1);
// the setup function runs once when you press reset or power the board
void setup()
{
//Set the mode the pins will operate in.
pinMode(DoorUpMotorPin, OUTPUT);
pinMode(doorDownMotorPin, OUTPUT);
pinMode(CloseDoorManual, INPUT);
//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 when setRTC = true.
// if (setRTC)
{
//write PC date and time while uploading and when setRTC = true
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
if (setRTC)
{
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
//uncomment line 71 to write date and time manually into RTC when line 38 setRTC = true. For testing sunset/sunrise
rtc.adjust(DateTime(2020, 7, 23, 10, 9, 20));
}
}
//switches setup
pinMode(doorUpSensorPin, INPUT);
pinMode(doorDownSensorPin, INPUT);
pinMode(LEDRED, OUTPUT);
pinMode(LEDGREEN, OUTPUT);
pinMode(transitLEDAmber, OUTPUT);
Serial.println("FinishedSetup");
if (digitalRead(doorUpSensorPin) == HIGH)
{
Serial.println("Door is open");
}
if (digitalRead(doorDownSensorPin) == HIGH)
{
Serial.println("Door is closed");
}
delay(3000);
}
}
// the loop function runs over and over again until power down or reset
void loop()
{
delay(2000);
if (debug)
{
Serial.println();
Serial.println("SERVO LOOP");
}
//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 = Kapellen.sunrise(now.year(), now.month(), now.day(), true);
sunset = Kapellen.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());
}
buttonstateclose = digitalRead(CloseDoorManual);
if (buttonstateclose == HIGH)
{
ManualClose();
}
//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....
//delay of 30 minutes after sunset time to make sure all chickens are inside before closing the door.
/*
if ((sunrise - 45 < currentMins && currentMins < sunset + 20) && (buttonstateclose != HIGH))
{
Serial.println("Door state -- must be OPEN");
while (digitalRead(doorUpSensorPin) == LOW)
{
raiseDoor();
digitalWrite(transitLEDAmber, HIGH);
}
}
else
{
Serial.println("Door state -- must be closed");
while (digitalRead(doorDownSensorPin) == LOW)
{
lowerDoor();
digitalWrite(transitLEDAmber, HIGH);
}
}
int DoorUp = digitalRead(doorUpSensorPin);
int DoorDown = digitalRead(doorDownSensorPin);
//This is just temporary switch debug code
//DoorUp is DOOR OPEN - DoorDown is DOOR CLOSED
if (DoorUp == HIGH)
{
Serial.println("Door Full OPEN");
digitalWrite(LEDRED, LOW);
digitalWrite(LEDGREEN, HIGH);
digitalWrite(transitLEDAmber, LOW);
stopDoor();
}
if (DoorDown == HIGH)
{
Serial.println("Door Full CLOSED");
digitalWrite(LEDRED, HIGH);
digitalWrite(LEDGREEN, LOW);
digitalWrite(transitLEDAmber, LOW);
stopDoor();
}
if ((DoorDown == LOW) && (DoorUp == LOW))
{
Serial.println("Door TRANSIT");
digitalWrite(LEDRED, LOW);
digitalWrite(LEDGREEN, LOW);
digitalWrite(transitLEDAmber, HIGH);
}
*/
}
//Wind the Door Up
void raiseDoor()
{
digitalWrite(DoorUpMotorPin, HIGH);
digitalWrite(doorDownMotorPin, LOW);
if (debug)
{
Serial.println("Door moving UP");
stopDoor();
}
}
//Wind The Door Down
void lowerDoor()
{
digitalWrite(doorDownMotorPin, HIGH);
digitalWrite(DoorUpMotorPin, LOW);
if (debug)
{
Serial.println("Door moving DOWN");
stopDoor();
}
}
//Stop the Door
void stopDoor()
{
digitalWrite(DoorUpMotorPin, LOW);
digitalWrite(doorDownMotorPin, LOW);
if (debug)
{
Serial.println("Door Stop");
}
}
//Close door manual
void ManualClose()
{
digitalWrite(doorDownMotorPin, HIGH);
digitalWrite(DoorUpMotorPin, LOW);
if (debug)
{
Serial.println("Door Closing Manual");
while (digitalRead(doorDownSensorPin) == LOW)
digitalWrite(transitLEDAmber, HIGH);
stopDoor();
}
}