#include <Wire.h>
#include <RTClib.h>

RTC_DS3231 rtc;

const int relayPins[] = {2, 3, 4, 5, 6};  
const int numRelays = sizeof(relayPins) / sizeof(relayPins[0]);

int prevDay = -1;
int currentRelay = 0;

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

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

  for (int i = 0; i < numRelays; i++) {
    pinMode(relayPins[i], OUTPUT);
    digitalWrite(relayPins[i], LOW);  // Turn off all relays initially
  }
  DateTime now = rtc.now();
  char buff[60];
	
		sprintf(buff,"%4d-%02d-%02d %02d:%02d:%02d,", \
			 now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
       Serial.println(buff);
}

void loop() {
  DateTime now = rtc.now();
  int currentDay = now.day();

  if (currentDay != prevDay) {
    Serial.print("New day detected! Previous day: ");
    Serial.print(prevDay);
    Serial.print(", Current day: ");
    Serial.println(currentDay);

    // Turn off the previous relay
    digitalWrite(relayPins[currentRelay], LOW);

    // Move to the next relay
    currentRelay = (currentRelay + 1) % numRelays;

    // Turn on the current relay
    digitalWrite(relayPins[currentRelay], HIGH);

    prevDay = currentDay;
  }

  delay(1000); // Wait for 1 second before checking again
}