#include <Keypad.h>
#include <Servo.h>
const int ROWS = 4; // Number of rows in the keypad
const int COLS = 4; // Number of columns in the keypad
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {6, 7, 8, 9}; // Connect to R1, R2, R3, R4 on the keypad
byte colPins[COLS] = {2, 3, 4, 5}; // Connect to C1, C2, C3, C4 on the keypad
const char* correctPassword = "123A"; // Define your correct password here
Servo servo; // Create a servo object
const int servoPin = 10; // Connect to the signal pin of the servo
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
servo.attach(servoPin); // Attach the servo to its pin
servo.write(90); // Initialize the servo to the center position
Serial.begin(9600);
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
Serial.println(key); // Print the pressed key to the Serial Monitor
// Check if the entered key is part of the password
static int position = 0;
if (key == correctPassword[position]) {
position++;
if (position == strlen(correctPassword)) {
// Password is correct, run the servo
Serial.println("Password matched!");
servo.write(0); // Rotate the servo to one position
delay(1000); // Wait for 1 second
servo.write(90); // Return the servo to the center position
position = 0; // Reset the position for the next attempt
}
} else {
position = 0; // Reset the position if a wrong key is pressed
}
}
}