#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <DHT.h>
#define dhtpin 23
#define dhttype DHT22
DHT dht(dhtpin, dhttype);
#define pot 15
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define sw 12
bool swState;
bool lastswState = HIGH;
bool state = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
pinMode(sw, INPUT);
dht.begin();
}
void loop() {
/////////// LCD ///////////
/*int val = analogRead(pot);
int po = map(val, 0, 4095, 0, 100);
lcd.setCursor(0, 0);
lcd.print("Value Pot: ");
lcd.print( po);
int reading = digitalRead(sw);
if (reading != lastswState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != swState) {
swState = reading;
if (swState == LOW) {
state = !state ;
}
}
}
lastswState = reading;
// ใช้อันนี้ก็ได้
lcd.setCursor(6, 1);
lcd.print(state ? "ON " : "OFF"); */
// ใช้อันนี้ก็ได้
// if (state == LOW){
// lcd.setCursor(0,1);
// lcd.print("OFF");
// }
// else{
// lcd.setCursor(0,1);
// lcd.print(" ON");
// }
/////////// DHT ///////////
float humi = dht.readHumidity(); // ดึงค่าความชื้น
float temp = dht.readTemperature(); // ดึงค่าอุณหภูมิ
if (isnan(humi) || isnan(temp)) {
Serial.println("Failed to read from DHT sensor");
return;
}
Serial.print("Humi : ");
Serial.print(humi);
Serial.print("Humi : ");
Serial.print("\t");
Serial.print("Temp : ");
Serial.println(temp);
delay(500);
lcd.setCursor(0,0);
lcd.print("Humi : ");
lcd.print(humi);
lcd.print(" %");
lcd.setCursor(0,1);
lcd.print("Temp : ");
lcd.print(temp);
lcd.print(" °C");
}