#define BUZZER_PIN 3 // GPIO pin connected to the buzzer
#include <Keypad.h>
//#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd_1(0x27, 16, 2);
const byte ROW_NUM = 4; // Number of rows in the keypad
const byte COL_NUM = 4; // Number of columns in the keypad
// Define the keymap for your keypad
char keys[ROW_NUM][COL_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Define the row and column pin numbers for the keypad
byte rowPins[ROW_NUM] = {6, 7, 8, 9}; // Pin numbers connected to the keypad rows
byte colPins[COL_NUM] = {10, 11, 12, 13}; // Pin numbers connected to the keypad columns
// Create an instance of the Keypad library
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROW_NUM, COL_NUM);
void setup() {
pinMode(4,OUTPUT);
pinMode(2,OUTPUT);
Serial.begin(9600);
lcd_1.begin(16, 2);
lcd_1.backlight();
pinMode(BUZZER_PIN, OUTPUT);
// Display a welcome message
lcd_1.setCursor(0, 0);
lcd_1.print("Enter key:");
// Initialize the 7-segment display pins as output
}
int i=0;
int z;
String pass="";
void loop() {
String pass = ""; // Reset password each time the loop starts
int i = 0; // Cursor position on the LCD for this password attempt
while (true) {
char key = keypad.getKey(); // Read the pressed key from the keypad
digitalWrite(2, LOW);
if (key) {
lcd_1.setCursor(i, 1);
lcd_1.print(key);
i++; // Move to the next position for the next character
pass += key; // Append the key to the password
}
// Display the entered password so far
// Check if the password has been entered fully
if (pass.length() == 6) {
if (pass == "ABCD12") {
// Correct password actions
digitalWrite(4, HIGH);
lcd_1.clear();
lcd_1.setCursor(0, 0);
lcd_1.print("CORRECT");
lcd_1.setCursor(0, 1);
lcd_1.print("key");
tone(BUZZER_PIN, 500); // Play a 500 Hz tone on the buzzer
delay(500); // Wait for 0.5 seconds
noTone(BUZZER_PIN); // Stop the tone
delay(500);
// Stop the loop to prevent further code execution after the correct password
while (true); // Infinite loop to halt further execution
} else {
// Incorrect password actions
digitalWrite(2, HIGH);
lcd_1.clear();
lcd_1.setCursor(0, 0);
lcd_1.print("WRONG");
lcd_1.setCursor(0, 1);
lcd_1.print("key");
tone(BUZZER_PIN, 1000); // Play a higher tone for incorrect password
delay(500);
noTone(BUZZER_PIN);
delay(500);
// Clear LCD and reset for another attempt
lcd_1.clear();
lcd_1.print("Enter key:");
pass = ""; // Reset password
i = 0; // Reset LCD cursor position
}
}
}
}