#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <RTClib.h>
// LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2); // Address 0x27, 16 columns, 2 rows
// Keypad setup
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// RTC setup
RTC_DS3231 rtc;
struct Room {
String name;
String doctor;
String password;
};
Room rooms[5] = {
{"A301", "Ahmed Ayoub", "1234"},
{"A302", "Essam", "5678"},
{"A303", "Ayman", "9101"},
{"B301", "Basma", "1121"},
{"B302", "Ahmed Selim", "3141"}
};
void setup() {
Serial.begin(9600);
// Initialize the LCD
lcd.begin(16, 2); // 16 columns and 2 rows
lcd.backlight(); // Turn on the backlight for the LCD
keypad.addEventListener(keypadEvent);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1); // Halt if RTC is not found
}
Serial.println("System Ready.");
}
void loop() {
char key = keypad.getKey();
if (key) {
handleKey(key);
}
}
void handleKey(char key) {
static String input = "";
if (key == '#') {
checkPassword(input);
input = ""; // Clear the input for the next entry
} else {
input += key; // Add the key pressed to the input string
}
}
void checkPassword(String input) {
bool found = false;
for (int i = 0; i < 5; i++) {
if (input == rooms[i].password) {
found = true;
displayRoomInfo(rooms[i]);
break;
}
}
if (!found) {
lcd.clear();
lcd.print("Invalid Password");
delay(2000); // Show message for 2 seconds
lcd.clear();
}
}
void displayRoomInfo(Room room) {
lcd.clear();
lcd.print("Room: " + room.name);
lcd.setCursor(0, 1);
lcd.print("Doctor: " + room.doctor);
unsigned long startTime = millis();
while (millis() - startTime < 3000) { // Display for 3 seconds non-blocking
// You can add code to handle keypad input here if needed.
}
lcd.clear();
}
void keypadEvent(KeypadEvent eKey) {
switch (keypad.getState()) {
case PRESSED:
Serial.print("Pressed: ");
Serial.println(eKey);
break;
case RELEASED:
Serial.print("Released: ");
Serial.println(eKey);
break;
}
}