#include <Keypad.h>
#include <LiquidCrystal.h>
// Keypad configuration
const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; // connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; // connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// LCD configuration
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
char input = '\0'; // to store the input from the keypad
void setup() {
// Initialize the LCD
lcd.begin(16, 2);
lcd.print("Select Relay:");
lcd.setCursor(0, 1); // move the cursor to the second row
// Initialize relay pins
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
// Ensure all relays are off initially
digitalWrite(A1, LOW);
digitalWrite(A2, LOW);
digitalWrite(A3, LOW);
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '#') {
// Enter key pressed
lcd.clear();
lcd.print("Selected:");
lcd.setCursor(0, 1);
lcd.print(input);
controlRelay(input);
delay(2000); // wait for 2 seconds
lcd.clear();
lcd.print("Select Relay:");
lcd.setCursor(0, 1);
input = '\0'; // clear the input after displaying
}
else if (key == '*') {
// Backspace key pressed
if (input != '\0') {
input = '\0'; // clear the input
lcd.clear();
lcd.print("Select Relay:");
lcd.setCursor(0, 1);
}
}
else if (key == 'A' || key == 'B' || key == 'C') {
// Option key pressed
input = key;
lcd.setCursor(0, 1);
lcd.print(input);
}
}
}
void controlRelay(char selection) {
// Turn off all relays initially
digitalWrite(A1, LOW);
digitalWrite(A2, LOW);
digitalWrite(A3, LOW);
// Turn on the selected relay
switch (selection) {
case 'A':
digitalWrite(A1, HIGH);
break;
case 'B':
digitalWrite(A2, HIGH);
break;
case 'C':
digitalWrite(A3, HIGH);
break;
default:
// Do nothing if the selection is invalid
break;
}
}