#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
//pins
long charger_pin = A0;
long button_pin = 7;
//time units
long second = 1000;
long minute = 60 * second;
long hour = 60 * minute;
//initial target
long target = hour;
int finished = 0;
void setup() {
lcd.init();
lcd.backlight();
lcd.print("PASSED: ");
lcd.setCursor(0,1);
lcd.print("REMAIN: ");
//pins
pinMode(charger_pin, OUTPUT);
digitalWrite(charger_pin, LOW); //turn on charger
pinMode(button_pin, INPUT_PULLUP);
}
void loop() {
if(!finished){
//check for button pressed
if(digitalRead(button_pin) == LOW){
target += hour;
}
//check if finished
long time_passed = millis();
if (time_passed > target){
finished = 1;
lcd.clear();
lcd.print(" " + millis_to_string(target));
lcd.setCursor(0,1);
lcd.print("CHARGING FINISH!");
digitalWrite(charger_pin, HIGH);
}else{
lcd.setCursor(8,0);
lcd.print(millis_to_string(time_passed));
lcd.setCursor(8,1);
lcd.print(millis_to_string(target - time_passed + 1000));
}
}
delay(1000);
}
String millis_to_string(long millis){
long time = round(millis / 1000);
long hours = time / 3600;
time = time - (hours * 3600);
long minutes = time / 60;
long seconds = time - (minutes * 60);
String hours2 = "0" + String(hours);
hours2 = hours2.substring(hours2.length() - 2);
String minutes2 = "0" + String(minutes);
minutes2 = minutes2.substring(minutes2.length() - 2);
String seconds2 = "0" + String(seconds);
seconds2 = seconds2.substring(seconds2.length() - 2);
return hours2 + ":" + minutes2 + ":" + seconds2;
}