Arduino freezing/locking up every day

Good Day,

Ive been Banging my head in the few days with my arduino that seems to freeze up every night.
the lights and pump work fine and turn off when they should,but somewhere between 12am and 6am the arduino seems to go to into a frozen state and lights/pump do not not turn on again when they supposed to. I Have to power down the arduino and back up to get the code looping

I Have tried using the relay powered by the arduino and using an external 5v 2A power supply to run the arduino and the relays with same results.

If i change the RTC Time manually to before the lights come on to try replicate the problem it doesnt freeze up.

Below is the code im using, If anyone is able to assist me in troubleshooting if its a coding problem or hardware issue

#include <DS3231.h>

int LightsRelay = 8;
int PumpRelay = 7;
int PumpRelayState = HIGH;
unsigned long previousMillis = 0;
long OnTime = 60000;  //Pump Time on
long OffTime = 360000; //Pump time off

// Init the DS3231 using the hardware interface
DS3231  rtc(SDA, SCL);
Time t;



void setup()
{
  Serial.begin(115200);
  rtc.begin();
  pinMode(LightsRelay, OUTPUT);
  digitalWrite(LightsRelay, HIGH);
  pinMode(PumpRelay, OUTPUT);
  digitalWrite(PumpRelay, HIGH);
}

//count minutes from midnight
uint16_t get_time()
{
    uint16_t temp_time = 0;
    t = rtc.getTime();
    temp_time += t.hour * 60;
    temp_time += t.min;
    return temp_time;
}

void loop()
{
  t = rtc.getTime();
  Serial.print(t.hour);
  Serial.print(" hour(s), ");
  Serial.print(t.min);
  Serial.print(" minute(s), ");
  Serial.println(" ");
  Serial.print("Temperature: ");
  Serial.print(rtc.getTemp());
  Serial.println(" C");
  

//Turn lights off and on

uint16_t current_time = get_time();
  if (current_time >= 350 && current_time <= 1430){
    digitalWrite(LightsRelay,LOW);
    Serial.println("Lights ON");
  }
   else {
    digitalWrite(LightsRelay,HIGH);
    Serial.println("Lights OFF");
  }


//Turn Pump on and off
unsigned long currentMillis = millis();
 
  if((PumpRelayState == LOW) && (currentMillis - previousMillis >= OnTime))
  {
    PumpRelayState = HIGH;  // Turn it off
    previousMillis = currentMillis;  // Remember the time
    digitalWrite(PumpRelay, PumpRelayState);  // Update the actual LED
    Serial.println("Pump OFF");
  }
  else if ((PumpRelayState == HIGH) && (currentMillis - previousMillis >= OffTime))
  {
    PumpRelayState = LOW;  // turn it on
    previousMillis = currentMillis;   // Remember the time
    digitalWrite(PumpRelay, PumpRelayState);
    Serial.println("Pump On");
  }
  delay(1000);  
}

Any help would be great.

Is this line correct

  uint16_t current_time = get_time();

shouldn't it be

  uint16_t current_time = rtc.get_time();

and if it should then why does it exist as you have already got the time in line 38 - however I strongly advise that you don NOT use single character variable names. They make debugging impossible - how can you find all the instances of a variable called 't'?

Why is there a delay() in the program?

...R

I can't see a logic error which could cause erratic behaviour so maybe you should also look at the hardware and for electrical noise. For example the flyback diode on the relays, decoupling capacitors etc. etc.

Maybe post a schematic.

Looking at DS3231.h I have serious questions about your use of the library. Maybe a quick read here would help.

Sory My arduino skills are not great and been learning as i've been going along

Robin2: Is this line correct

  uint16_t current_time = get_time();

shouldn't it be

  uint16_t current_time = rtc.get_time();

and if it should then why does it exist as you have already got the time in line 38

I had the RTC displaying the time in the loop at first with

t = rtc.getTime();

, then someone suggested i try use the midnight counter

uint16_t get_time()

.

I was under the impression they 2 functions. one to display current time and other call minutes since midnight. Should the midnight counter function be in the loop instead?

Why is there a delay() in the program?

was using it to slow down the serial monitor responses.

DKWatson: Looking at DS3231.h I have serious questions about your use of the library. Maybe a quick read here would help.

I Am using this library http://www.rinkydinkelectronics.com/library.php?id=73 Its what was used and shown by the examples i went by. Nothing is said in the manual though about how to use a midnight counter. only setting and displaying dates and times.

I have a problem with 3rd party libraries that name themselves the same as existing libraries. The suggestion is that they’re are swappable which they are not. I suggest you contact the good people at Rinky-Dink Electronics and ask them what the problem with their library is.

R3plic8: I had the RTC displaying the time in the loop at first with

t = rtc.getTime();

, then someone suggested i try use the midnight counter

uint16_t get_time()

I was under the impression they 2 functions. one to display current time and other call minutes since midnight. Should the midnight counter function be in the loop instead?

The problem with the second line is where is the get_time() function defined? It is part of the RTC library - hence the need to preface it with rtc.

was using it to slow down the serial monitor responses.

Don't use delay(). Use another millis() test for that - for example

if (currentMillis - prevPrintMillis >= 1000) {
  prevPrintMillis += 1000;
  // print stuff
}

...R

Robin2: The problem with the second line is where is the get_time() function defined? It is part of the RTC library - hence the need to preface it with rtc.

I Think this is where the confusion is coming in between the 2 libraries.

Don't use delay(). Use another millis() test for that - for example

if (currentMillis - prevPrintMillis >= 1000) {
  prevPrintMillis += 1000;
  // print stuff
}

And then i will use my serial prints with the if statement?

Thanks i will give both options this a try a bit later today. I Actually just went and got a digital geyser timer and put the lights on that few minutes ago as i need the lights running properly when i go away this weekend.