#include <TimeLib.h>

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);

  for (int i = 0; i < numRelays; i++) {
    pinMode(relayPins[i], OUTPUT);
    digitalWrite(relayPins[i], LOW);  // Turn off all relays initially
  }
  //Allow the clock to set the day - otherwise may change twice in first few seconds of pwoer on
  delay(2000);
}

void loop() {

  int currentDay = 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

    Serial.print("Turning OFF Relay on Pin ");
    Serial.println(relayPins[currentRelay]);
    digitalWrite(relayPins[currentRelay], LOW);

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

    // Turn on the current relay
    Serial.print("Turning ON  Relay on Pin ");
    Serial.println(relayPins[currentRelay]);
    digitalWrite(relayPins[currentRelay], HIGH);

    prevDay = currentDay;
  }

  delay(10000); // Wait for 10 second before checking again - could be much longer
}