#include <Wire.h>
#include <Servo.h>
#include <DHT.h>
#include <IRremote.h>
#include <Keypad.h>
#include <NewPing.h>
#include <LiquidCrystal_I2C.h>
#define PIN_RECEIVER 6
IRrecv receiver(PIN_RECEIVER);
const int s1Pin = 11;
const int s2Pin = 10;
const int s3Pin = 9;
Servo servo1;
Servo servo2;
Servo servo3;
const int pass_Length = 8;
char password[pass_Length + 1] = "12345678";
char enteredPassword[pass_Length + 1];
int pass_Index = 0;
const byte ROWS = 4; // Number of rows on the keypad
const byte COLS = 4; // Number of columns on the keypad
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {8,7,5,4};
byte colPins[COLS] = {A0,A1,A2,A3};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int triggerPin = 2;
const int echoPin = 3;
const int minDistance = 10;
const int maxDistance = 300;
NewPing sonar(triggerPin, echoPin, maxDistance);
#define DHTPIN 13
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
servo1.attach(s1Pin);
servo2.attach(s2Pin);
servo3.attach(s3Pin);
servo1.write(0);
servo2.write(0);
servo3.write(0);
lcd.begin(16, 2); // Initialize the LCD screen
lcd.print("Enter password:"); // Display initial message
Serial.begin(9600);
receiver.enableIRIn();
}
void loop() {
DHT_22();
Servo();
char key = keypad.getKey();
if (key) {
if (key == '#') {
if (pass_Index >= pass_Length) {
enteredPassword[pass_Index] = '\0';
checkPass();
}
pass_Index = 0;
}
else if (pass_Index < pass_Length) {
enteredPassword[pass_Index] = key;
pass_Index++;
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print("*");
}
}
delay(50);
int distance = sonar.ping_cm();
if (distance > 0 && distance <= minDistance) {
KitchenDoor();
}
}
///
void DHT_22(){
float Temperature = dht.readTemperature();
float Humidity = dht.readHumidity();
lcd.setCursor(0, 2);
lcd.print("Humidity: ");
lcd.print(Humidity);
lcd.setCursor(0, 3);
lcd.print("Temperature: ");
lcd.print(Temperature);
}
///
void checkPass() {
if (strcmp(enteredPassword, password) == 0) {
MainDoor();
} else {
lcd.setCursor(0, 0);
lcd.print("error");
}
for (int i = 0; i < pass_Length; i++) {
enteredPassword[i] = '\0';
}
}
///
void Servo(){
if (receiver.decode()) {
switch (receiver.decodedIRData.command) {
case 144:
servo3.write(servo3.read() + 100);
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print("open the window");
break;
case 224:
servo3.write(servo3.read() - 100);
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print("closed the window");
break;
}
}
receiver.resume();
}
///
void KitchenDoor() {
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print("Kitchen door opened!");
servo2.write(90);
delay(2000);
servo2.write(0);
}
///
void MainDoor() {
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("Main door opened!");
servo1.write(90);
delay(2000);
servo1.write(0);
}