Code execution froze inside while function, RTC

Hello Guys,
I am completely new to this forum and I have started my journey with Arduino recently. I decided to built driver for my parents’ filter in their house. It is supposed to handle automatic closing and opening of valves at specified times.
I have no idea why, but code stops working suddenly in one function when it was running for over 14h inside it (runOnceAfterMounting).

My goal is to set this driver to run rinsing once in every 2 weeks. I created this runOnceAfterMounting function to adjust time when rinsing starts and not to be forced to upload code at the time when I want it to start (probably there is easier way, but as I said, I am new to that kind of stuff).
When I was testing code with smaller values (around 30min) for following variables, it was working fine, whole code was running fine.

int NumberOfDays = 0;  // used in delayBetweenCycles                ######Value to be set
  int NumberOfHours = 12;   // used in delayBetweenCycles              ######## Value to be set
  int NumberOfMinutes = 0;  // used in delayBetweenCycles
  int NumberOfSeconds = 0;  // used in delayBetweenCycles
  int OnceNumberOfDays = 0;  // used in RunOnceAfterMounting                ######Value to be set  (hour correction)
  int OnceNumberOfHours = 17;   // used in RunOnceAfterMounting              #### Value to be set  (hour correction)
  int OnceNumberOfMinutes =25;  // used in RunOnceAfterMounting

When I set for example OnceNumberOfHours = 17 hours, code stopped executing 3h24m before it was supposed to and everything was frozen. LCD was showing value 3h24m till the end and it was lasting like that for whole night. In the morning even resets were not helpful (only log I got was “Inicjalizacja DS1307” which is very beginig of the code). I touched RTC and probably disconected battery, then I saw log “RTC is NOT running!” and next reset made my project work again.

I have no idea what might cause such issue. Everything (LCD and clock) are powered directly from Arduino and it is powered via USB from laptop. Clock is TinyRTC.
Function that was running for several hours and finally froze is here:

void RunOnceAfterMounting()
{
    Sequence = EEPROM.read(1);
  
    Serial.print(F("SETUP"));
    Serial.println();
    StartTime = EEPROM.get(10, StartTime);
    Serial.print(StartTime.hour(), DEC);
    Serial.print(F(":"));
    Serial.print(StartTime.minute(), DEC);
    Serial.print(F(":"));
    Serial.print(StartTime.second(), DEC);
    Serial.print('\t');
    Serial.print(F("StartTime settled earlier"));
    Serial.println();
      
    while (FlagDelay28DaysONCE != true)
    {
      TimeNow = rtc.now();
      runValvesOnceEveryDay(); 
      filtrationLcdONCE();   
    
      if( TimeNow > (StartTime + TimeSpan(OnceNumberOfDays,OnceNumberOfHours,OnceNumberOfMinutes,0)) && TimeNow < (StartTime + TimeSpan(OnceNumberOfDays,OnceNumberOfHours,OnceNumberOfMinutes+1,0)))     // leave 1 minuyte safety
      {      
        StartTime = StartTime + TimeSpan(OnceNumberOfDays,OnceNumberOfHours,OnceNumberOfMinutes,0);
        FlagDelay28DaysONCE = true;
      } 

      if( TimeNow == (StartTime + TimeSpan(OnceNumberOfDays,OnceNumberOfHours,OnceNumberOfMinutes,0)))     
      {    
        StartTime = StartTime + TimeSpan(OnceNumberOfDays,OnceNumberOfHours,OnceNumberOfMinutes,0);
        FlagDelay28DaysONCE = true;
      }
    
      if (TimeNow > (StartTime + TimeSpan(OnceNumberOfDays,OnceNumberOfHours,OnceNumberOfMinutes+1,0)))   
      {
        Serial.print(F("TimeNow > StartTime+TimeSpan SETUP"));
        Serial.println();
        StartTime = StartTime + TimeSpan(7,0,0,0);      // wait tillnext saturaday
        Serial.print(F("Move cycle one week (SETUP function run once)"));
        Serial.println();    
      }
    }
  FlagDelay28DaysONCE = true;
  EEPROM.put(4, FlagDelay28DaysONCE);
  EEPROM.put(10, StartTime);
  addToCounter;
}

I am struggling with this issue for longer time. I was trying several things, changed clocks, changed power source but nothing helps.
Maybe I am exceeding memory in this fucntion (but to be honest don’t know where…)
I guess it might be somehow related to the clock. As I previously said, when I ahve shorete values of time everything works perfectly. It stops working when this fucntion needs to be executed for longer time (many hours). it is supposed to last in this function for 14 days, at least I want it like that…

Thank you in advance!

And whole code is attached to the post in .txt file

Code.txt (14.4 KB)

I cannot see your code. If it is too large to post then please attach it to a post

How are your timing variables declared ?

Time variables I have declared in such way

  DateTime TimeNow;
  DateTime timeNowOncePerDay;
  DateTime StartTime;
  DateTime TimeIn28Days;

Whole code in attachemnt

Code.txt (14.4 KB)

If the valve function is not called within the while statement which is hanging does the code still hang?

while (FlagDelay28DaysONCE != true)
    {
      TimeNow = rtc.now();
     // runValvesOnceEveryDay();
      filtrationLcdONCE();

Wasn't testing this solution.

One time code was running function runValvesOnceEveryDay all the time ( I assume). Unortunatelly I have no logs and don't know what was on the display as my parents disconected everything from power source.

they only said that all valves vere runing on and off in period ~2seconds which matches this function. DelayOncePerDay = 2s.

But that would mean that clock stoped working at given hour, given minute, and one of 4 seconds as SecondOncePerDay is set to 55. What is more, stopped working at that time after few hours of normal work. Nobody was touching whole project or antyhing. It just hang like from nowhere.

void runValvesOnceEveryDay()
{
   timeNowOncePerDay = rtc.now();
      if (timeNowOncePerDay.hour() == HourOncePerDay)
      {
        if (timeNowOncePerDay.minute() == MinuteOncePerDay)
        {  
          if (timeNowOncePerDay.second() >= SecondOncePerDay)
          {
          Serial.print(F("Czyszczenie zaworow:"));
          startCleaningValvesOncePerDay();
          delay(DelayOncePerDay); 
          stopCleaningValvesOncePerDay();
          delay(3000);
          }
        }
      }