#include <IRremote.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <TimeLib.h>

#define relay1 0
#define relay2 19
#define relay3 18
#define relay4 5
#define recv 4

#define pushbutton1 23
#define pushbutton2 15
#define pushbutton3 2
#define pushbutton4 16

#define tactSwitch1 14
#define tactSwitch2 13
#define tactSwitch3 12
#define tactSwitch4 35

IRrecv recv_pin(recv);
LiquidCrystal_I2C lcd(0x27, 20, 4);

bool led1State = false;
bool led2State = false;
bool led3State = false;
bool led4State = false;

tmElements_t onTime1, offTime1;
tmElements_t onTime2, offTime2;
tmElements_t onTime3, offTime3;
tmElements_t onTime4, offTime4;

void setup() {
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);

  pinMode(pushbutton1, INPUT_PULLUP);
  pinMode(pushbutton2, INPUT_PULLUP);
  pinMode(pushbutton3, INPUT_PULLUP);
  pinMode(pushbutton4, INPUT_PULLUP);

  pinMode(tactSwitch1, INPUT_PULLUP);
  pinMode(tactSwitch2, INPUT_PULLUP);
  pinMode(tactSwitch3, INPUT_PULLUP);
  pinMode(tactSwitch4, INPUT_PULLUP);

  lcd.init();
  lcd.clear();
  lcd.backlight();
  recv_pin.enableIRIn();
  recv_pin.blink13(true);

  setTime(12, 0, 0, 1, 1, 2023); // Set initial time (12:00:00, Jan 1, 2023)
}

void loop() {
  lcd.setCursor(0, 0);
  lcd.print("Status:");
  lcd.setCursor(8, 0);
  lcd.print("LED1 ");
  lcd.setCursor(8, 1);
  lcd.print("LED2 ");
  lcd.setCursor(8, 2);
  lcd.print("LED3 ");
  lcd.setCursor(8, 3);
  lcd.print("LED4 ");

  // IR remote control logic (unchanged)

  // Check tactSwitch1 state for setting time
  if (digitalRead(tactSwitch1) == LOW) {
    setOnOffTime(1);
    delay(500);
  }

  // Check tactSwitch2 state for setting time
  if (digitalRead(tactSwitch2) == LOW) {
    setOnOffTime(2);
    delay(500);
  }

  // Check tactSwitch3 state for setting time
  if (digitalRead(tactSwitch3) == LOW) {
    setOnOffTime(3);
    delay(500);
  }

  // Check tactSwitch4 state for setting time
  if (digitalRead(tactSwitch4) == LOW) {
    setOnOffTime(4);
    delay(500);
  }

  // Push button control logic (unchanged)
}

void setOnOffTime(int ledNumber) {
  tmElements_t now;
  breakTime(now, now.second());

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Set On/Off Time");
  lcd.setCursor(0, 2);
  lcd.print("Press again to save");

  while (digitalRead(tactSwitch1) == HIGH && digitalRead(tactSwitch2) == HIGH &&
         digitalRead(tactSwitch3) == HIGH && digitalRead(tactSwitch4) == HIGH) {
    lcd.setCursor(0, 1);
    lcd.print("HH:MM");
    lcd.setCursor(0, 3);
    lcd.print("DD:MM:YYYY");

    switch (ledNumber) {
      case 1:
        lcd.setCursor(8, 1);
        lcd.print("LED1 ");
        break;
      case 2:
        lcd.setCursor(8, 1);
        lcd.print("LED2 ");
        break;
      case 3:
        lcd.setCursor(8, 1);
        lcd.print("LED3 ");
        break;
      case 4:
        lcd.setCursor(8, 1);
        lcd.print("LED4 ");
        break;
    }

    lcd.setCursor(13, 1);
    lcd.print(now.Hour);
    lcd.print(':');
    lcd.print(now.Minute);

    lcd.setCursor(13, 3);
    lcd.print(now.Day);
    lcd.print(':');
    lcd.print(now.Month);
    lcd.print(':');
    lcd.print(year(now));

    delay(100);
  }

  // Set On Time
  lcd.setCursor(0, 1);
  lcd.print("Set On Time    ");
  lcd.setCursor(0, 3);
  lcd.print("HH:MM");

  setClockTime(now);

  while (digitalRead(tactSwitch1) == HIGH && digitalRead(tactSwitch2) == HIGH &&
         digitalRead(tactSwitch3) == HIGH && digitalRead(tactSwitch4) == HIGH) {
    lcd.setCursor(0, 2);
    lcd.print(now.Day);
    lcd.print(':');
    lcd.print(now.Month);
    lcd.print(':');
    lcd.print(year(now));

    lcd.setCursor(13, 2);
    lcd.print(now.Hour);
    lcd.print(':');
    lcd.print(now.Minute);

    delay(100);
    now = now + 60; // add one minute
  }

  onTime1 = now; // assuming LED1 for this example

  // Set Off Time
  lcd.setCursor(0, 1);
  lcd.print("Set Off Time   ");
  lcd.setCursor(0, 3);
  lcd.print("HH:MM");

  setClockTime(now);

  while (digitalRead(tactSwitch1) == HIGH && digitalRead(tactSwitch2) == HIGH &&
         digitalRead(tactSwitch3) == HIGH && digitalRead(tactSwitch4) == HIGH) {
    lcd.setCursor(0, 2);
    lcd.print(now.Day);
    lcd.print(':');
    lcd.print(now.Month);
    lcd.print(':');
    lcd.print(year(now));

    lcd.setCursor(13, 2);
    lcd.print(now.Hour);
    lcd.print(':');
    lcd.print(now.Minute);

    delay(100);
    now = now + 60; // add one minute
  }

  offTime1 = now; // assuming LED1 for this example

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("On/Off Time Set");
  delay(1000);
  lcd.clear();
}

void setClockTime(tmElements_t &time) {
  time.Hour = 12;  // default value for hours
  time.Minute = 0; // default value for minutes
  time.Second = 0; // default value for seconds
  time.Day = 1;    // default value for day
  time.Month = 1;  // default value for month
  time.Year = 2023; // default value for year
}
$abcdeabcde151015202530fghijfghij
$abcdeabcde151015202530fghijfghij
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
GND5VSDASCLSQWRTCDS1307+