#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include "DHT.h"
#include <Stepper.h>
#define DHTTYPE DHT22
#define DHTPIN 7
#define LDR_PIN 2
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
int LED1 = 14;
// LDR Characteristics
const float GAMMA = 0.7;
const float RL10 = 50;
const int triggerPin = 2; // Trigger pin of the ultrasonic sensor
const int echoPin = 3; // Echo pin of the ultrasonic sensor
const int servoPin = 9; // Servo control pin
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
DHT dht(DHTPIN, DHTTYPE);
Stepper myStepper(200, 10, 11, 12, 13);
uint8_t heart[8] = {
0b00000,
0b01010,
0b11111,
0b11111,
0b11111,
0b01110,
0b00100,
0b00000,
};
uint8_t Object[8] = {
0b00000,
0b00000,
0b01110,
0b11011,
0b11100,
0b01110,
0b00000,
0b00000
};
Servo myServo;
void DisplayGroupName() {
lcd.createChar(3, heart);
lcd.setCursor(3, 0);
lcd.print(" \x03 Group 6 \x03");
}
void setup() {
//Serial.begin(9600);
pinMode(LDR_PIN, INPUT);
pinMode(LED1, OUTPUT);
pinMode(triggerPin, OUTPUT); //ultrasonic
pinMode(echoPin, INPUT); //ultrasonic
myServo.attach(servoPin);
myServo.write(0); // Initial position of the servo (closed door)
dht.begin();
lcd.init();
lcd.backlight();
DisplayGroupName();
}
float calculateLux() {
//digitalWrite(LED1, HIGH);
int analogValue = analogRead(A0);
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
return lux;
}
void LightOnCheck( float claculatedLux, float PreviousLux) {
if (claculatedLux > 400) {
if(PreviousLux != claculatedLux) {
lcd.setCursor(0, 1);
digitalWrite(LED1, LOW);
lcd.print("Light - OFF");
}
} else {
if(PreviousLux != claculatedLux) {
digitalWrite(LED1, HIGH);
lcd.setCursor(0, 1);
lcd.print("Light - ON ");
PreviousLux = claculatedLux;
}
}
}
void heartAnimation(){
// Heart Design - Not mandatory code
uint8_t heart2[8] = {0};
for (int i = 0; i < 8; i++) {
heart2[i] = heart[i];
lcd.createChar(3, heart2);
delay(10);
}
}
long distanceCalculation() {
long duration = 0,distance =0;
// Trigger the ultrasonic sensor
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
// Measure the echo duration to calculate distance
duration = pulseIn(echoPin, HIGH);
distance = (duration * 0.0343) / 2; // Calculate distance in centimeters
return distance;
}
void DoorOpenCheck(long distance) {
if (distance <= 200) { // If someone is close to the doorway
myServo.write(90); // Open the door (rotate servo by 90 degrees)
// Fan speed Check
calculateHum();
//calculateTemp();
controlFanSpeed();
calculateHum();
//calculateTemp();
controlFanSpeed();
delay(1000); // Keep the door open for 2 seconds
myServo.write(0); // Close the door (return servo to initial position)
//delay(1000); // Wait for 1 second before next iteration
}
}
float humidity = 0.0, temprature = 0;
int fanspeed = 0;
void calculateHum (){
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
humidity = dht.readHumidity();
// Read temperature as Celsius (the default)
temprature = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temprature) || isnan(f)) {
// Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Compute heat index in Fahrenheit (the default)
//float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
//float hic = dht.computeHeatIndex(t, h, false);
/*Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));*/
}
void controlFanSpeed(){
if(temprature <= 25.0) {
fanspeed = 0;
} else if(temprature > 25.0 && temprature< 30 ) {
fanspeed = 2;
} else if(temprature >=30 && temprature< 35 ) {
fanspeed = 3;
} else if(temprature >= 35.0 && temprature< 40 ) {
fanspeed = 4;
} else {
fanspeed = 5;
}
myStepper.setSpeed(map(fanspeed, 0, 5, 0, 200));
myStepper.step(map(fanspeed, 0, 5, 0, 200));
}
void loop() {
// Varibles to hold sensor values
float claculatedLux = 0.0, PreviousLux = 0.0;
long distance =0.0; //ultrasonic
// Door-Check logic
distance = distanceCalculation();
DoorOpenCheck(distance);
// Lux-Check logic
claculatedLux = calculateLux();
LightOnCheck( claculatedLux, PreviousLux);
PreviousLux = claculatedLux;
// Fan speed Check
calculateHum();
//calculateTemp();
controlFanSpeed();
//Serial.print("Distance: ");
//Serial.println(distance);
lcd.setCursor(0, 3);
lcd.print("Lux:");
lcd.print(claculatedLux);
lcd.print(" Temp:");
lcd.print(temprature);
lcd.createChar(1, Object);
lcd.setCursor(0, 2);
if (distance <= 400) {
lcd.print("\1in:");
lcd.print(distance);
lcd.print("cm ");
}
lcd.print(" Hum:");
lcd.print(humidity);
lcd.setCursor(12, 1);
lcd.print(" Fan: ");
lcd.print(fanspeed);
// Serial.print("Lux: ");
// Serial.print(claculatedLux);
// Serial.println(" ");
heartAnimation();
//delay(1);
}