#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Servo.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
/* Keypad setup */
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);
void setup() {
// put your setup code here, to run once:
lcd.init();
lcd.backlight();
showStartupMessage();
}
void loop() {
// put your main code here, to run repeatedly:
}
// void lock() {
// lockServo.write(SERVO_LOCK_POS);
// safeState.lock();
// }
// void unlock() {
// lockServo.write(SERVO_UNLOCK_POS);
// }
void showStartupMessage() {
lcd.setCursor(6, 1);
lcd.print("Welcome!");
delay(1000);
lcd.setCursor(4, 2);
String message = "Smart House";
for (byte i = 0; i < message.length(); i++) {
lcd.print(message[i]);
delay(100);
}
delay(500);
}
String inputSecretCode() {
lcd.setCursor(5, 1);
lcd.print("[____]");
lcd.setCursor(6, 1);
String result = "";
while (result.length() < 4) {
char key = keypad.getKey();
if (key >= '0' && key <= '9') {
lcd.print('*');
result += key;
}
}
return result;
}
void showWaitScreen(int delayMillis) {
lcd.setCursor(2, 1);
lcd.print("[..........]");
lcd.setCursor(3, 1);
for (byte i = 0; i < 10; i++) {
delay(delayMillis);
lcd.print("=");
}
}
bool setNewCode() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter new password:");
String newCode = inputSecretCode();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Confirm new password");
String confirmCode = inputSecretCode();
if (newCode.equals(confirmCode)) {
setCode(newCode);
return true;
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Password doesn't match");
lcd.setCursor(3, 1);
lcd.print("Door is not locked!");
delay(2000);
return false;
}
}
void showUnlockMessage() {
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("Door Unlocked!");
delay(1000);
}
void safeUnlockedLogic() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press # to lock the door");
bool newCodeNeeded = true;
if (safeState.hasCode()) {
lcd.setCursor(0, 2);
lcd.print("Press A = set new password");
newCodeNeeded = false;
}
auto key = keypad.getKey();
while (key != 'A' && key != '#') {
key = keypad.getKey();
}
bool readyToLock = true;
if (key == 'A' || newCodeNeeded) {
readyToLock = setNewCode();
}
if (readyToLock) {
safeState.lock();
lock();
showWaitScreen(100);
}
}
void safeLockedLogic() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(ICON_LOCKED_CHAR);
lcd.print(" Door Locked! ");
lcd.write(ICON_LOCKED_CHAR);
String userCode = inputSecretCode();
bool unlockedSuccessfully = safeState.unlock(userCode);
showWaitScreen(200);
if (unlockedSuccessfully) {
showUnlockMessage();
unlock();
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Access Denied!");
showWaitScreen(1000);
}
}
/*--------------------------Project Notes--------------------------*/
// This project covers the whole subject.
// The assigment subjectives on E-learning.
// we need multiplexer to connect all the items, we build it using logic gates.
// search for multiplexer circuit diagram (https://images.app.goo.gl/KthhiWZewi6oRdUX7).
// write a code to set and change the password - for ex = press # and * to change the password.
// we need to write a documant about the project in the solution file.
// servo motors use (PWM) pins.
/*--------------------------Project Objectives--------------------------*/
// 1- Open main door with password
// 2- Open kitchen door according to distance sensor value
// 3- Display temperature and humidity on LCD screen
// 4- Remote control to open windows according to key press value from 0 degree to 180 degree ( depends on time key pressed) keys right and left.