#include <Wire.h>
#include <LiquidCrystal_I2C.h> // Include the library for I2C LCD
#include <Keypad.h> // Include the library for Keypad
// Define the I2C address for the LCD module
#define I2C_ADDR 0x27 // Adjusted address for your module
// Define the dimensions of the LCD (number of columns and rows)
#define LCD_COLS 16
#define LCD_ROWS 2
// Create an instance of the LiquidCrystal_I2C class
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLS, LCD_ROWS);
// Define the keypad layout
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 colPins[COLS] = {5, 4, 3, 2}; // Connect to the column pinouts of the keypad
byte rowPins[ROWS] = {9, 8, 7, 6}; // Connect to the row pinouts of the keypad
// Create an instance of Keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
// Initialize LCD
lcd.init();
lcd.backlight();
// Print initial message on LCD
lcd.print("Arduino LCD Keypad");
// Set up serial communication for debugging
Serial.begin(9600);
}
void loop() {
// Read keypad
char key = keypad.getKey();
// If a key is pressed
if (key) {
Serial.println(key); // Print the key to serial monitor
char password[] = "1234"; // Change this to your desired password
char enteredPassword[5]; // Array to store entered password (max length + 1 for null terminator)
int passwordIndex = 0; // Index to track current position in enteredPassword array
// Create an instance of Keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
// Initialize LCD
lcd.init();
lcd.backlight();
// Print initial message on LCD
lcd.print("Enter password:");
// Set up serial communication for debugging
Serial.begin(9600);
}
void loop() {
// Read keypad
char key = keypad.getKey();
// If a key is pressed
if (key) {
if (key == '#') { // Check if Enter key is pressed
// Compare entered password with predefined password
if (strcmp(enteredPassword, password) == 0) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access granted");
delay(2000); // Delay to show "Access granted" message
lcd.clear();
lcd.print("Welcome!");
// Add your code to perform actions after successful authentication
} else {
lcd.clear();
lcd.print("Wrong password");
delay(2000); // Delay to show "Wrong password" message
lcd.clear();
lcd.print("Enter password:");
}
// Reset entered password for next attempt
memset(enteredPassword, 0, sizeof(enteredPassword));
passwordIndex = 0;
} else if (passwordIndex < sizeof(enteredPassword) - 1) { // Check if there's space to store more characters
// Append pressed key to enteredPassword
enteredPassword[passwordIndex++] = key;
lcd.setCursor(passwordIndex - 1, 1);
lcd.print('*'); // Display '*' on LCD for each entered character
}
}
}
}
}