#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// Pin definitions
#define DHTPIN1 2
#define DHTPIN2 3
#define BUTTON_PIN 4
#define BUZZER_PIN 5
// Constants
#define DHTTYPE DHT22
#define TEMP_THRESHOLD_C 30.0
#define TEMP_THRESHOLD_F 86.0
// Create DHT objects
DHT dht1(DHTPIN1, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);
// LCD object: 20x4 display at I2C address 0x27
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Variables
bool useFahrenheit = false;
bool lastButtonState = HIGH;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50;
void setup() {
Serial.begin(9600);
dht1.begin();
dht2.begin();
lcd.init();
lcd.backlight();
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
lcd.setCursor(0, 0);
lcd.print("Temp Monitor Ready");
delay(2000);
lcd.clear();
}
void loop() {
// --- BUTTON LOGIC (Toggle between C/F) ---
bool reading = digitalRead(BUTTON_PIN);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading == LOW && lastButtonState == HIGH) {
useFahrenheit = !useFahrenheit;
}
}
lastButtonState = reading;
// --- READ SENSORS ---
float temp1 = dht1.readTemperature();
float humid1 = dht1.readHumidity();
float temp2 = dht2.readTemperature();
float humid2 = dht2.readHumidity();
// Convert to Fahrenheit if needed
float displayTemp1 = useFahrenheit ? (temp1 * 1.8 + 32) : temp1;
float displayTemp2 = useFahrenheit ? (temp2 * 1.8 + 32) : temp2;
float threshold = useFahrenheit ? TEMP_THRESHOLD_F : TEMP_THRESHOLD_C;
// Calculate averages
float avgTemp = (displayTemp1 + displayTemp2) / 2.0;
float avgHumid = (humid1 + humid2) / 2.0;
// --- DISPLAY ON LCD ---
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("T1:");
lcd.print(isnan(displayTemp1) ? "Err" : String(displayTemp1, 1));
lcd.print(useFahrenheit ? "F " : "C ");
lcd.print("H1:");
lcd.print(isnan(humid1) ? "Err" : String(humid1, 1));
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("T2:");
lcd.print(isnan(displayTemp2) ? "Err" : String(displayTemp2, 1));
lcd.print(useFahrenheit ? "F " : "C ");
lcd.print("H2:");
lcd.print(isnan(humid2) ? "Err" : String(humid2, 1));
lcd.print("%");
lcd.setCursor(0, 2);
lcd.print("AVG T:");
lcd.print((isnan(avgTemp) ? "Err" : String(avgTemp, 1)));
lcd.print(useFahrenheit ? "F " : "C ");
lcd.print("H:");
lcd.print((isnan(avgHumid) ? "Err" : String(avgHumid, 1)));
lcd.print("%");
// --- BUZZER LOGIC ---
if (!isnan(avgTemp) && avgTemp > threshold) {
tone(BUZZER_PIN, 1000); // sound buzzer
} else {
noTone(BUZZER_PIN);
}
delay(1000);
}