#include <LiquidCrystal.h> // Include the LCD library
#include <DHT.h> // Include the DHT sensor library
#include <Arduino.h> // For Arduino-specific functions
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // LCD pins (for 20x4 LCD)
#define DHTPIN 7
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
const int relayPin = 6; // Digital pin connected to relay module
class HVACSystem {
private:
float temperature;
float target_temperature;
String hvac_mode;
String fan_speed;
int iterations;
public:
HVACSystem() {
temperature = 0.0;
target_temperature = 0.0;
hvac_mode = "off";
fan_speed = "auto";
iterations = 0;
lcd.begin(20, 4); // Initialize the LCD for 20x4 display
pinMode(relayPin, OUTPUT); // Set relay pin as output
dht.begin(); // Start DHT sensor
}
void set_hvac_mode(String mode) {
hvac_mode = mode;
if (mode == "off") {
digitalWrite(relayPin, LOW); // Turn off HVAC system
} else {
digitalWrite(relayPin, HIGH); // Turn on HVAC system
}
}
void set_fan_speed(String speed) {
fan_speed = speed;
}
void read_temperature() {
temperature = dht.readTemperature(); // Read real-time temperature from DHT22 sensor
}
void update_target_temperature() {
if (temperature >= 23) {
target_temperature = random(max(18, temperature - 5), min(22, temperature - 1));
} else if (temperature <= 20) {
target_temperature = random(max(18, temperature + 1), min(22, temperature + 1));
} else {
target_temperature = temperature; // Set target temperature equal to current temperature
}
}
void run() {
iterations = 0; // Reset iterations to 0
lcd.clear(); // Clear LCD screen
lcd.setCursor(0, 0); // Set cursor to beginning of first line
lcd.print("Custom HVAC ON");
delay(2000); // Wait for 2 seconds
int sleep_time;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sleep time (s):");
while (!Serial.available()); // Wait for input from Serial Monitor
sleep_time = Serial.parseInt(); // Read the input as integer
Serial.println(sleep_time);
while (iterations < 20) {
read_temperature();
update_target_temperature();
// Ensure target temperature is within the range of 18 to 22 degrees Celsius
if (target_temperature > 22) {
target_temperature = 22;
} else if (target_temperature < 18) {
target_temperature = 18;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Current Temp: ");
lcd.print((int)temperature); // Cast temperature to integer
lcd.print("*C");
lcd.setCursor(0, 1);
lcd.print("Target Temp: ");
lcd.print((int)target_temperature); // Cast target_temperature to integer
lcd.print("*C");
if (temperature >= 23) {
set_hvac_mode("cooling");
set_fan_speed("auto"); // Set fan speed to auto if cooling is required
} else if (temperature <= 18) {
set_hvac_mode("heating");
set_fan_speed("auto"); // Set fan speed to auto if heating is required
} else {
set_hvac_mode("off");
set_fan_speed("off"); // Set fan speed to off if temperature is within range
}
lcd.setCursor(0, 2); // Move cursor to the beginning of the third line
lcd.print("HVAC Mode: ");
lcd.print(hvac_mode);
lcd.setCursor(0, 3); // Move cursor to the beginning of the fourth line
lcd.print("Fan Speed: ");
lcd.print(fan_speed);
delay(1000 * sleep_time); // Delay in seconds
iterations++;
}
Serial.println("This is a demo of the Custom Climate Controlled HVAC System.");
}
};
HVACSystem hvac;
void setup()
{
Serial.begin(9600);
Serial.println("Welcome to the Serial Monitor!");
Serial.println("---------------------------------");
}
void loop() {
hvac.run();
}