#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
uint8_t clock[8] = {0x0, 0xe, 0x15, 0x17, 0x11, 0xe, 0x0};
const int switch_on = 2, input_off_time = 3, output = 9;
int off_timer_start = 0, offtime = 0;
void setup() {
pinMode(switch_on, INPUT);
pinMode(input_off_time, INPUT);
pinMode(output, OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(6, 0);
lcd.print("OFF ");
lcd.createChar(2, clock);
lcd.setCursor(0, 1);
lcd.write(2);
lcd.print((String)" " + offtime + " ");
}
void loop() {
int x = ((millis() / 1000) - off_timer_start)/60;
if (x <= offtime && digitalRead(output) == HIGH) {
int m = ((offtime * 60) + off_timer_start - (millis() / 1000))/60;
int s = ((offtime * 60) + off_timer_start - (millis() / 1000))%60;
lcd.setCursor(10, 0);
lcd.print((String)" " + m + ":" + s +" ");
}
if (digitalRead(switch_on) == HIGH) {
digitalWrite(output, HIGH);
off_timer_start = millis() / 1000;
lcd.setCursor(6, 0);
lcd.print("ON ");
} else if (x >= offtime && digitalRead(output == HIGH)) {
digitalWrite(output, LOW);
lcd.setCursor(6, 0);
lcd.print("OFF ");
}
while (digitalRead(input_off_time) == HIGH) {
if (offtime <= 29) { //change max time limit
offtime += 1; //change increment by x
} else {
offtime = 0;
}
lcd.setCursor(0, 1);
lcd.write(2);
lcd.print((String)" " + offtime + " ");
delay(500);// adjust speed of incrementing
}
}