#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <DS1307RTC.h>
#include <TimeLib.h>

int Day = 0, Month = 0, Year = 0, Hour = 0, Minute = 0, Second = 0, hr = 0;
const char *monthName[12] = 
{
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
#define OLED_ADDR   0x3C
Adafruit_SSD1306 display(-1);  // -1 = no reset pi

void setup() 
{
  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  display.clearDisplay();
  display.display();
  Serial.begin(9600);
  
}

void loop()
{
  //read date time and store to variables//
  
  tmElements_t tm;
  RTC.read(tm);                                
  Day = tm.Day;
  Month = tm.Month;
  Year = tmYearToCalendar(tm.Year);
  Hour = tm.Hour;
  Minute = tm.Minute;
  Second = tm.Second;

  
  if (RTC.read(tm))              //if data is available //
  {
  display.clearDisplay();
  display.setTextSize(2);       //set dispalay parameter//
  display.setTextColor(WHITE);
  display.setCursor(2,2);
  if(Hour<=12)                  //for AM
  {
      if(Hour<=9)
      {
      display.print("0");       //add 0 if value is lase then 9//
      display.print(Hour);      //print hour//
      }
      else
      {
      display.print(Hour);      //print hour without 0 if it is grather then 9//
      }
  }
  if(Hour>=13)                  //for PM
  {
    
      if((Hour-12)<=9)
      {
      display.print("0");        //add 0 if value is lase then 9//
      display.print(Hour-12);    //print hour - 12 convert into 12 hour mode//
      }
      else
      {
      display.print(Hour-12);   //print hour-12 without 0 if it is grather then 9//
      }
  }
   
  display.print(":"); 
  if(Minute<=9)
  {
    display.print("0");        //add 0 if value is lase then 9// 
    display.print(Minute);     //print Minute//
  }
  else
  {
    display.print(Minute);     //print Minute without 0 if it is grather then 9//
  }
  display.print(":");
  if(Second<=9)
  {
    display.print("0");        //add 0 if value is lase then 9// 
    display.print(Second);     //print Second//
  }
  else
  {
    display.print(Second);     //print Second without 0 if it is grather then 9//
  }

  if(Hour<=11)
  {
    display.setCursor(105,2);
    display.setTextSize(1);
    display.print("AM");      //print AM if hour is lase then 12
  }
   else if(Hour>=12) 
  {
    display.setCursor(105,2);
    display.setTextSize(1);
    display.print("PM");      //print PM if hour is grater or equal to 12
  } 
  display.setCursor(5,20);
  if(Day<=9)
  {
    display.print("0");        //add 0 if value is lase then 9// 
    display.print(Day);        //print Day//
  }
  else
  {
    display.print(Day);       //print Day without 0 if it is grather then 9//
  }
  display.print(" "); 
  display.print(monthName[Month-1]); //print month from month array by taking month index//
  display.display();
  delay(10);
  }
  
}
