clock function freezes after random amount of time

Hello all,

I need some help. I have made a climate controller system together with somebody else (he’s busy so he sadly has no time to help me anymore) and everything works, but the clock function is not stable. After a while the clock simply freezes and all functions that rely on it dont work anymore. I’ve been debugging via the serial and everytime the last thing its says is ‘wintertime’ and shows no more updates beside the display backlight
Im debugging in a minimal setup now which is: arduino mega, nextion touchscreen and rtc.
Because of the wintertime print i think it’s something to do with the arduino time to sync with the RTC and see if its wintertime or summertime. Which is this code:

// de RTC module staat altijd ingesteld op de wintertijd
// de interne arduino klok wordt gelijkgezet met de RTC module en aangepast aan zomer,- en wintertijd
// De interne klok word gebruikt als tijd en periodiek gelijk gezet met de RTC module

/* set RTC module */
void SerialRTCtime(String input){
  input.replace("timeset=","");
  input.replace("-",":");
  input +="~";
  byte commaPosition;  /* the position of the next comma in the string */
  word data[6];
  for (byte i = 0; i <= 5; i++) {
    commaPosition = input.indexOf(':');
    data[i] = input.substring(0, commaPosition).toInt();
    input = input.substring(commaPosition + 1, input.length());
    Serial.println(data[i]);
  }
  // controleer op zomertijd
  if ((data[0] == 0) && (zomer == 1)) {data[0] = 23;}
  else { data[0] = data[0] - 1*zomer;}
  
  rtc.setTime(data[0], data[1], data[2]);  
  rtc.setTime(19, 56, 50) ; //24h format
  //rtc.setDate(15, 12, 2020);   // Set the date to January 1st, 2014
  rtc.setDate(data[3],data[4],data[5]);
  setRTCtime();
}


/*set arduino clock*/
void setRTCtime(){
  t = rtc.getTime();
  setTime(t.hour, t.min, t.sec, t.date, t.mon, t.year);
  
  if (inSummerTime(t.hour, t.date, t.mon, t.year, t.dow)){
    adjustTime(3600);
    zomer = 1;
   Serial.println("zomertijd");
  }
  else {
    zomer = 0;
   Serial.println("wintertijd");
   }
}

// sync arduino time met rtc module
void timeSync(){
  int s = second();
  timesyncCounter++;
  if ((timesyncCounter == 300) && (timesync == false)){
    if ((second() > 55) || (second()<5)){
      timesyncCounter = 290;
    }
    else {  
      setRTCtime();
      timesync = true;
      s-=second();
    //  Serial.println(s);
    }
  }
  if (timesyncCounter == 420){
    timesync = false;
    timesyncCounter = 120;
  }
}

wintertime is the arduino base time, and to this function syncs rtc time to the arduino time so it can check is its summer time (‘zomertijd’, its in dutch sorry) or wintertime. And when checked it prints it on the serial monitor.

Whole code

Thanks in advance!
Sincere,
T.

*EDIT: wanted to add a pic of the serial monitor when it freezes but it wont let me upload sorry.

**EDIT: serial monitor 'screenshot":

humidity: nan, temperature: nan, max temp: 24.00, mintemp: 18.00, DHTalarm: 0, UVIalarm: 0
16:39:30 31-01-2021, zon: 08:18-17:23, PWM a=245,1, b=122,1, c=255,1, UVlamp=1 indx=26.35, warmte=1, fogger a=0, b=0, c=0, Spray=0, fan=255,255,255 
16:39:31 31-01-2021, zon: 08:18-17:23, PWM a=245,1, b=122,1, c=255,1, UVlamp=1 indx=26.69, warmte=1, fogger a=0, b=0, c=0, Spray=0, fan=255,255,255 
16:39:32 31-01-2021, zon: 08:18-17:23, PWM a=245,1, b=122,1, c=255,1, UVlamp=1 indx=26.44, warmte=1, fogger a=0, b=0, c=0, Spray=0, fan=255,255,255 
16:39:33 31-01-2021, zon: 08:18-17:23, PWM a=245,1, b=122,1, c=255,1, UVlamp=1 indx=27.08, warmte=1, fogger a=0, b=0, c=0, Spray=0, fan=255,255,255 
16:39:34 31-01-2021, zon: 08:18-17:23, PWM a=245,1, b=122,1, c=255,1, UVlamp=1 indx=26.69, warmte=1, fogger a=0, b=0, c=0, Spray=0, fan=255,255,255 
16:39:35 31-01-2021, zon: 08:18-17:23, PWM a=245,1, b=122,1, c=255,1, UVlamp=1 indx=26.69, warmte=1, fogger a=0, b=0, c=0, Spray=0, fan=255,255,255 
16:39:36 31-01-2021, zon: 08:18-17:23, PWM a=245,1, b=122,1, c=255,1, UVlamp=1 indx=26.83, warmte=1, fogger a=0, b=0, c=0, Spray=0, fan=255,255,255 
16:39:37 31-01-2021, zon: 08:18-17:23, PWM a=245,1, b=122,1, c=255,1, UVlamp=1 indx=26.56, warmte=1, fogger a=0, b=0, c=0, Spray=0, fan=255,255,255 
16:39:38 31-01-2021, zon: 08:18-17:23, PWM a=245,1, b=122,1, c=255,1, UVlamp=1 indx=26.82, warmte=1, fogger a=0, b=0, c=0, Spray=0, fan=255,255,255 
16:39:39 31-01-2021, zon: 08:18-17:23, PWM a=245,1, b=122,1, c=255,1, UVlamp=1 indx=26.45, warmte=1, fogger a=0, b=0, c=0, Spray=0, fan=255,255,255 
humidity: nan, temperature: nan, max temp: 24.00, mintemp: 18.00, DHTalarm: 0, UVIalarm: 0
16:39:40 31-01-2021, zon: 08:18-17:23, PWM a=245,1, b=122,1, c=255,1, UVlamp=1 indx=26.32, warmte=1, fogger a=0, b=0, c=0, Spray=0, fan=255,255,255 
16:39:41 31-01-2021, zon: 08:18-17:23, PWM a=245,1, b=122,1, c=255,1, UVlamp=1 indx=26.56, warmte=1, fogger a=0, b=0, c=0, Spray=0, fan=255,255,255 
16:39:42 31-01-2021, zon: 08:18-17:23, PWM a=245,1, b=122,1, c=255,1, UVlamp=1 indx=26.96, warmte=1, fogger a=0, b=0, c=0, Spray=0, fan=255,255,255 
16:39:43 31-01-2021, zon: 08:18-17:23, PWM a=245,1, b=122,1, c=255,1, UVlamp=1 indx=26.94, warmte=1, fogger a=0, b=0, c=0, Spray=0, fan=255,255,255 
16:39:44 31-01-2021, zon: 08:18-17:23, PWM a=245,1, b=122,1, c=255,1, UVlamp=1 indx=26.69, warmte=1, fogger a=0, b=0, c=0, Spray=0, fan=255,255,255 
16:39:45 31-01-2021, zon: 08:18-17:23, PWM a=245,1, b=122,1, c=255,1, UVlamp=1 indx=26.95, warmte=1, fogger a=0, b=0, c=0, Spray=0, fan=255,255,255 
16:39:46 31-01-2021, zon: 08:18-17:23, PWM a=245,1, b=122,1, c=255,1, UVlamp=1 indx=26.57, warmte=1, fogger a=0, b=0, c=0, Spray=0, fan=255,255,255 
16:39:47 31-01-2021, zon: 08:18-17:23, PWM a=245,1, b=122,1, c=255,1, UVlamp=1 indx=26.68, warmte=1, fogger a=0, b=0, c=0, Spray=0, fan=255,255,255 
16:39:48 31-01-2021, zon: 08:18-17:23, PWM a=245,1, b=122,1, c=255,1, UVlamp=1 indx=26.45, warmte=1, fogger a=0, b=0, c=0, Spray=0, fan=255,255,255 
wintertijd

You can copy the text from the serial monitor and paste it here. Use code tags for it when posting it.

sterretje:
You can copy the text from the serial monitor and paste it here. Use code tags for it when posting it.

Thanks, didnt think of that! Added it.

If this is an AVR-based Arduino (Uno, Nano, etc.), the problem may be due to use of Strings.

The fix is to not use Strings, as they cause memory problems and unpredictable program crashes.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.