#include <Servo.h>
#include <LiquidCrystal.h>
#include <Keypad.h>
#define ROWS 4
#define COLS 4
#define RS_PIN 9
#define E_PIN 8
#define D4_PIN 7
#define D5_PIN 6
#define D6_PIN 5
#define D7_PIN 4
#define SERVO_PIN 3
#define PASS_LEN 8
const char password[PASS_LEN] = "14523069";
const uint8_t ROW_PINS[ROWS] = {A0, A1, A2, A3};
const uint8_t COL_PINS[COLS] = {13, 12, 11, 10};
const char keymap[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'},
};
Keypad keypad = Keypad(makeKeymap(keymap), ROW_PINS, COL_PINS, ROWS, COLS);
LiquidCrystal lcd = LiquidCrystal(RS_PIN, E_PIN, D4_PIN, D5_PIN, D6_PIN, D7_PIN);
Servo servo;
uint8_t i = 0;
char passBuf[PASS_LEN];
bool equalString(char *a, char *b, uint32_t len) {
for(int i = 0; i < len; i++)
if(a[i] != b[i]) return false;
return true;
}
void setup() {
servo.attach(SERVO_PIN);
servo.write(0);
lcd.begin(16, 2);
lcd.clear();
lcd.print("Password:");
lcd.setCursor(0, 1);
}
void loop() {
char key = keypad.getKey();
if(key != NO_KEY) {
lcd.print(key);
passBuf[i++] = key;
if(i == PASS_LEN) {
if(equalString(passBuf, password, PASS_LEN)) {
servo.write(90);
lcd.clear();
lcd.print("Welcome!");
} else {
lcd.clear();
lcd.print("Incorrect!!!");
delay(1000);
lcd.clear();
lcd.print("Password:");
lcd.setCursor(0, 1);
i = 0;
}
}
}
}