#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define Password_Lenght 7
LiquidCrystal_I2C lcd(0x27, 16, 2);
char Data[Password_Lenght]; // 6 is the number of chars it can hold + the null char = 7
char Master[Password_Lenght] = "080808";
byte data_count = 0;
bool Pass_is_good;
char customKey;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', '*'},
{'C', '0', '#', '/'} // '#' acts as Enter key
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad customKeypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); // initialize an instance of class NewKeypad
// Define LED pins
const int greenLED = 12;
const int redLED = 13;
void setup() {
lcd.init(); // initialize the lcd
lcd.backlight();
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
digitalWrite(greenLED, LOW);
digitalWrite(redLED, LOW);
}
void loop() {
lcd.setCursor(0, 0);
lcd.print("Enter Password");
customKey = customKeypad.getKey();
if (customKey) {
if (customKey == '#') { // If Enter ('#') is pressed, check the password
lcd.clear();
lcd.setCursor(0, 0);
if (!strcmp(Data, Master)) {
lcd.print("Selamat Datang");
digitalWrite(greenLED, HIGH); // Turn on green LED
digitalWrite(redLED, LOW); // Turn off red LED
} else {
lcd.print("Password Salah");
digitalWrite(redLED, HIGH); // Turn on red LED
digitalWrite(greenLED, LOW); // Turn off green LED
}
delay(5000); // Wait 5 seconds before resetting
lcd.clear();
clearData();
digitalWrite(greenLED, LOW); // Reset LEDs
digitalWrite(redLED, LOW);
} else if (customKey == 'C') { // 'C' clears the input
lcd.clear();
clearData();
} else if (data_count < Password_Lenght - 1) { // Only add if within password limit
Data[data_count] = customKey;
lcd.setCursor(data_count, 1);
lcd.print(Data[data_count]);
data_count++;
}
}
}
void clearData() {
while (data_count != 0) {
Data[data_count--] = 0;
}
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the second line on the LCD
return;
}