#include <Wire.h>
#include <DS3231.h>
DS3231 clk;
int format;
int hours;
int minutes;
int seconds;
String time_period = "";
 
bool h12Flag;
bool pmFlag;
////////////////////////
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
/////////////////////
void time_setup()
 {  
    Serial.println(" ");
    Serial.println("DS3231 based Clock and Calender");
    
    //Set up the preferred Time Format (12 hrs / 24 hrs)
    Serial.println("Select the time format (12/24) : ");
    while (Serial.available() == 0) { }                             // Wait for user input
    format = Serial.parseInt();
    Serial.print("Time format : ");
    Serial.print(format);                                           //Use this to set 12/24 clock cycle
    Serial.println(" hrs");

    //Take in the time values from the user
    Serial.println("Enter the hours : ");
    while(Serial.available() == 0) { }                              // Wait for user input
    hours = Serial.parseInt();
    
    Serial.println("Enter the minutes : ");
    while(Serial.available() == 0) { }                               // Wait for user input
    minutes = Serial.parseInt();
    
    Serial.println("Enter the seconds : ");
    while(Serial.available() == 0) { }                              // Wait for user input 
    seconds = Serial.parseInt();
    
    //If 12 hrs clock cycle, AM or PM?
    if(format == 12)
    {
      Serial.println("Time Period (AM/PM) : ");
      while(Serial.available() == 0) { }                            // Wait for user input
      time_period = Serial.readString();
    }


    
    //Print the values
    Serial.println("-------------------------------------------------------------");
    Serial.print("The RTC is set to : ");
    Serial.print(hours);
    Serial.print(" : ");
    Serial.print(minutes);
    Serial.print(" : ");
    Serial.print(seconds);
    if(format == 12)
    {
      Serial.print(" ");
      Serial.println(time_period);
    }
    else
      Serial.println(" ");
    Serial.println("-------------------------------------------------------------");
 }

 void RTC_setup()
 {
    clk.setHour(hours);
    clk.setMinute(minutes);
    clk.setSecond(seconds);
    if (format==12)
      clk.setClockMode(true);
    else
      clk.setClockMode(false);   
    delay(1000);
    Serial.println("RTC has be set with new time.");
    Serial.println("-------------------------------------------------------------");
    Serial.println("The DS3231 time is : ");
 }

 void RTC_display()
 {
    /*
     * This block displays the time in the serial monitor.
     */
    delay(1000);
    
    hours   = clk.getHour(h12Flag,pmFlag);
    minutes = clk.getMinute();
    seconds = clk.getSecond();

    // Print Hours
    if(hours<10)
      Serial.print(" 0");
    else
      Serial.print(" ");
    Serial.print(hours);
    Serial.print(" : ");

    // Print Minutes
    if(minutes<10)
      Serial.print("0");
    Serial.print(minutes);
    Serial.print(" : ");

    // Print Seconds
    if(seconds<10)
      Serial.print("0");
    Serial.print(seconds);


    //Print AM/PM
    if(format == 12)
    {
      Serial.print(" ");
      Serial.println(time_period);
    }
    else
      Serial.println(" ");

   // Reset AM/PM during clock transitions
    if (format == 12 && hours == 11 && minutes == 59 && seconds ==59)
    {
      if ( time_period == "AM")
        time_period = "PM";
      else if ( time_period == "PM")
        time_period = "AM";
    }
     
 }

void setup() {
  lcd.begin(16, 2);
  //lcd.backlight();
  lcd.print("Hello World!");

  time_setup();
  RTC_setup();

}

void loop() {
  // put your main code here, to run repeatedly:
  RTC_display();
}
$abcdeabcde151015202530354045505560fghijfghij
NOCOMNCVCCGNDINLED1PWRRelay Module
GND5VSDASCLSQWRTCDS1307+