#include <Keypad.h>
const char* correctPassword = "1234";
char enteredPassword[5];
int position = 0;
const int relayPin = 13; // Pin for Relay
const int buzzerPin = 12; // Pin for Buzzer
// Define the Keypad
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] = {5, 18, 19, 21}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {22, 23, 25, 26}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
digitalWrite(relayPin, LOW); // Ensure relay is off initially
digitalWrite(buzzerPin, LOW);
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.print("Key Pressed: ");
Serial.println(key); // Print the key to Serial Monitor
if (key == '#') {
enteredPassword[position] = '\0'; // Null-terminate the string
checkPassword();
position = 0; // Reset position for next input
}
else if (key == '*') {
// Reset the entered password
position = 0;
Serial.println("Password cleared!");
}
else {
enteredPassword[position] = key;
position++;
}
}
}
void checkPassword() {
if (strcmp(enteredPassword, correctPassword) == 0) {
Serial.println("Correct Password!");
digitalWrite(relayPin, HIGH); // Turn on the relay to power the LED
delay(10000); // Keep LED on for 10 seconds
digitalWrite(relayPin, LOW); // Turn off the relay after 10 seconds
} else {
Serial.println("Incorrect Password!");
tone(buzzerPin, 1000, 2000); // Buzzer on for 2 seconds
}
}