#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C LCD Shield
DHT dht(5, DHT22); // DHT22 sensor connected to pin 2
const int button1Pin = 7; // Button 1 pin
const int button2Pin = 6; // Button 2 pin
bool showCelsius = true;
void setup() {
lcd.init();
lcd.backlight();
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
}
void loop() {
if (digitalRead(button1Pin) == LOW) {
do {
showCelsius = true;
}
while (digitalRead(button1Pin) == HIGH && digitalRead(button2Pin) == HIGH); // Wait for the button to be released
}
else if (digitalRead(button2Pin) == LOW) {
do {
showCelsius = false;
}
while (digitalRead(button2Pin) == HIGH && digitalRead(button1Pin) == HIGH); // Wait for the button to be released
}
float temperature = dht.readTemperature();
if (showCelsius) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp (C): ");
lcd.setCursor(0, 1);
lcd.print(temperature, 1);
} else {
// Convert Celsius to Fahrenheit
lcd.clear();
float temperatureF = (temperature * 9 / 5) + 32;
lcd.setCursor(0, 0);
lcd.print("Temp (F): ");
lcd.setCursor(0, 1);
lcd.print(temperatureF, 1);
}
delay(200); // Update the display every second
}