#include <LiquidCrystal_I2C.h> //เรียกใช้งาน Library จาก Library Manager
#include "DHT.h"
#define LCD_ADDR 0x27
#define LCD_COLUMNS 16 // 16 คอลัมน์
#define LCD_ROWS 2 // 2 แถว
#define DHTPIN 2
#define DHTTYPE DHT22
int btn = 4 ;
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(LCD_ADDR ,LCD_COLUMNS ,LCD_ROWS);
bool state = false; //เก็บค่าปุ่ม
void setup() {
lcd.backlight();
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(btn,INPUT_PULLUP);
lcd.begin(16,2); //กำหนดขนาดของ LCD ที่มี 16 คอลัมน์และ 2 แถว.
lcd.init(); // ทำการเริ่มต้น LCD
lcd.backlight(); // เปิดไฟ LCD
// lcd.setCursor(0,0); // กำหนดตำแหน่งตัวอักษร
// lcd.print("temperature test"); // แสดงผล
// lcd.setCursor(5,5); // กำหนดตำแหน่งตัวอักษร
// lcd.print("START!"); // แสดงผล
//lcd.clear();
dht.begin();
}
void loop() {
lcd.backlight();
// put your main code here, to run repeatedly:
delay(100);
bool btnV = !digitalRead(btn);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (btnV) {
state = !state;
lcd.clear();
delay(200); // รอสักครู่เพื่อป้องกันการกดปุ่มซ้ำ
}
if (state== 1) {
// if(btnV==0){
lcd.setCursor(2,0);
lcd.print("Temperature"); // แสดงผล
lcd.setCursor(5,1); // กำหนดตำแหน่งตัวอักษร
lcd.print(t);
lcd.print("°C ");
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
// }
} else {
lcd.setCursor(4,0); // กำหนดตำแหน่งตัวอักษร
lcd.print("Humidity");
lcd.setCursor(5,1); // กำหนดตำแหน่งตัวอักษร
lcd.print(h);
lcd.setCursor(10,1); // กำหนดตำแหน่งตัวอักษร
lcd.print("%");
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("°F Humidity: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.println(hif);
Serial.println(btnV);
}