#include <Keypad.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Servo.h>
#define ROW_NUM 4 // four rows
#define COLUMN_NUM 4 // four columns
char key = '\0'; // null key
String input_password = "";
String user_password = "6969";
String door_state = "open";
String prev_door_state = "open";
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
Servo myServo;
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[ROW_NUM] = {7, 6, 5, 4}; // GIOP19, GIOP18, GIOP5, GIOP17 connect to the row pins
byte pin_column[COLUMN_NUM] = {3, 2, 8, 9}; // GIOP16, GIOP4, GIOP0, GIOP2 connect to the column pins
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
void setup() {
Serial.begin(9600);
myServo.attach(13);
myServo.write(100);
delay(3000);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("SSD1306 allocation failed");
for (;;);
}
display_message("Welcome to your personal lock services");
}
void loop() {
door_check();
delay(1000);
}
void display_message(String message){
display.setTextSize(1);
display.setTextColor(WHITE);
display.clearDisplay();
display.setCursor(10, 20);
display.print(message);
display.display();
}
void check_password() {
delay(3000);
display_message("Enter Password. Press # when password entered : ");
while (true) {
key = keypad.getKey();
if (key) {
if (key == '#') {
if (input_password == user_password) {
if(prev_door_state == "open"){
door_state = "close";
display_message("closing");
}
else if(prev_door_state == "close"){
door_state = "open";
display_message("opening");
}
prev_door_state = door_state;
input_password = "";
break;
}
else {
display_message("Wrong password try again");
input_password = "";
}
}
input_password.concat(key);
display.setTextSize(1);
display.setTextColor(WHITE);
display.print("*");
display.print(" ");
display.display();
}
}
}
void door_check(){
check_password();
delay(1000);
if(door_state == "open"){
myServo.write(0);
delay(100);
}
else if(door_state == "close"){
myServo.write(180);
delay(100);
}
delay(1000);
}