#include <IRremote.h>
#include <LiquidCrystal_I2C.h>
#define RELAY_1 0
#define RELAY_2 19
#define RELAY_3 18
#define RELAY_4 5
#define IR_RECEIVER_PIN 4
#define PUSHBUTTON_1 23
#define PUSHBUTTON_2 15
#define PUSHBUTTON_3 2
#define PUSHBUTTON_4 16
IRrecv recv_pin(IR_RECEIVER_PIN);
LiquidCrystal_I2C lcd(0x27, 20, 4);
bool led1State = false;
bool led2State = false;
bool led3State = false;
bool led4State = false;
void setup() {
pinMode(RELAY_1, OUTPUT);
pinMode(RELAY_2, OUTPUT);
pinMode(RELAY_3, OUTPUT);
pinMode(RELAY_4, OUTPUT);
pinMode(PUSHBUTTON_1, INPUT_PULLUP);
pinMode(PUSHBUTTON_2, INPUT_PULLUP);
pinMode(PUSHBUTTON_3, INPUT_PULLUP);
pinMode(PUSHBUTTON_4, INPUT_PULLUP);
lcd.init();
lcd.clear();
lcd.backlight();
recv_pin.enableIRIn();
recv_pin.blink13(true);
}
void handleButtonPress(int buttonIndex) {
bool& ledState = (buttonIndex == 0) ? led1State : ((buttonIndex == 1) ? led2State : ((buttonIndex == 2) ? led3State : led4State));
int relayPin = (buttonIndex == 0) ? RELAY_1 : ((buttonIndex == 1) ? RELAY_2 : ((buttonIndex == 2) ? RELAY_3 : RELAY_4));
ledState = !ledState;
digitalWrite(relayPin, ledState ? HIGH : LOW);
lcd.setCursor(13, buttonIndex);
lcd.print(ledState ? "ON " : "OFF");
delay(500); // Add a small delay to debounce the button
}
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 ");
if (recv_pin.decode()) {
String response = String(recv_pin.decodedIRData.command);
if (response == "48") { // NUM 1 BUTTON
handleButtonPress(0);
} else if (response == "24") { //NUM 2 BUTTON
handleButtonPress(1);
} else if (response == "122") { // NUM 3 BUTTON
handleButtonPress(2);
} else if (response == "16") { // NUM 4 BUTTON
handleButtonPress(3);
} else if (response == "162") { //POWER OFF BUTTON
for (int i = 0; i < 4; i++) {
bool& ledState = (i == 0) ? led1State : ((i == 1) ? led2State : ((i == 2) ? led3State : led4State));
int relayPin = (i == 0) ? RELAY_1 : ((i == 1) ? RELAY_2 : ((i == 2) ? RELAY_3 : RELAY_4));
ledState = false;
digitalWrite(relayPin, LOW);
lcd.setCursor(13, i);
lcd.print("OFF ");
}
} else if (response == "34") { //TEST BUTTON
for (int i = 0; i < 4; i++) {
handleButtonPress(i);
}
}
recv_pin.resume();
}
// Check pushbutton state
for (int i = 0; i < 4; i++) {
if (digitalRead(PUSHBUTTON_1 + i) == LOW) {
handleButtonPress(i);
}
}
}