Adafruit DS3231 Precision RTC Breakout

Hello,
I'm trying to use the DS3231 RTC module from Adafruit to turn on and off a relay at a specific time of day. For example in 24h I want the relay to be on for 8 of those hours and off for the rest. I connected the module to Arduino as per Adafruit's instructions and also used their library(found here). The relay switches on when Digi IO is LOW and off when IO high.

I modified cede library to the look like the code seen below. The problem I have is that as soon as the time off finishes it does not carry on past the time specified in off hour and min line in the code. I've attached a photo of the serial monitor data. If power is removed from arduino after that time it "forgets" its state and and keeps IO LOW when it should be HIGH.

Basically I'm looking for a solution where the arduino is not losing it's state if there is a power cut.

I've just started playing with Arduino so my experience with programming is pretty much nonexistent atm. I've tried explaining this as best as I could.

// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include "RTClib.h"

RTC_DS3231 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

int Relay = 3;
const int OnHour = 7; //SET TIME TO ON RELAY (24 HOUR FORMAT)
const int OnMin = 30;
const int OffHour = 21; //SET TIME TO OFF RELAY
const int OffMin = 51;


void setup () {
  
  Serial.begin(9600);

  delay(1000); // wait for console opening

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  
  
  // If you need to set the time of the uncomment line 34 or 37
  // following line sets the RTC to the date & time this sketch was compiled
  // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  // This line sets the RTC with an explicit date & time, for example to set
  // January 21, 2014 at 3am you would call:
  // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  pinMode(Relay, OUTPUT);
  digitalWrite(Relay, LOW);
  

}

void loop () {
    DateTime now = rtc.now();

    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

  
    Serial.print("Temperature: ");
    Serial.print(rtc.getTemperature());
    Serial.println(" C");
    //Serial.println();
    
    if (now.hour() == OnHour && now.minute() == OnMin) {
    digitalWrite(Relay, LOW);
    Serial.println("LIGHT ON");
  }

    else if (now.hour() == OffHour && now.minute() == OffMin) {
    digitalWrite(Relay, HIGH);
    Serial.println("LIGHT OFF");
    
    } 
    
    
    delay(3000);
}

Serial_Monitor.png

Basically I'm looking for a solution where the arduino is not losing it's state if there is a power cut.

That is impossible. The Arduino outputs are all by definition LOW when the power is off, and when the power comes back on, the reboot process resets everything.

What you can do is change the program logic completely so that the timing interval is determined by a range of "minutes past midnight". Using that logic, the Arduino can immediately determine the proper output state at any time of the day or night.

For example, if the output is to be on from noon to 8PM, it is on if "minutes past midnight" is within the range of 720 to 1200 (there are 1440 minutes in 24H).

When the Arduino is powered down and then reboots, it gets the correct time from the RTC and carries on from there.