// CONNECTIONS:
// DS3231 SDA --> SDA
// DS3231 SCL --> SCL
// DS3231 VCC --> 3.3v or 5v
// DS3231 GND --> GND

/* for software wire use below_
#include <SoftwareWire.h>  // must be included here so that Arduino library object file references work
#include <RtcDS3231.h>
SoftwareWire myWire(SDA, SCL);
RtcDS3231<SoftwareWire> Rtc(myWire);
 for software wire use above */
#define DS1307   //for test in wokwi portal

/* for normal hardware wire use below_ */
#include <Wire.h> // must be included here so that Arduino library object file references work
#ifdef DS1307
 #include <RTClib.h>
#else
 #include <RtcDS3231.h>
#endif

#ifdef DS1307
RTC_DS1307 rtc;
#define HIGH_ LOW
#define LOW_ HIGH
#else
RtcDS3231<TwoWire> Rtc(Wire);
#define HIGH_ HIGH
#define LOW_ LOW
#endif
/* for normal hardware wire use above */

#define L1  13
#define L2  4
#define L3  5
#define L4  6
#define L5  7
#define L6  8
#define L7  9
#define POT 2

#define DELAY  150
int i= 0;
//static uint8_t iStart = 19;
//static uint8_t iStop  = 5;
//test
static uint8_t iStart = 16;
static uint8_t iStop  = 5;
char logp[50];
int Pot_value;

void setup () 
{
    Serial.begin(115200);
  pinMode(POT,INPUT);
  pinMode(L1, OUTPUT);   //Set pin 13 as port 1
  pinMode(L2, OUTPUT);   //Set pin 4 as port 2
  pinMode(L3, OUTPUT);   //Set pin 5 as port 3
  pinMode(L4, OUTPUT);   //Set pin 6 as port 4
  pinMode(L5, OUTPUT);   //Set pin 7 as port 5
  pinMode(L6, OUTPUT);   //Set pin 8 as port 6
  pinMode(L7, OUTPUT);   //Set pin 9 as port 6
 attachInterrupt (digitalPinToInterrupt(POT), isr, CHANGE); // pressed

AllOff();  //initially al off
digitalWrite(L7, HIGH_);

    Serial.print("compiled: ");
    Serial.print(__DATE__);
    Serial.print(" ");
    Serial.println(__TIME__);

    //--------RTC SETUP ------------
    // if you are using ESP-01 then uncomment the line below_ to reset the pins to
    // the available pins for SDA, SCL
    // Wire.begin(0, 2); // due to limited pins, use pin 0 and 2 for SDA, SCL
  #if defined(DS1307)
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }
#else
 Rtc.Begin();
//
    RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
//    
    printDateTime(compiled);
    Serial.println();
Rtc.SetDateTime(compiled);
    if (!Rtc.IsDateTimeValid()) 
    {
//        // Common Cuases:
//        //    1) first time you ran and the device wasn't running yet
//        //    2) the battery on the device is low_ or even missing
//
        Serial.println("RTC lost confidence in the DateTime!");
//
//        // follow_ing line sets the RTC to the date & time this sketch was compiled
//        // it will also reset the valid flag internally unless the Rtc device is
//        // having an issue

        Rtc.SetDateTime(compiled);
    }
//
    if (!Rtc.GetIsRunning())
    {
        Serial.println("RTC was not actively running, starting now");
        Rtc.SetIsRunning(true);
    }

    RtcDateTime now = Rtc.GetDateTime();
    if (now < compiled) 
   {
        Serial.println("RTC is older than compile time!  (Updating DateTime)");
        Rtc.SetDateTime(compiled);
    }
    else if (now > compiled) 
    {
        Serial.println("RTC is newer than compile time. (this is expected)");
    }
    else if (now == compiled) 
    {
        Serial.println("RTC is the same as compile time! (not expected but all is fine)");
    }
//
//    // never assume the Rtc was last configured by you, so
//    // just clear them to your needed state
    Rtc.Enable32kHzPin(false);
    Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone); 

    //test date
    // RtcDateTime test = RtcDateTime(2021,11,01,10,29,50);
      
  //   Rtc.SetDateTime(test);
#endif
}

void isr() {
 Pot_value=map(analogRead(POT),0,1023,100,255);
 sprintf(logp,"ISR intr Brightness: %d",Pot_value);
 Serial.println(logp);
 //  FastLED.setBrightness(  BRIGHTNESS );
 }

void loop () 
{

uint8_t ihournow = 0;
uint8_t iminute = 0;
uint8_t iDate = 0;
uint8_t iMonth = 0;
int value;
//Read analog value from potentiometer middle pin
value=analogRead(POT);
//Map analog values 0-1024 to pwm values 0-255
value = map(value, 0, 1023, 0, 255);
sprintf(logp,"intr Brightness: %d",value);
Serial.println(logp);

#if defined(DS1307)
   DateTime now = rtc.now();
    printDateTime(now);
 ihournow = now.hour();
 iminute = now.minute();
 iDate = now.day();
 iMonth = now.month();

#else
  if (!Rtc.IsDateTimeValid()) 
  {
//        // Common Cuases:
//        //    1) the battery on the device is low_ or even missing and the power line was disconnected
    Serial.println("RTC lost confidence in the DateTime!");
  }
//
    RtcDateTime now = Rtc.GetDateTime();
    printDateTime(now);
    Serial.println();
//
// 
//
//  RtcTemperature temp = Rtc.GetTemperature();
//  temp.Print(Serial);
//  // you may also get the temperature as a float and print it
//    // Serial.print(temp.AsFloatDegC());
//    Serial.println("C");

  i = 0;
  //PotVal = analogRead(P1);    // read the value from the sensor
  // read the analog in value:
  
 // sensorValue = analogRead(P1);
  // map it to the range of the analog out:
//  PotVal = map(sensorValue, 0, 1023, 0, 255);
//  Serial.println(PotVal);
  //delay(1);
ihournow = now.Hour();
iminute = now.Minute();

//test hour value
//ihournow = 5;

iDate = now.Day();
iMonth = now.Month();

#endif

Serial.print("Current Hour:");
Serial.print(ihournow);
Serial.print("  Current Minute:");
Serial.println(iminute);


//check for sigle relay on off
//check for relay 7, we need to on it for 15 minutes
//if((ihournow == 10 || ihournow == 19) && (iminute >=30 && iminute < 50))
//{
//  Serial.println("L7 running..."); 
//digitalWrite(L7, LOW_);
//}
//else
//{
//  digitalWrite(L7, HIGH_);
//}




//19 <= 16 && 16 < 22
// if((iStart <= ihournow) && (ihournow < iStop)
 /*&& ((iDate >= 18) && (iDate <  5))*/
// ) 

 if(iStart <= ihournow  || ihournow < iStop)
 {
  
  Serial.println("running..."); 

  digitalWrite(L7, LOW_);

  for(i = 0;i<50;i++)  Flasher(DELAY);   delay(10);
  
  for(i=0;i<30;i++)  Flasher2pair(DELAY);   delay(10);
   
  for(i = 0;i<15;i++)  Flasher3pair(DELAY); delay(10);
    
  for(i=0;i<15;i++) Hole2(DELAY);  delay(10); 
  
    for(i=0;i<5;i++)   Wipe(DELAY);     delay(10);

    // for(i=0;i<5;i++)    BlinkAll(DELAY+50);  delay(1);
 }
 else
 {
   Serial.println("Not Running..."); 
   AllOff();
   digitalWrite(L7, HIGH_);
 }
   
   delay(1000); 
}//End of Loop()

void Flasher(int Timeout)
{
  digitalWrite(L6, HIGH_);  digitalWrite(L1, LOW_);
  delay(Timeout); 
  
  digitalWrite(L1, HIGH_);   digitalWrite(L2, LOW_);
  delay(Timeout); 
  
  digitalWrite(L2, HIGH_);  digitalWrite(L3, LOW_);
  delay(Timeout); 
  
  digitalWrite(L3, HIGH_);   digitalWrite(L4, LOW_);
  delay(Timeout); 
  
  digitalWrite(L4, HIGH_);   digitalWrite(L5, LOW_);
  delay(Timeout); 
  
  digitalWrite(L5, HIGH_);   digitalWrite(L6, LOW_);
  delay(Timeout); 
  
 }

#define countof(a) (sizeof(a) / sizeof(a[0]))
#if defined (DS1307)
void printDateTime(const DateTime& dt)
{
    char datestring[20];

    snprintf_P(datestring, 
            countof(datestring),
            PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
            dt.month(),
            dt.day(),
            dt.year(),
            dt.hour(),
            dt.minute(),
            dt.second() );
    Serial.println(datestring);
}

#else
void printDateTime(const RtcDateTime& dt)
{
    char datestring[20];

    snprintf_P(datestring, 
            countof(datestring),
            PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
            dt.Month(),
            dt.Day(),
            dt.Year(),
            dt.Hour(),
            dt.Minute(),
            dt.Second() );
    Serial.println(datestring);
}
#endif

void Flasher2pair(int Timeout)
{
  digitalWrite(L6, HIGH_);
  digitalWrite(L1, LOW_);
  digitalWrite(L2, LOW_);
  delay(Timeout); 
  
  digitalWrite(L1, HIGH_);
  digitalWrite(L2, LOW_);
  digitalWrite(L3, LOW_);
  delay(Timeout); 
  
  digitalWrite(L2, HIGH_);
  digitalWrite(L3, LOW_);
  digitalWrite(L4, LOW_);
  delay(Timeout); 
  
  digitalWrite(L3, HIGH_);
  digitalWrite(L4, LOW_);
  digitalWrite(L5, LOW_);
  delay(Timeout); 
  
  digitalWrite(L4, HIGH_);
  digitalWrite(L5, LOW_);
  digitalWrite(L6, LOW_);
  delay(Timeout); 

   digitalWrite(L5, HIGH_);
  digitalWrite(L6, LOW_);
  digitalWrite(L1, LOW_);
  delay(Timeout); 
 
}

void Flasher3pair(int Timeout)
{
  digitalWrite(L4, HIGH_);   digitalWrite(L5, HIGH_); digitalWrite(L6, HIGH_);
  digitalWrite(L1, LOW_);  digitalWrite(L2, LOW_); digitalWrite(L3, LOW_);
  delay(Timeout);

   digitalWrite(L5, HIGH_); digitalWrite(L6, HIGH_); digitalWrite(L1, HIGH_);
  digitalWrite(L2, LOW_);   digitalWrite(L3, LOW_); digitalWrite(L4, LOW_);
  delay(Timeout);

digitalWrite(L6, HIGH_);   digitalWrite(L1, HIGH_); digitalWrite(L2, HIGH_);
  digitalWrite(L3, LOW_); digitalWrite(L4, LOW_); digitalWrite(L5, LOW_);
  delay(Timeout);

  digitalWrite(L1, HIGH_);   digitalWrite(L2, HIGH_); digitalWrite(L3, HIGH_);
  digitalWrite(L4, LOW_); digitalWrite(L5, LOW_); digitalWrite(L6, LOW_);
  delay(Timeout);

digitalWrite(L2, HIGH_);   digitalWrite(L3, HIGH_); digitalWrite(L4, HIGH_);
  digitalWrite(L5, LOW_); digitalWrite(L6, LOW_); digitalWrite(L1, LOW_);
  delay(Timeout);

digitalWrite(L3, HIGH_);   digitalWrite(L4, HIGH_); digitalWrite(L5, HIGH_);
  digitalWrite(L6, LOW_); digitalWrite(L1, LOW_); digitalWrite(L2, LOW_);
  delay(Timeout);

  
  }

void Hole2(int Timeout)
{
  digitalWrite(L1, HIGH_);   digitalWrite(L2, HIGH_);
  digitalWrite(L3, LOW_);   digitalWrite(L4, LOW_);
  digitalWrite(L5, LOW_);   digitalWrite(L6, LOW_);
  delay(Timeout); 

  digitalWrite(L1, LOW_);  digitalWrite(L2, HIGH_);
  digitalWrite(L3, HIGH_); digitalWrite(L4, LOW_);
  digitalWrite(L5, LOW_);  digitalWrite(L6, LOW_);
 // digitalWrite(L7, HIGH_);
  delay(Timeout);

  digitalWrite(L1, LOW_);  digitalWrite(L2, LOW_);
  digitalWrite(L3, HIGH_); digitalWrite(L4, HIGH_);
  digitalWrite(L5, LOW_);  digitalWrite(L6, LOW_);
 // digitalWrite(L7, HIGH_);
  delay(Timeout);

  digitalWrite(L1, LOW_);  digitalWrite(L2, LOW_);
  digitalWrite(L3, LOW_);  digitalWrite(L4, HIGH_);
  digitalWrite(L5, HIGH_); digitalWrite(L6, LOW_);
 // digitalWrite(L7, HIGH_);
  delay(Timeout);

  digitalWrite(L1, LOW_); digitalWrite(L2, LOW_);
  digitalWrite(L3, LOW_); digitalWrite(L4, LOW_);
  digitalWrite(L5, HIGH_); digitalWrite(L6, HIGH_);
//  digitalWrite(L7, HIGH_);
  delay(Timeout);

  digitalWrite(L1, HIGH_); digitalWrite(L2, LOW_);
  digitalWrite(L3, LOW_);  digitalWrite(L4, LOW_);
  digitalWrite(L5, LOW_);  digitalWrite(L6, HIGH_);
//  digitalWrite(L7, LOW_);
  delay(Timeout);


}

void AllOff()
{
digitalWrite(L1, HIGH_);
digitalWrite(L2, HIGH_);
digitalWrite(L3, HIGH_);
digitalWrite(L4, HIGH_);
digitalWrite(L5, HIGH_);
digitalWrite(L6, HIGH_);
//digitalWrite(L7, LOW_);
//delay(timeout);
}


void AllOn()
{
digitalWrite(L1, LOW_);
digitalWrite(L2, LOW_);
digitalWrite(L3, LOW_);
digitalWrite(L4, LOW_);
digitalWrite(L5, LOW_);
digitalWrite(L6, LOW_);
}


void Wipe(int Timeout)
{

//forward
  digitalWrite(L1, LOW_); delay(Timeout);
  digitalWrite(L2, LOW_); delay(Timeout);
  digitalWrite(L3, LOW_);delay(Timeout);
  digitalWrite(L4, LOW_);delay(Timeout);
  digitalWrite(L5, LOW_);delay(Timeout);
  digitalWrite(L6, LOW_);

  delay(Timeout);
  digitalWrite(L1, HIGH_); delay(Timeout);
  digitalWrite(L2, HIGH_); delay(Timeout);
  digitalWrite(L3, HIGH_);delay(Timeout);
  digitalWrite(L4, HIGH_);delay(Timeout);
  digitalWrite(L5, HIGH_);delay(Timeout);
  digitalWrite(L6, HIGH_);
  //backward
  delay(Timeout);
  digitalWrite(L6, LOW_); delay(Timeout);
  digitalWrite(L5, LOW_); delay(Timeout);
  digitalWrite(L4, LOW_);delay(Timeout);
  digitalWrite(L3, LOW_);delay(Timeout);
  digitalWrite(L2, LOW_);delay(Timeout);
  digitalWrite(L1, LOW_);

  delay(Timeout);
  digitalWrite(L6, HIGH_); delay(Timeout);
  digitalWrite(L5, HIGH_); delay(Timeout);
  digitalWrite(L4, HIGH_);delay(Timeout);
  digitalWrite(L3, HIGH_);delay(Timeout);
  digitalWrite(L2, HIGH_);delay(Timeout);
  digitalWrite(L1, HIGH_);
delay(Timeout);
  
}

void BlinkAll (int Timeout)
{
AllOff();
//  digitalWrite(L7, HIGH_);
//  digitalWrite(L8, HIGH_);
    delay(Timeout);
  
  AllOn();
  delay(Timeout);
  
}
GND5VSDASCLSQWRTCDS1307+