#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#define dhtpin 2
#define dhttype DHT22
#define switchpin 3
#define lcd_address 0x27//address of lcd in i2c communication protocol
#define column 16//number of bit in a column
#define row 2//number of row in lcd display
unsigned long previous_time=0;
LiquidCrystal_I2C lcd(lcd_address,column,row);
DHT dht(dhtpin,dhttype);
float humidity;
float temperature_c;
float temp_f;
int switch_val;
volatile int temperature_print=HIGH;//high for print temperature in celsius low for fahrenite
void setup() {
// put your setup code here, to run once:
lcd.init();
lcd.backlight();
Serial.begin(9600);
dht.begin();
attachInterrupt(digitalPinToInterrupt(switchpin),Fahrenheit_value,FALLING);
pinMode(switchpin, INPUT);
digitalWrite(switchpin,HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
if(temperature_print==HIGH){
temperature_c=dht.readTemperature();
lcd.setCursor(0,0);
lcd.print("Temp celsius");
lcd.setCursor(12,1);
lcd.print(temperature_c);
if(millis()-previous_time >2000){
previous_time=millis();
lcd.clear();}
}
else{
temp_f=dht.readTemperature(true);//return the rtemperature in fahrenite
lcd.setCursor(0,0);
lcd.print("Temp fahrenite");
lcd.setCursor(10,1);
lcd.print(temp_f);
if(millis()-previous_time >2000){
previous_time=millis();
lcd.clear();
temperature_print=HIGH;
}
}
}
void Fahrenheit_value(){
temperature_print= LOW ;
}