#include <Servo.h> // include servo library
#include <Keypad.h> // include keypad library
char key1='B'; // store ASCII code of character entered from keypad
int x=0; // store actual character entered from keypad
String password = "1869"; // set password to open the door (can be changed as needed)
String Entered_password; // password entered by the user
Servo myservo; // declare servo object
const byte ROWS = 4; // number of Keypad rows
const byte COLS = 3; // number of Keypad columns
// Position array of keypad keys
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {10, 8, 7, 6}; // row pins of keypad
byte colPins[COLS] = {5, 4, 3}; // column pins of keypad
// declare keypad object
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600); // begin serial communication with buad rate 9600
myservo.attach(9); // attach servo to pin 9
}
void loop() {
do{
Entered_password = ""; // clear variable
Serial.println();
Serial.println("Enter password "); // print on Serial monitor
do{
key1 = customKeypad.getKey(); // get ASCII code of button pressed
if(key1 && key1!=35) // if any button pressed except '#' (35 is ASCII code for #)
{
x=key1-48; // store actual character
Entered_password = Entered_password + key1; // add current character to Entered_password
Serial.print(x); // print pressed button character on Serial monitor
}
}while(key1!=35); // While 35 in ASCII (which is # in actual character) is not pressed, keep control in the loop
Serial.println(); // provide a little gap before next entry
Entered_password = Entered_password.substring(0,5); // Take first 5 characters from entered number
if(Entered_password == password) // if entered password is same as preset password
{
Serial.println("DOOR OPENED "); // print on Serial monitor
myservo.write(0); // move servo to 0 degree
delay(10000); // delay for 10 seconds
myservo.write(90); // rotate servo to 90 degrees
}
else // if entered password is not same as preset password
{
Serial.println("INCORRECT PASSWORD, PLEASE TRY AGAIN"); // print on Serial monitor
}
}while(Entered_password != password); // keep control in do while loop until correct password is not entered
}