// Simple time display on LCD by Kidkara.
// LCD displays the day of the week and time in 24-hour format.
// There is no automatic time setting.
// To display realtime, this sketch must be ran at exactly 00:00:00 Hour-Sunday.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

unsigned long curmil;
unsigned long prevmil;
const unsigned long duration = 1000;
int sec = 0;
int min = 0;
int hour = 0;
int day = 0;

char weekdays[7][10] = {"Sunday   ", "Monday   ", "Tuesday  ", "Wednesday", "Thursday ", "Friday   ", "Saturday "};


void setup() {
  lcd.init();
  lcd.backlight();
  lcd.clear();
}


void loop()
{
  curmil = millis();
  if (curmil - prevmil >= duration)
  {
    sec = ++sec;
    if (sec == 60)
    {
      min = ++min;
      sec = 0;
    }

    if (min == 60)
    {
      hour = ++hour;
      min = 0;
    }

    if (hour >= 24)
    {
      day = ++day;
      hour = 0;
    }

    if (day > 6 )
    {
      day = 0;
    }

    lcd.setCursor(0, 0);
    lcd.print(weekdays[day]);

    if (hour < 10)
    {
      lcd.setCursor(0, 1);
      lcd.print("0");
      lcd.print(hour);
      lcd.print(":");
    }

    else
    {
      lcd.print(hour);
      lcd.print(":");
    }

    if (min < 10)
    {
      lcd.print("0");
      lcd.print(min);
      lcd.print(":");
    }

    else
    {
      lcd.print(min);
      lcd.print(":");
    }

    if (sec < 10)
    {
      lcd.print("0");
      lcd.print(sec);
    }

    else
    {
      lcd.print(sec);
    }

    prevmil = curmil;
  }

}