#include <Keypad.h>
#include <Servo.h>
Servo myServo;
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] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8, 9};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const String correctCode = "0000";
String inputCode = "";
void setup()
{
myServo.attach(10);
myServo.write(0);
Serial.begin(9600);
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.println(key);
if (key == '#')
{
if (inputCode == correctCode)
{
unlockDoor();
}
else
{
Serial.println("Wrong code");
inputCode = "";
}
}
else if (key == '*')
{
inputCode = "";
}
else
{
inputCode += key;
}
}
}
void unlockDoor()
{
Serial.println("Correct code. Door unlocked.");
myServo.write(90);
delay(5000);
myServo.write(0);
}