#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
const byte ROWS = 4;
const byte COLS = 4;
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 };
byte colPins[COLS] = { 5, 4, 3, 2 };
Keypad customKeypad = Keypad(makeKeymap(Keys), rowPins, colPins, ROWS, COLS);
char customKey;
int number = 0;
int password = 1415;
int buzzer = 10; // Define buzzer pin
void setup() {
lcd.init();
lcd.setBacklight(HIGH);
pinMode(buzzer, OUTPUT);
}
void loop() {
lcd.setCursor(0, 0);
lcd.print("Welcome master");
customKey = customKeypad.getKey();
if (customKey) {
if (customKey >= '0' && customKey <= '9') {
number = number * 10 + (customKey - '0');
lcd.setCursor(0, 1);
lcd.print(number);
}
if (customKey == '#') {
if (number == password) {
lcd.setCursor(0, 1);
lcd.print("Access Accepted");
number = 0;
delay(800);
lcd.clear();
playMelody(); // Play melody on correct password
} else {
lcd.setCursor(0, 1);
lcd.print("Invalid Password");
number = 0;
delay(800);
lcd.clear();
}
}
if (customKey == '*') {
number = 0;
lcd.clear();
}
}
}
void playMelody() {
int melody[] = {262, 294, 330, 349, 392}; // Example notes (C4 to G4)
int noteDuration = 500; // Duration for each note
for (int i = 0; i < 5; i++) {
tone(buzzer, melody[i], noteDuration);
delay(noteDuration);
noTone(buzzer);
}
}