#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// Pin definitions
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
const int buzzerPin = 8;
const int dht1Pin = 6;
const int dht2Pin = 7;
// DHT sensor setup
#define DHTTYPE DHT22
DHT dht1(dht1Pin, DHTTYPE);
DHT dht2(dht2Pin, DHTTYPE);
// LCD setup
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Variables for temperature averaging (only for DHT sensors)
float tempDHT1 = 0;
float tempDHT2 = 0;
void setup() {
// Initialize RGB LED pins
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// Initialize DHT sensors
dht1.begin();
dht2.begin();
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Temperature Monitor");
Serial.begin(9600);
}
void loop() {
// Update the DHT sensors
tempDHT1 = dht1.readTemperature(true); // Request Fahrenheit
tempDHT2 = dht2.readTemperature(true); // Request Fahrenheit
// Check for valid DHT readings
if (isnan(tempDHT1)) {
Serial.println("Error reading temperature from DHT1");
tempDHT1 = 0;
}
if (isnan(tempDHT2)) {
Serial.println("Error reading temperature from DHT2");
tempDHT2 = 0;
}
// Calculate the average temperature from DHT sensors
float avgTempFahrenheit = (tempDHT1 + tempDHT2) / 2;
// Print the temperature values (Fahrenheit)
Serial.print("Temperature from DHT1: ");
Serial.print(tempDHT1);
Serial.println("°F");
Serial.print("Temperature from DHT2: ");
Serial.print(tempDHT2);
Serial.println("°F");
Serial.print("Average Temperature (DHT Sensors): ");
Serial.print(avgTempFahrenheit);
Serial.println("°F");
// Display temperature on LCD
lcd.setCursor(0, 1);
lcd.print("DHT1: ");
lcd.print(tempDHT1);
lcd.print(" F");
lcd.setCursor(0, 2);
lcd.print("DHT2: ");
lcd.print(tempDHT2);
lcd.print(" F");
lcd.setCursor(0, 3);
lcd.print("Avg: ");
lcd.print(avgTempFahrenheit);
lcd.print(" F ");
// Temperature irregularities
if (avgTempFahrenheit > 120) {
Serial.println("Warning: Temperature too hot! Get somewhere cool or back inside!");
flashLED(redPin);
soundBuzzer();
} else if (avgTempFahrenheit < 0) {
Serial.println("Warning: Temperature too cold! Get back inside or somewhere warm!");
flashLED(bluePin);
soundBuzzer();
} else {
// Set RGB LED color based on average temperature
if (avgTempFahrenheit < 50) {
setRGBColor(0, 0, 255); // Blue for below 50°F
} else if (avgTempFahrenheit >= 50 && avgTempFahrenheit < 60) {
setRGBColor(255, 255, 0); // Yellow for 50-60°F
} else if (avgTempFahrenheit >= 60 && avgTempFahrenheit < 80) {
setRGBColor(0, 255, 0); // Green for 60-80°F
} else if (avgTempFahrenheit >= 80 && avgTempFahrenheit < 90) {
setRGBColor(255, 165, 0); // Orange for 80-90°F
} else {
setRGBColor(255, 0, 0); // Red for above 90°F
}
}
delay(1000); // Wait 1 second
}
void setRGBColor(int red, int green, int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
void flashLED(int pin) {
for (int i = 0; i < 5; i++) { // Flash 5 times
digitalWrite(pin, HIGH);
delay(500); // Half-second on
digitalWrite(pin, LOW);
delay(500); // Half-second off
}
}
void soundBuzzer() {
for (int i = 0; i < 5; i++) { // Sound buzzer 5 times
digitalWrite(buzzerPin, HIGH);
delay(500); // Half-second on
digitalWrite(buzzerPin, LOW);
delay(250); // Quarter-second off
}
}