#include <Keypad.h>
#include <Servo.h>
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
const int openAngle = 90; // door open angle
const int closeAngle = 0;
int incorrect_attempts=0;
char password[4]={'1','9','8','2'};
int flag=0;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 5, 4, 3, 2 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
Servo lockServo;
void setup() {
// put your setup code here, to run once:
pinMode(10, OUTPUT);
lockServo.attach(11);
lockServo.write(closeAngle);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
//
// delay(1000); // Wait for 1000 millisecond(s)
// digitalWrite(10, LOW);
// delay(1000); // Wait for 1000 millisecond(s)
char typed[4];
for(int i=0;i<4;++i){
char key = keypad.waitForKey();
delay(1000);
typed[i]=key;
if (key != NO_KEY) {
Serial.print(key);
}
}
Serial.println();
for(int i=0;i<4;++i){
if(typed[i]!=password[i]){
Serial.println("ACCESS DENIED");
incorrect_attempts++;
flag=1;
break;
}
}
if(incorrect_attempts>=3){
Serial.println("LOCKOUT TRY AGAIN AFTER 5 SECONDS");
incorrect_attempts=0;
digitalWrite(10, HIGH);
delay(5000);
Serial.println("RETYPE PASSWORD");
}
if(flag==0){
Serial.println("ACCESS GRANTED");
lockServo.write(90);
delay(3000);
lockServo.write(0);
digitalWrite(10, LOW);
}
flag=0;
}