#include <Servo.h>
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <DHT.h>
// Define IR sensors and servo motor
Servo servo;
int IRpin1 = 15;
int IRpin2 = 16;
int servo_pin = 17;
int val1, val2;
// Define LCD connections
LiquidCrystal lcd(12, 11, 6, 5, 4, 3);
// Define keypad connections
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] = {22, 24, 26, 28}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {30, 32, 34, 36}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Security code
char securityCode[] = "1234";
// Define LED
int greenLED = 13;
int buzzer = 14;
// Define DHT11 sensor
#define DHTTYPE DHT22
#define DHTPIN 2 // Pin connected to the DHT11 sensor
DHT dht(DHTPIN, DHTTYPE);
// Define fan, alarm, push button pins
int fan = 19;
int alarm = 7;
int pushButton = 18;
// Define LDR and LEDs
int ldrPin = A0;
int led1 = 27;
int led2 = 29;
// Temperature threshold
const float THRESHOLD_LOW = 25.0;
const float THRESHOLD_HIGH = 35.0;
bool accessGranted = false;
void setup() {
pinMode(IRpin1, INPUT);
pinMode(IRpin2, INPUT);
servo.attach(servo_pin);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Enter password:");
pinMode(greenLED, OUTPUT);
pinMode(buzzer, OUTPUT);
dht.begin();
pinMode(fan, OUTPUT);
pinMode(alarm, OUTPUT);
pinMode(pushButton, INPUT_PULLUP);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}
void resetSystem() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter password:");
accessGranted = false;
digitalWrite(fan, LOW); // Turn off the fan
digitalWrite(alarm, LOW); // Turn off the alarm
analogWrite(led1, 0); // Turn off LED1
analogWrite(led2, 0); // Turn off LED2
pinMode(pushButton, INPUT_PULLUP); // Reset the push button pin
}
void loop() {
val1 = digitalRead(IRpin1);
val2 = digitalRead(IRpin2);
if (val1 == 0 || val2 == 0) {
servo.write(0);
} else if (val1 == 1 || val2 == 1) {
servo.write(180);
}
static char enteredCode[5]; // Array to store entered code
static byte currentIndex = 0; // Current index of the entered code
char key = keypad.getKey();
if (accessGranted && key == '*') {
resetSystem();
currentIndex = 0;
} else if (!accessGranted && key) {
if (key == 'D') {
if (currentIndex > 0) {
currentIndex--;
lcd.setCursor(currentIndex, 1);
lcd.print(" "); // Clear the digit
}
} else if (key == 'C') {
lcd.setCursor(0, 1);
lcd.print(" "); // Clear all entered digits
currentIndex = 0;
} else if (key == '#') {
if (currentIndex == 4) {
// Compare entered code with the security code
enteredCode[currentIndex] = '\0'; // Null-terminate the entered code array
if (strcmp(enteredCode, securityCode) == 0) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Welcome home!");
digitalWrite(greenLED, HIGH); // Turn on the LED indicating access granted
delay(2000);
digitalWrite(greenLED, LOW);
lcd.clear();
accessGranted = true;
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Wrong password!");
digitalWrite(buzzer, HIGH); // Activate the buzzer indicating wrong password
delay(1000);
digitalWrite(buzzer, LOW); // Deactivate the buzzer
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter password:");
currentIndex = 0;
}
}
}
// Invalid keys before accessgranted.
else if (key != 'A' && key != 'B' && key != '*') {
if (currentIndex < 4) {
lcd.setCursor(currentIndex, 1);
lcd.print('*');
enteredCode[currentIndex] = key;
currentIndex++;
}
}
}
static bool alarmOn = true; // Flag variable to track alarm state
if (digitalRead(pushButton) == HIGH) {
if (alarmOn) {
digitalWrite(alarm, LOW); // Turn off the alarm
alarmOn = false;
}
} else {
alarmOn = true;
}
if (accessGranted) {
// Read light level from LDR
int lightLevel = analogRead(ldrPin);
// Control LED1 brightness based on light level
int led1Brightness = map(lightLevel, 0, 1023, 0, 255);
analogWrite(led1, led1Brightness);
// Control LED2 brightness based on light level
int led2Brightness = map(lightLevel, 0, 1023, 0, 255);
analogWrite(led2, led2Brightness);
lcd.setCursor(0, 1);
lcd.print(digitalRead(pushButton));
// Read temperature from DHT11 sensor
float temperature = dht.readTemperature();
if (!isnan(temperature)) {
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print("C");
// Control the fan based on temperature
if (temperature > THRESHOLD_LOW) {
digitalWrite(fan, HIGH); // Turn on the fan
} else {
digitalWrite(fan, LOW); // Turn off the fan
}
// Control the alarm based on temperature
if (temperature >= THRESHOLD_HIGH) {
digitalWrite(alarm, HIGH); // Turn on the alarm
} else {
digitalWrite(alarm, LOW); // Turn off the alarm
}
}
}
}