#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// Pin definitions
#define DHTPIN1 2
#define DHTPIN2 3
#define DHTTYPE DHT22
DHT dht1(DHTPIN1, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 20, 4);
unsigned long lastUpdate = 0;
const unsigned long updateInterval = 2000; // DHT22 timing
// Helper to prevent leftover digits
void printPadded(float value, int width, int decimals) {
if (isnan(value)) {
lcd.print("Err");
for (int i = 0; i < width - 3; i++) lcd.print(" ");
} else {
String s = String(value, decimals);
lcd.print(s);
for (int i = s.length(); i < width; i++) lcd.print(" ");
}
}
void setup() {
Serial.begin(9600);
dht1.begin();
dht2.begin();
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Temp Monitor Ready");
delay(2000);
lcd.clear();
// Static labels (printed once)
lcd.setCursor(0, 0); lcd.print("S1: F %");
lcd.setCursor(0, 1); lcd.print("S2: F %");
lcd.setCursor(0, 2); lcd.print("AVG: F %");
}
void loop() {
if (millis() - lastUpdate >= updateInterval) {
lastUpdate = millis();
float t1C = dht1.readTemperature();
float h1 = dht1.readHumidity();
float t2C = dht2.readTemperature();
float h2 = dht2.readHumidity();
float t1F = t1C * 1.8 + 32;
float t2F = t2C * 1.8 + 32;
float avgTF = (t1F + t2F) / 2.0;
float avgH = (h1 + h2) / 2.0;
// S1
lcd.setCursor(4, 0); // Temp
printPadded(t1F, 6, 1);
lcd.setCursor(12, 0); // Humidity
printPadded(h1, 6, 1);
// S2
lcd.setCursor(4, 1);
printPadded(t2F, 6, 1);
lcd.setCursor(12, 1);
printPadded(h2, 6, 1);
// AVG
lcd.setCursor(5, 2);
printPadded(avgTF, 6, 1);
lcd.setCursor(13, 2);
printPadded(avgH, 5, 1);
}
}