#include <Keypad.h>
#include <Servo.h>
#include <DHT.h>
#define DHTPIN 7
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// 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] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 8, 9, 10};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String password = "1234";
String inputPassword = "";
// Servo setup
Servo myServo;
int pos = 0;
// Sensor pins
const int pirPin = 12;
const int ldrPin = A0;
const int ledPin = 13;
const int fanLedPin = 8; // fan - green pin
const int heaterLedPin = 9; //heater - blue pin
// Thresholds
int lightThreshold = 500;
float tempThresholdHigh = 25.0; // Temperature to turn on fan
float tempThresholdLow = 20.0; // Temperature to turn on heater
void setup() {
Serial.begin(9600);
myServo.attach(11);
myServo.write(0);
pinMode(pirPin, INPUT);
pinMode(ldrPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(fanLedPin, OUTPUT);
pinMode(heaterLedPin, OUTPUT);
dht.begin();
}
void loop() {
// Temperature-based fan/heater control
float temperature = dht.readTemperature();
Serial.print("Temperature: ");
Serial.println(temperature);
if (temperature > tempThresholdHigh) {
digitalWrite(fanLedPin, HIGH);
digitalWrite(heaterLedPin, LOW);
} else if (temperature < tempThresholdLow) {
digitalWrite(fanLedPin, LOW);
digitalWrite(heaterLedPin, HIGH);
} else {
digitalWrite(fanLedPin, LOW);
digitalWrite(heaterLedPin, LOW);
}
delay(3000);
// Light control
int lightLevel = analogRead(ldrPin);
int motionDetected = digitalRead(pirPin);
Serial.print("PIR: ");
Serial.print(motionDetected);
Serial.print(" | LDR: ");
Serial.println(lightLevel);
if ((lightLevel < lightThreshold) && motionDetected==HIGH) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
delay(100);
// Keypad door lock
char key = keypad.getKey();
if (key) {
inputPassword += key;
if (key == '#') {
if (inputPassword == password) {
myServo.write(90); // Open lock
delay(5000);
myServo.write(0); // Lock again after 5 seconds
} else {
Serial.println("Wrong Password");
}
inputPassword = "";
} else if (key == '*') {
inputPassword = ""; // Reset input
}
}
delay(1000);
}