#include <DHT.h>
#include <Keypad.h>
#include <IRremote.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
// Readme: Bấm nút "#" để nhập mật khẩu mở cửa khi cửa đóng và nhấn "#" để đóng cửa khi cửa mở
// Ở remote: phím 1 đến bật đèn, phím nguồn để tắt đèn
// Pin Definitions
#define DHTPIN 10
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Keypad setup
const byte ROW_NUM = 4;
const byte COLUMN_NUM = 4;
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte pin_rows[ROW_NUM] = {2, 3, 4, 5};
byte pin_column[COLUMN_NUM] = {6, 7, 8, 9};
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
// IR Remote setup
int recv_pin = 11;
IRrecv irrecv(recv_pin);
// decode_results results;
// Servo setup for door control
Servo myServo;
int servoPin = 13;
// LED setup for light control
int lightPin = 12;
// LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Variables for password and door state
String password = "1234";
String enteredPassword = "";
bool doorOpen = false;
void setup() {
Serial.begin(9600);
dht.begin();
myServo.attach(servoPin);
myServo.write(0); // Start with the door closed
irrecv.enableIRIn();
lcd.begin(16, 2);
lcd.setBacklight(LOW);
pinMode(lightPin, OUTPUT);
}
void loop() {
char key = keypad.getKey();
if (key == '#') {
if (doorOpen) {
// Close the door if it's open
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Closing Door...");
myServo.write(0);
doorOpen = false;
} else {
// Prompt for password if the door is closed
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
enteredPassword = "";
while (true) {
key = keypad.getKey();
if (key) {
if (key == '#') { // Check the password on '#'
if (enteredPassword == password) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Opening Door...!");
myServo.write(90); // Open the door
doorOpen = true;
delay(500);
break;
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Incorrect Pass");
delay(500);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
enteredPassword = "";
}
} else if (key == '*') {
enteredPassword = ""; // Reset password on '*'
lcd.setCursor(0, 1);
lcd.print(" ");
} else if(key=='A'){
break;
} else{
enteredPassword += key; // Add key to password
lcd.setCursor(0, 1);
lcd.print(enteredPassword);
}
}
}
}
}
if(key=='*'){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Enter a new password");
password="";
while(true){
key=keypad.getKey();
if(key){
if(key=='#'){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Password is set");
delay(1000);
break;
}
else {
password+=key;
}
}
}
}
// Display temperature and humidity on LCD
float temp = dht.readTemperature();
float humidity = dht.readHumidity();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: " + String(temp) + " C");
lcd.setCursor(0, 1);
lcd.print("Humidity: " + String(humidity) + " %");
delay(500); // Update every 0.5 seconds
// Check for door control with the '#' key
// IR remote light control
if (irrecv.decode()) {
Serial.println(irrecv.decodedIRData.command);
// Bật bằng 1
if (irrecv.decodedIRData.command == 48) {
digitalWrite(lightPin, HIGH);
lcd.setCursor(0, 1);
lcd.clear();
lcd.print("Light ON");
delay(1000);
} else if (irrecv.decodedIRData.command == 162) {
// Tắt bằng nút tắt nguồn
digitalWrite(lightPin, LOW);
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Light OFF");
delay(1000);
}
irrecv.resume();
}
}