Arduino sketch time stops after a particular time

hi,
i‘m working whit Arduino for some weeks and can’t make my sketch run continuously.
I’am using the rtc and the time lib.
Basically my sketch should open a gate and close a gate+ light on, once a day.
After calling the doorclose() in the evening and dooropen() in the morning(or the other way) the time just stops running. If I plugin my pc to the Arduino board the time starts running from this time when the function was calling last time. What I’am doing wrong.
I use Arduino IDK 1.0.6 and my Board is a Arduino Mega 2560

#include <Time.h>

#include <DS1302RTC.h>

#define debugg true
// pin assignments
#define photoPin A0
#define reedPinClose 2 // Tuer Schranke Zu
#define reedPinOpen 3 // Tuer Schranke Offen
#define relaisPin1  9	//Licht
#define relaisPin2 10	//NotLicht
#define relaisPin3 12	//Tor zu
#define relaisPin4 11	//Tor auf
#define OFF 1	//HIGH
#define ON 0	//LOW
#define DS1302_SCLK_PIN   6    // Arduino pin for the Serial Clock
#define DS1302_IO_PIN     7    // Arduino pin for the Data I/O
#define DS1302_CE_PIN     8    // Arduino pin for the Chip Enable
// Creation of the Real Time Clock Object

DS1302RTC RTC(DS1302_CE_PIN, DS1302_IO_PIN, DS1302_SCLK_PIN);
const int thresholdSunDown = 600;
const int thresholdSunUP = 650;
boolean doorclosed;
boolean LightOn;
boolean EmergencyLightOn;
byte dayTimeStart;
byte dayTimeEnds;
byte nightTimeStart;
byte nightTimeEnds;
//int currentLux;
time_t t;

void setup()  {
	if (debugg){
		Serial.begin(9600);
	}
	pinMode(photoPin, INPUT);
	pinMode(reedPinClose, INPUT);
	pinMode(reedPinOpen, INPUT);
	pinMode(relaisPin1, OUTPUT);
	pinMode(relaisPin2, OUTPUT);
	pinMode(relaisPin3, OUTPUT);
	pinMode(relaisPin4, OUTPUT);
	digitalWrite(relaisPin1, OFF);
	digitalWrite(relaisPin2, OFF);
	digitalWrite(relaisPin3, OFF);
	digitalWrite(relaisPin4, OFF);
	doorclosed = true;
	LightOn = false;
	EmergencyLightOn = false;
	dayTimeStart = 5;
	dayTimeEnds = 15;
	nightTimeStart = 16;
	nightTimeEnds = 4;
	// Set the current date, and time in the following format:
	//setTime(int hr,int min,int sec,int day, int month, int yr)
	//setTime(8, 21, 0, 30, 12, 2014);
	//RTC.set(now());

	initTime();

}
void loop()  {
	// This allows for the update of variables for time or accessing the individual elements.  
	while (checkNightTime() && !doorclosed){
		if (debugg){
			Serial.println("While1");
			debbug();
		}
		if ((lux() > thresholdSunDown) && !LightOn){
			lightOn(relaisPin1);
			while ((hour() == 20) && !EmergencyLightOn){
				lightOn(relaisPin2);
				delay(5000UL);
				lightOff(relaisPin1);
				t = now();
				if (minute(t) >= 10){
					lightOff(relaisPin2);
					doorClose();
					//delay(100UL);
					//initTime();
					if (debugg){
						Serial.println("While11");
						debbug();
					}
				}
			}
		}
	}

	while (checkDayTime() && doorclosed){
		if (lux() < thresholdSunUP){
			doorOpen();
			//delay(100UL);
			//initTime();
			if (debugg){
				Serial.println("While2");
				debbug();
			}
		}
	}
	//delay(1000UL);
	if (debugg){
		Serial.println("Outside");
		debbug();
	}
}
void lightOn(int RELAIS){
	if (digitalRead(RELAIS) == OFF)
	{
		digitalWrite(RELAIS, ON);
	}
}

void lightOff(int RELAIS){
	if (digitalRead(RELAIS) == ON)
	{
		digitalWrite(RELAIS, OFF);
	}
}

void doorClose(){
	//solange die Tuer nicht zu ist ist das Relais an

	unsigned long pasttime = now() + 21UL;
	unsigned long timetmp = now();
	while ((digitalRead(reedPinClose) == HIGH) && (pasttime > timetmp)){
		if (digitalRead(relaisPin3) == OFF)
		{
			digitalWrite(relaisPin3, ON);
		}
		delay(100UL);
		timetmp = now();
	}
	if ((digitalRead(reedPinClose) == LOW) || (pasttime < timetmp)){
		digitalWrite(relaisPin3, OFF);
	}
	doorclosed = !doorclosed;
	LightOn = true;
	EmergencyLightOn = true;
}

void doorOpen(){
	unsigned long pasttime = now() + 23UL;
	unsigned long timetmp = now();

	while ((digitalRead(reedPinOpen) == HIGH) && (pasttime > timetmp)){
		if (digitalRead(relaisPin4) == OFF)
		{
			digitalWrite(relaisPin4, ON);
		}
		delay(100UL);
		timetmp = now();
	}
	if ((digitalRead(reedPinOpen) == LOW) || (pasttime < timetmp)){
		digitalWrite(relaisPin4, OFF);
	}
	doorclosed = !doorclosed;
	LightOn = false;
	EmergencyLightOn = false;
}

boolean checkNightTime(){
	t = now();
	if ((hour(t) >= nightTimeStart) || (hour(t) <= nightTimeEnds)){
		return true;
	}
	else{
		return false;
	}
}
boolean checkDayTime(){
	t = now();
	if ((hour(t) >= dayTimeStart) && (hour(t) <= dayTimeEnds)){
		return true;
	}
	else{
		return false;
	}
}
void debbug(){

	Serial.print("Current Date / Time: ");                                                                 
	Serial.print(day());                                                                       
	Serial.print("/");                                                                                      
	Serial.print(month());                                                                              
	Serial.print("/");                                                                                     
	Serial.print(year());                                                                               
	Serial.print("  ");                                                                                   
	print2digits(hour());                                                                          
	Serial.print(":");                                                                                    
	print2digits(minute());                                                                           
	Serial.print(":");                                                                                      
	print2digits(second());
	Serial.println();  //| 
	Serial.print("Lux: ");
	Serial.println(lux());
	Serial.print("Doorclosed: ");
	Serial.println(doorclosed);
	if (timeStatus() == timeSet)
		Serial.println(" Ok!");
	else
		Serial.println(" FAIL!");
	delay(1000UL);

}
void print2digits(int number) {
	if (number >= 0 && number < 10) {
		Serial.print('0');
	}
	Serial.print(number);
}
int lux(){
	int lux = 0;
	for (int i = 0; i <= 9; i++){
		lux += analogRead(photoPin);
		delay(100);
	}
	return lux / 10;
}
void initTime(){
	RTC.haltRTC(false);
	setSyncProvider(RTC.get);
	setSyncInterval(60UL);
}
void doorClose(){
...
	while ((digitalRead(reedPinClose) == HIGH) && (pasttime > timetmp)){
...
		timetmp = now();
	}
	if ((digitalRead(reedPinClose) == LOW) || (pasttime < timetmp)){
		digitalWrite(relaisPin3, OFF);
	}
...
}

the if statement in doorClose() will always be true as it checks for the same conditions - albeit opposite - as the while statement. In other words when the while breaks either the first part of the if is true or the second part. so what appears a conditionally IF is not.

doorOpen() same story.

This does not solve the problem, it only pinpoints a logic flaw.

Thanks, that’s true. I correct this. but I guess the time bug still persist. If I call the open or doorclose() the time stops, I guess, after exiting the function. Due to recordet time. e.g on calling doorclose() at 20:10:23

I tried reading the code but the similar or identical names were confusing enough that I gave up: debugg/debbug LightOn/lightOn and the worst offender lux/lux.

I'd suggest fixing your identifiers before debugging - a few minutes of find and replace should do it.

I second look at your code shows that there is a lot of blocking code in the sketch. you might introduce a state machine that reads all sensors at start of loop and depending on their value changes the state of the system.

in the second half of the loop the state is reflected in the actuators (relays, leds etc) .

I have a feeling the code is rather complex for what you say it is intended to do. I imagine all you need in loop() is something like this pseudo code

void loop() {
   if (time == morning) {
      switchLight('f'); // n for ON and f for OFF
      moveDoor('o'); // o for OPEN and c for CLOSE
   }
  if (time == evening) {
    switchLight('n')
    moveDoor('c');
  }
}

...R

thanks for your suggestions and sorry for the messy code.
I just managed to write a new one. I will test it, to find out how it works. Hope there is no time bug anymore.

#include <Time.h>
#include <DS1302RTC.h>


// pin assignments
#define photoPin A0
#define reedPinClose 2 // Door closed
#define reedPinOpen 3 // Door open
#define relaisPin1  9	//Light
#define relaisPin2 10	//Light2
#define relaisPin3 12	//close Door
#define relaisPin4 11	//open Door
#define OFF 1	//HIGH
#define ON 0	//LOW
#define CLOSE 1	//HIGH
#define OPEN 0	//LOW
#define DS1302_SCLK_PIN   6    // Arduino pin for the Serial Clock
#define DS1302_IO_PIN     7    // Arduino pin for the Data I/O
#define DS1302_CE_PIN     8    // Arduino pin for the Chip Enable
// Creation of the Real Time Clock Object

DS1302RTC RTC(DS1302_CE_PIN, DS1302_IO_PIN, DS1302_SCLK_PIN);
const int thresholdSunDown = 600;
const int thresholdSunUP = 650;
boolean doorclosed;
byte dayTimeStart;
byte dayTimeEnds;
byte nightTimeStart;
byte nightTimeEnds;
unsigned int photoValue;
time_t t;


void setup()
{
	pinMode(photoPin, INPUT);
	pinMode(reedPinClose, INPUT);
	pinMode(reedPinOpen, INPUT);
	pinMode(relaisPin1, OUTPUT);
	pinMode(relaisPin2, OUTPUT);
	pinMode(relaisPin3, OUTPUT);
	pinMode(relaisPin4, OUTPUT);
	digitalWrite(relaisPin1, OFF);
	digitalWrite(relaisPin2, OFF);
	digitalWrite(relaisPin3, OFF);
	digitalWrite(relaisPin4, OFF);
	doorclosed = true;
	dayTimeStart = 5;
	dayTimeEnds = 15;
	nightTimeStart = 16;
	nightTimeEnds = 4;
	//setTime(int hr,int min,int sec,int day, int month, int yr)
	//setTime(8, 21, 0, 30, 12, 2014);
	//RTC.set(now());
	initTime();
}

void loop()
{
	t = now();
	photoValue = getPhotoValue();
	//switch light on while twilight till 20:00 and close the gate at 20:10
	if (checkNightTime(t) && !doorclosed){
		if (photoValue > thresholdSunDown){
			digitalWrite(relaisPin1, ON);
			if (hour(t) == 20){
				digitalWrite(relaisPin2, ON);
				delay(5000UL);
				digitalWrite(relaisPin1, OFF);
				if (minute(t) >= 10){
					digitalWrite(relaisPin2, OFF);
					doorMove(CLOSE);
					doorclosed = !doorclosed;
				}
			}
		}	
	}
	// opens the gate in the morning while sunrise
	if (checkDayTime(t) && doorclosed){
		if (photoValue < thresholdSunUP){
			doorMove(OPEN);
			doorclosed = !doorclosed;
		}
	}
}
//is needet for the time lib, to synch whit rtc
void initTime(){
	RTC.haltRTC(false);
	setSyncProvider(RTC.get);
	setSyncInterval(60UL);
}
//returns current average(of 10) photoresistor value
int getPhotoValue(){
	int photoValue = 0;
	for (int i = 0; i <= 9; i++){
		photoValue += analogRead(photoPin);
		delay(100);
	}
	return photoValue / 10;
}
// Open/Close a gate
void doorMove(boolean action){
	unsigned long pasttime = now() + 23UL;
	unsigned long timetmp = now();
	switch (action)
	{
	case 0:
		// Relays 4 control a left drive motor, which stops if reedpin is reached or after 23ec
		while ((digitalRead(reedPinOpen) == HIGH) && (pasttime > timetmp)){
				digitalWrite(relaisPin4, ON);
			delay(100UL);
			timetmp = now();
		}
		digitalWrite(relaisPin4, OFF);

		break;

	case 1:
		//Relays 3 control a right drive motor, which stops if reedpin is reached or after 23ec
		while ((digitalRead(reedPinClose) == HIGH) && (pasttime > timetmp)){
				digitalWrite(relaisPin3, ON);
			delay(100UL);
			timetmp = now();
		}
		digitalWrite(relaisPin3, OFF);
		break;
	default:
		break;
	}
}
boolean checkNightTime(time_t ttemp){
	//returns true if time between 16:00 � 04:00 
	return ((hour(ttemp) >= nightTimeStart) || (hour(ttemp) <= nightTimeEnds));
}
boolean checkDayTime(time_t ttemp){
	//returns true if time between 05:00 � 15:00
	return ((hour(ttemp) >= dayTimeStart) && (hour(ttemp) <= dayTimeEnds));
}