#include "DHT.h"
#define DHTTYPE DHT22
#define DHT22_PIN 7
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
DHT dht(DHT22_PIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
int button = 4;
int bttn = 2;
int state = 0;
int state1 = 0;
const int analogIn = A0;
int RawValue= 0;
double tempC = 0;
double tempF = 0;
const double BETA = 3950;
float humidity;
float temperature;
void setup() {
Serial.begin(9600);
Serial.print("Initializing... ");
pinMode(button, INPUT);
pinMode(bttn, INPUT);
lcd.init();
lcd.begin(16,2);
lcd.backlight();
dht.begin();
}
void loop() {
state = digitalRead(button);
state1 = digitalRead(bttn);
if(state == HIGH && state1 == LOW){
temp();
}
if(state1 == HIGH && state == LOW){
dhtt();
}
if(state1 == LOW && state == LOW){
nopush();
}
if(state1 == HIGH && state == HIGH){
both();
}
lcd.setCursor(0, 0);
lcd.print("Temp C = ");
lcd.setCursor(8, 0);
lcd.print(tempF);
lcd.setCursor(0, 1);
lcd.print("Humi = ");
lcd.setCursor(8, 1);
lcd.print(humidity);
}
void temp(){
RawValue = analogRead(analogIn);
tempC = 1 / (log(1 / (1023. / RawValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
tempF = (tempC * 1.8) + 32; // convert to F
Serial.print("TEMP =");
Serial.println(tempF);
state1 == 0;
state == 0;
delay(3000);
}
void dhtt(){
temperature = dht.readTemperature();
humidity = dht.readHumidity();
Serial.print("Temperature = ");
Serial.print(temperature);
Serial.print("°C Humidity = ");
Serial.print(humidity);
Serial.println("%");
state == LOW;
state1 == LOW;
delay(1000);
}
void nopush(){
Serial.println("PushButton");
state1 == 0;
state == 0;
delay(1000);
}
void both(){
RawValue = analogRead(analogIn);
tempC = 1 / (log(1 / (1023. / RawValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
tempF = (tempC * 1.8) + 32; // convert to F
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
Serial.print("TEMP =");
Serial.print(tempF);
Serial.print(", Temperature = ");
Serial.print(temperature);
Serial.print("°C, Humidity = ");
Serial.print(humidity);
Serial.println("%");
state == 0;
state1 == 0;
delay(1000);
}