#include <Keypad.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
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 myservo;
int pos = 0; // variable to store the servo position
String message = "";
void setup() {
myservo.attach(11);
myservo.write(0);
Serial.begin(9600);
// Init
lcd.init();
lcd.backlight();
showStartupMessage();
initializeLcd();
}
void showStartupMessage() {
lcd.setCursor(6, 0);
lcd.print("Welcome!");
delay(1000);
lcd.setCursor(2, 2);
String message = "Door lock system";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(100);
}
delay(500);
}
void openDoor() {
lcd.setCursor(4, 2);
lcd.print("Opening Door");
for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
void closeDoor(){
lcd.setCursor(4, 2);
lcd.print("Closing Door");
for (pos = 90; pos >= 0; pos -= 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
void initializeLcd() {
lcd.clear();
lcd.cursor();
lcd.setCursor(2, 1);
lcd.print("Enter Password");
message = "";
lcd.setCursor(2, 2);
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
Serial.println(key);
// opening door
switch (key) {
case 'C':
initializeLcd();
return;
case 'D':
lcd.clear();
lcd.setCursor(5, 1);
if (message.compareTo("1234") == 0) {
lcd.print("Correct");
openDoor();
delay(3000);
closeDoor();
} else {
lcd.print("Incorrect");
}
return;
default:
lcd.print("*");
message = message + key;
Serial.print(message);
return;
}
if (key == '2') {
}
// closing door
if (key == '3') {
}
}
//myservo.write(0);
}