// Parts of this code was modified from :
// https://github.com/PaulStoffregen/Time/blob/master/Time.cpp

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);  // Display I2C 16x2
#include <TimerOne.h>
const int led = LED_BUILTIN;  // the pin with a LED
volatile bool printOut = false;
volatile unsigned long blinkCount = 0; // use volatile for shared variables
volatile unsigned long myTime = 0;
unsigned int mySeconds = 0;
unsigned int myMinutes = 0;
unsigned int myHours = 0;
unsigned int myDay = 0;

//----------------------------------------------------------------
void setup() {
  lcd.begin(16, 2);
  lcd.backlight();
  lcd.clear();
  pinMode(led, OUTPUT);
  //Timer1.initialize(100000);  // Test with velocity time x 10
  Timer1.initialize(1000000);
  Timer1.attachInterrupt(blinkLED); // blinkLED to run every 1 seconds
  Serial.begin(9600);
  lcd.setCursor(1, 0);
  lcd.print("HH MM SS");
  lcd.setCursor(11, 0);
  lcd.print("DDDD");
}
//-------------------------------------------------------------------
void blinkLED() {
  blinkCount ++ ; // 1 second;
  //blinkCount += 60UL ; // test with  1 minute;
  //blinkCount += 3600UL ; // test with 1 hour;
  //blinkCount += 3600UL * 24UL; // test with 1 day;
  digitalWrite(led, !digitalRead(led));
  printOut = true;
}
//-------------------------------------------------------------------
void loop() {
  if (printOut == true) {
    myTime = blinkCount;
    mySeconds = myTime % 60;
    myTime /= 60; // now it is minutes
    myMinutes = myTime % 60;
    myTime /= 60; // now it is hours
    myHours = myTime % 24;
    myTime /= 24; // now it is days
    myDay = myTime % 24;
    lcd.setCursor(1, 1);
    if (myHours < 10)
      lcd.print("0");
    lcd.print(myHours);
    lcd.print(":");
    if (myMinutes < 10)
      lcd.print("0");
    lcd.print(myMinutes);
    lcd.print(":");
    if (mySeconds < 10)
      lcd.print("0");
    lcd.print(mySeconds);
    lcd.print(" ");
    lcd.print("    ");
    lcd.setCursor(11, 1);
    if (myDay < 10)
      lcd.print("000");
    if (myDay >= 10 and myDay < 100)
      lcd.print("00");
    if (myDay >= 100 and myDay < 1000)
      lcd.print("0");
    lcd.print(myDay);
    printOut = false;
  }
}