//=====LCD=====
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
unsigned long lcdInterval = 1000;   // Time between LCD Updates.
unsigned long lcdUpdateTime;        // Last LCD Update time.
unsigned long currentTime;          // current time. 
long counter;                       // counter.
long seconds, minutes, hours;       // seconds, minutes, hours since turn on.

void setup() {
  // put your setup code here, to run once:
  lcd.init();
  lcd.backlight();

  // initialize timing
  counter = 0;
  seconds = 0;
  minutes = 0;
  hours = 0;

  lcdUpdateTime = millis();
}

void loop() {
  // put your main code here, to run repeatedly:
  currentTime = millis();

  if (currentTime - lcdUpdateTime > lcdInterval) {
    // update lcd
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("LCD Test: ");

    hours = counter / 3600;
    minutes = (counter % 3600) / 60;
    seconds = counter % 60;
    lcd.setCursor(0, 2);
    lcd.print(timestamp());

    lcdUpdateTime = currentTime;
    counter++;
  }

  delay(20);
}

String timestamp() {
  String result = "";

  // hour
  if (hours < 10) {
    result += "0";
  }
  result += String(hours) + ":";

  // minute
  if (minutes < 10) {
    result += "0";
  }
  result += String(minutes) + ":";

  // second
  if (seconds < 10) {
    result += "0";
  }
  result += String(seconds);

  return result;
}
