Switch OFF timer with time adjust and LCD display

This code is the same as above, but with few additional options to change the off time, LCD display to show the off time, the present state of the device, and remaining time to OFF

Here the circuit has two input button switches, one is connected to Pin 2 which is to switch ON the device, and the second button connected to pin3 to set the OFF-time value; in the above code the off time needs to be declared at first and once the sketch is uploaded then it needs to re-upload again to change the off-time value. Here that disadvantage can be overcome by adding input to change the OFF time.

In the code as long as the pin3 is held ON, the off-time increments by 1 and repeats again from 0 once it reaches the value of 30 minutes. The limit of time, increment value, speed of increment can be modified by adjusting the values in the code; it is specified in the corresponding lines in the code.

#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
  }
}
Previous
Previous

Arduino ON and OFF timer

Next
Next

Timer switch with Relay and LCD Arduino code