#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// Pin setup for Keypad
const byte ROWS = 4, COLS = 4;
const byte rowsPins[ROWS] = {9, 8, 7, 6};
const byte colsPins[COLS] = {5, 4, 3, 2};
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Initialize keypad
Keypad keypad = Keypad(makeKeymap(keys), rowsPins, colsPins, ROWS, COLS);
// String to store the key sequence
String kod = "";
// LED control flags
bool c1 = false, c2 = false, c3 = false;
// Initialize LCD with I2C address (0x27 is the default address for many LCD modules)
LiquidCrystal_I2C lcd(0x27, 16, 2); // 16 columns, 2 rows
void setup() {
Serial.begin(9600);
// Set LED pins as output
for (int led = 11; led < 14; led++) {
pinMode(led, OUTPUT);
digitalWrite(led, LOW); // Ensure LEDs are initially off
}
// Initialize the LCD
lcd.begin(16, 2); // Set the dimensions of the LCD
lcd.clear(); // Clear any previous text on the LCD
lcd.setCursor(0, 0); // Move cursor to the top-left
lcd.print("Enter Code:"); // Display prompt on the first row
delay(2000); // Delay to show the initial prompt for 2 seconds
}
void loop() {
char key = keypad.getKey();
if (key) {
kod += key; // Append the pressed key to the sequence
Serial.println(kod); // Output the current key sequence to Serial Monitor
// Display the input code on the LCD (second row)
lcd.setCursor(0, 1); // Move the cursor to the second row
lcd.print(kod); // Print the current key sequence
// Check key sequences and control LEDs
if (kod == "147") {
c1 = !c1; // Toggle LED1
digitalWrite(11, c1);
updateLCD(); // Update LCD to show the status of LEDs
}
if (kod == "123") {
c2 = !c2; // Toggle LED2
digitalWrite(12, c2);
updateLCD(); // Update LCD to show the status of LEDs
}
if (kod == "369") {
c3 = !c3; // Toggle LED3
digitalWrite(13, c3);
updateLCD(); // Update LCD to show the status of LEDs
}
if (kod == "333") {
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
c1 = c2 = c3 = true; // All LEDs on
updateLCD(); // Update LCD to show the status of LEDs
}
if (kod == "999") {
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
c1 = c2 = c3 = false; // All LEDs off
updateLCD(); // Update LCD to show the status of LEDs
}
// Reset the code after 3 or more characters
if (kod.length() >= 3) {
kod = ""; // Clear the input code
lcd.setCursor(0, 1); // Move cursor to the second row
lcd.print(" "); // Clear the second row
lcd.setCursor(0, 0); // Move cursor to the first row
lcd.print("Enter Code:"); // Prompt again
}
}
}
// Function to update the LCD with the current LED statuses
void updateLCD() {
lcd.setCursor(0, 0); // Set the cursor to the first row
lcd.print("LED1:");
lcd.print(c1 ? "YONIB TURIBDI " : "OCHGAN"); // Display LED1 status
lcd.setCursor(8, 0); // Move the cursor to the middle of the first row
lcd.print("LED2:");
lcd.print(c2 ? "YONIB TURIBDI " : "OCHGAN"); // Display LED2 status
lcd.setCursor(0, 1); // Move the cursor to the second row
lcd.print("LED3:");
lcd.print(c3 ? "YONIB TURIBDI " : "OCHGAN"); // Display LED3 status
}