#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <Keypad.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
// LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change the I2C address if necessary
// Servo setup
Servo motor;
const int motorPin = 11;
int motorPosition = 0;
// 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}; // R1, R2, R3, R4
byte colPins[COLS] = {5, 4, 3, 2}; // C1, C2, C3, C4
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
// PIR motion sensor setup
const int pirPin = 12;
// DHT22 setup
#define DHTPIN 13
#define DHTTYPE DHT22
DHT_Unified dht(DHTPIN, DHTTYPE);
sensors_event_t event;
// Password setup
const char* correctPassword = "0000";
char enteredPassword[5] = "";
boolean passwordEntered = false;
void setup() {
// LCD initialization
lcd.init();
lcd.backlight();
// Servo initialization
motor.attach(motorPin);
motor.write(motorPosition);
// DHT22 initialization
dht.begin();
// Initialize Serial communication for debugging
Serial.begin(9600);
}
void loop() {
if (!passwordEntered) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter the password");
delay(175);
char key = keypad.getKey();
if (key) {
if (key == '#') {
if (strcmp(enteredPassword, correctPassword) == 0) {
passwordEntered = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Password correct");
delay(1000);
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Wrong password");
delay(500);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter the password");
memset(enteredPassword, 0, sizeof(enteredPassword));
} else if (key == '*') {
memset(enteredPassword, 0, sizeof(enteredPassword));
} else {
strncat(enteredPassword, &key, 1);
}
}
} else {
lcd.clear();
lcd.setCursor(0, 0);
dht.temperature().getEvent(&event);
lcd.print("Temp: ");
lcd.print(event.temperature);
lcd.print(" C");
delay(125);
lcd.setCursor(0, 1);
dht.humidity().getEvent(&event);
lcd.print("Humid: ");
lcd.print(event.relative_humidity);
lcd.print(" %");
delay(125);
int motionValue = digitalRead(pirPin);
if (motionValue == HIGH) {
motor.write(90);
} else {
motor.write(0);
}
}
}