/* This code simulates a door lock system:
- Press 'A' to check if the entered password is correct.
- Press 'C' to clear input.
- Press 'D' to delete the last character entered.
** This project is developed from a project "https://wokwi.com/projects/408096861796816897" BY "bjane7171".
*/
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
#define I2C_ADD 0X27
#define LCD_COL 16
#define LCD_ROW 2
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
const uint8_t pinRows[ROWS] = {4, 5, 6, 7};
const uint8_t pinCols[COLS] = {8, 9, 10, 11};
LiquidCrystal_I2C lcd(I2C_ADD, LCD_COL, LCD_ROW);
Servo myservo;
Keypad mykey = Keypad(makeKeymap(keys), pinRows, pinCols, ROWS, COLS);
int i = 0;
String input = "";
const String passcode = "1234";
void grantAccess() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access Granted!!");
lcd.setCursor(0, 1);
lcd.print("Door Opening..");
myservo.write(90);
digitalWrite(12, HIGH);
delay(5000);
myservo.write(0);
lcd.setCursor(0, 1);
lcd.print("Door Closing..");
delay(3000);
digitalWrite(12, LOW);
resetSystem();
}
void denyAccess() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access Denied!");
digitalWrite(13, HIGH);
tone(2, 800);
delay(2000);
noTone(2);
digitalWrite(13, LOW);
resetSystem();
}
void clearInput() {
input = "";
updateDisplay();
}
void updateDisplay() {
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(input);
}
void resetSystem() {
input = "";
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Code:");
updateDisplay();
}
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Enter Code: ");
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(2, OUTPUT);
myservo.attach(3);
myservo.write(0);
}
void loop() {
char key = mykey.getKey();
if (key) {
tone(2, 800);
delay(100);
noTone(2);
if (key == 'A') {
if (input == passcode) {
grantAccess();
}
else {
denyAccess();
}
} else if (key == 'C') {
clearInput();
}
else if (key == 'D') {
input = input.substring(0, input.length() - 1);
updateDisplay();
}
else {
input += key;
updateDisplay();
}
}
}