#include <Keypad.h>
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 rowPins[ROWS] = {9, 8, 7, 6}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const int relayPin = 13; // Connect the relay to pin 10
// Define your passcode
const char passcode[] = "1234"; // Change this to your desired passcode
char enteredCode[5]; // Buffer to store entered code
void setup() {
pinMode(relayPin, OUTPUT); // Set the relay pin as output
Serial.begin(9600); // Initialize serial communication for debugging
Serial.println("Enter Passcode:"); // Display initial prompt
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '#') { // If # key is pressed, check the passcode
if (strlen(enteredCode) == 4) { // Check if passcode length is 4
enteredCode[4] = '\0'; // Null terminate the string
if (strcmp(enteredCode, passcode) == 0) {
// If the entered passcode matches the stored passcode, activate the relay
digitalWrite(relayPin, HIGH);
Serial.println("\n\n*****Access Granted*****\n");
delay(2000); // Delay to keep the relay activated for a short duration
digitalWrite(relayPin, LOW); // Deactivate the relay after delay
} else {
Serial.println("\n\n*****Access Denied*****\n");
}
memset(enteredCode, 0, sizeof(enteredCode)); // Clear the entered code buffer
} else {
Serial.println("\n\n*****Invalid Passcode*****\n");
memset(enteredCode, 0, sizeof(enteredCode)); // Clear the entered code buffer
}
Serial.println("Enter Passcode:"); // Display prompt for next passcode entry
} else if (strlen(enteredCode) < 4) { // If less than 4 digits have been entered
Serial.print(key); // Print the pressed key
enteredCode[strlen(enteredCode)] = key; // Store the pressed key
}
}
}