#include <Keypad.h> // Include the keypad library
#include <Servo.h> // Include the servo library
// Define the keypad configuration
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
// Define the keys on the keypad
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Connect the keypad rows and columns to Arduino pins
byte rowPins[ROWS] = {2, 3, 4, 5}; // Rows connected to pins 2-5
byte colPins[COLS] = {6, 7, 8, 9}; // Columns connected to pins 6-9
// Create the Keypad object
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Define the password
const char password[] = "1234"; // Set your desired password
char inputPassword[5]; // Store the entered password (4 digits + null terminator)
byte passwordIndex = 0; // Track the current input position
// Create a Servo object
Servo myServo;
// Define servo parameters
const int servoPin = 10; // Servo signal pin
const int openAngle = 90; // Angle to rotate when the password is correct
const int closeAngle = 0; // Default (closed) position
void setup() {
// Attach the servo to the specified pin
myServo.attach(servoPin);
// Initialize the servo to the closed position
myServo.write(closeAngle);
// Start Serial Monitor for debugging
Serial.begin(9600);
}
void loop() {
// Get the key pressed on the keypad
char key = keypad.getKey();
if (key) { // If a key is pressed
Serial.print("Key pressed: ");
Serial.println(key);
if (key == '#') { // '#' indicates the user is done entering the password
inputPassword[passwordIndex] = '\0'; // Null-terminate the input
if (strcmp(inputPassword, password) == 0) { // Compare input to stored password
Serial.println("Correct Password!");
rotateServo(); // Rotate servo to open position
} else {
Serial.println("Incorrect Password!");
resetServo(); // Reset servo to closed position
}
passwordIndex = 0; // Reset input index for the next attempt
} else if (passwordIndex < 4 && key != '*' && key != '#') {
// Only allow up to 4 characters, ignore '*' and '#' as input
inputPassword[passwordIndex] = key; // Store the key
passwordIndex++;
}
}
}
// Function to rotate the servo to the open position
void rotateServo() {
myServo.write(openAngle); // Rotate servo to the specified angle
delay(2000); // Hold the position for 2 seconds
resetServo(); // Return the servo to the closed position
}
// Function to reset the servo to the closed position
void resetServo() {
myServo.write(closeAngle); // Reset servo to the closed position
}

