#include <Keypad.h>
#include <Servo.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define SERVO_PIN A1
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Servo servo;
char keys[4][4] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[4] = {9, 8, 7, 6};
byte pin_column[4] = {5, 4, 3, 2};
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, 4, 4 );
const String password = "12345678";
String input_password;
int angle = 0;
unsigned long lastTime;
void setup() {
Serial.begin(9600);
input_password.reserve(32);
servo.attach(SERVO_PIN);
servo.write(0);
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS))
{
Serial.println(("Display ready"));
for(;;);
}
}
void loop()
{
char key = keypad.getKey();
if (key)
{
Serial.println(key);
if (key == '*')
{
input_password = "";
}
else if (key == '#')
{
if (input_password == password)
{
Serial.println("The password is correct");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,28);
display.println("OPEN");
display.display();
delay(1000);
display.clearDisplay();
servo.write(90);
delay(5000);
servo.write(0);
}
else
{
Serial.println("The password is incorrect, try again");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,28);
display.println("close");
display.display();
delay(2000);
display.clearDisplay();
}
input_password = "";
}
else
{
input_password += key;
}
}
}