#include <Keypad.h>
// #include <Tone.h>
const int ROW_NUM = 4; // Four rows
const int COLUMN_NUM = 3; // Three columns
#define buzzer 10
// Define the keypad matrix
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; // Connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
// Tone speaker;
const String correctPassword = "1234";
String enteredPassword = "";
const int relayPin = 2;
void setup() {
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
pinMode(buzzer,OUTPUT);
}
void loop() {
char key = keypad.getKey();
if (key) {
tone(buzzer, 200,100); // Play a tone when a key is pressed
Serial.print(key); // Print the key value to serial monitor
if (key == '#') {
if (enteredPassword == correctPassword) {
Serial.println(" - Password correct");
tone(buzzer,400,500);
digitalWrite(relayPin, HIGH); // Trigger the relay
} else {
Serial.println(" - Password incorrect");
tone(buzzer,800,500);
}
enteredPassword = ""; // Reset the entered password
} else {
enteredPassword += key;
}
}
}