#define light 13
#define LED_PIN 18
bool g_lightOn = false;
// Include libraries
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#define DEBUG 0 // Set to 1 for debugging statements
// Define ON/OFF values
#define ON 1 // Set to 0 if the logic is inverted
#define OFF 0 // Set to 1 if the logic is inverted
#if DEBUG == 1
#define debug(x) Serial.print(x)
#define debugln(x) Serial.println(x)
#else
#define debug(x)
#define debugln(x)
#endif
#define FIRE_THRESHOLD 30.0
// Thresholds for fan control
#define MaxTemperature 28 // Maximum temperature (in Celsius) to turn on the fan
#define MinTemperature 28 // Minimum temperature (in Celsius) to turn off the fan
// Pin configuration
#define led_pin 2 // Onboard LED pin
#define DHTPIN 4 // Pin to connect the DHT sensor to
#define fan_pin 5 // Pin to connect the fan to
// LCD definitions
#define refresh_rate 500 // Refresh rate of the LCD screen (in milliseconds)
// LCD display
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust the LCD address if necessary
// DHT sensor
DHT dht(DHTPIN, DHT22, 6); // Change DHT22 to DHT11 if using a different sensor model
unsigned long prev = 0; // Store the previous time
bool fan_status = false; // Flag to track the state of the fan
bool led_status = false; // Flag to track the state of the LED
// Function to turn the fan on/off
void fan_on(bool value)
{
digitalWrite(fan_pin, value);
}
// Function to read sensor data and send to LCD
void read_send_Data()
{
// Read temperature and humidity values from the sensor
float temperature_ = dht.readTemperature();
float f = dht.readTemperature(true);
int humidity = dht.readHumidity();
// Display sensor values on the LCD screen
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temperature: ");
lcd.print(temperature_, 1);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print("%");
// Fan control
if (temperature_ >= MaxTemperature)
{
fan_status = true;
Serial.println("Fan ON");
fan_on(ON);
}
else if (temperature_ < MinTemperature)
{
fan_status = false;
Serial.println("Fan OFF");
fan_on(OFF);
}
// Display fan status on LCD
lcd.setCursor(0, 1);
if (!fan_status)
lcd.print("Temperature Normal, Fan: OFF");
else
lcd.print("Temperature High, Fan: ON");
// Print sensor values to the serial monitor
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% Temperature: ");
Serial.print(temperature_);
Serial.print(" C (");
Serial.print(f);
Serial.println(" F)");
}
void setup()
{
// Initialize serial communication
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT); // Set LED_PIN as an output pin
// Initialize the LCD display
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Starting...");
Serial.println("Smart Temperature Fan System"); // Print system information to the serial monitor
// Set pin modes
pinMode(fan_pin, OUTPUT);
pinMode(led_pin, OUTPUT);
digitalWrite(led_pin, LOW); // Turn off the LED
// Make sure the fan is initially off
fan_on(ON);
lcd.setCursor(0, 1);
lcd.print("Initialization Done");
dht.begin(); // Initialize the DHT sensor
Serial.println("Initialization Done");
lcd.clear();
delay(500);
}
void controlLights() {
const float GAMMA = 0.7;
const float RL10 = 50;
// Convert the analog value into lux value:
int analogValue = analogRead(light);
Serial.println(analogValue);
if (analogValue > 1100 && analogValue < 2210) {
if (!g_lightOn) {
g_lightOn = true;
digitalWrite(LED_PIN, HIGH); // Turn on the LED
Serial.println("LED on");
}
}
else {
if (g_lightOn) {
g_lightOn = false;
digitalWrite(LED_PIN, LOW); // Turn off the LED
Serial.println("LED off");
}
}
}
void loop()
{
digitalWrite(led_pin, led_status); // Control the LED
if (millis() - prev > refresh_rate)
{
led_status = !led_status; // Toggle the state of the LED
read_send_Data();
prev = millis();
controlLights(); // Call the controlLights() function
}
delay(10);
}