#include "SevSeg.h"
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <DS3231.h>

const char *monthName[12] = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
tmElements_t tm;
SevSeg Display;
unsigned int number;
unsigned long currentMillis;
unsigned int Hour;
unsigned long previousMillis = 0;
const long interval = 500;

void setup()
{
  pinMode(A2, OUTPUT);
  pinMode(8, INPUT_PULLUP);  // button1 is connected to pin A1
  pinMode(9, INPUT_PULLUP); // button2 is connected to pin A0
  byte numDigits = 4;
  byte digitPins[] = {10, 11, 12, 13};
  byte segmentPins[] = {2, 3, 4, 5, 6, 7, 8, 9};
  bool resistorsOnSegments = true;
  bool updateWithDelaysIn = true;
  byte hardwareConfig = COMMON_CATHODE;
  Display.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
  Display.setBrightness(100);
  bool parse=false;
  bool config=false;

  // get the date and time the compiler was run
  if (getDate(__DATE__) && getTime(__TIME__)) 
  {
    parse = true;
    // and configure the RTC with this info
    if (RTC.write(tm)) 
    {
      config = true;
    }
  }
  dots();
}
void loop()
{
  tmElements_t tm;
  
if (RTC.read(tm))
  {
    Hour = tm.Hour;
    if (tm.Hour > 23) Hour = 0;
    
  }
  number = Hour * 100 + tm.Minute;
  Display.setNumber(number,2);
  Display.refreshDisplay();
}

bool getTime(const char *str)
{
  int Hour, Min, Sec;

  if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  tm.Hour = Hour;
  tm.Minute = Min;
  tm.Second = Sec;
  return true;
}

bool getDate(const char *str)
{
  char Month[12];
  int Day, Year;
  uint8_t monthIndex;

  if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  for (monthIndex = 0; monthIndex < 12; monthIndex++) 
  {
    if (strcmp(Month, monthName[monthIndex]) == 0) break;
  }
  if (monthIndex >= 12) return false;
  tm.Day = Day;
  tm.Month = monthIndex + 1;
  tm.Year = CalendarYrToTm(Year);
  return true;
}

void dots() {
  digitalWrite(A2, HIGH);
}
GND5VSDASCLSQWRTCDS1307+