#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
#include <Adafruit_AHT10.h>
 Adafruit_AHT10 aht;
 //input
#define sensorl 25//ควัน
#define sensorlA  26 // =ชื่น 
#define sensorl ldr  27// แสง
// output
#define ledred 15
#define ledyellow 18
#define ledgreen 19
#define relay 17
#define moter 16
#define value 0

void setup() {
Serial.println("Adafruit AHT10 demo!");
 Serial.begin(9600);
 
 lcd.init();
lcd.backlight();
pinMode (sensorl,INPUT_PULLUP);
pinMode (sensorlA,INPUT_PULLUP);
pinMode (sensorl ldr,INPUT_PULLUP);
pinMode(ledred, OUTPUT); 
pinMode(ledyellow, OUTPUT); 
pinMode(ledgreen, OUTPUT); 
pinMode(relay, OUTPUT); 
pinMode(moter, OUTPUT); 

}
 
void loop() {
   lcd.setCursor(0,0);
 lcd.print("Smoke");
  value = digitalRead(sensorl);
  Serial.println(value);
  lcd.setCursor(0,0);
lcd.print("Smoke");
  Serial.println(value);
  delay(100);
if(value == 0){
  digitalWrite(ledred,1);
  Serial.print("HaveSmoke");
  lcd.setCursor(0,1);
   lcd.print(":HaveSmoke ");
}else{
  digitalWrite(ledred,0);
  Serial.print("No Smoke");
  lcd.setCursor(0,1);
   lcd.print(": NO Smoke");
}
 if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    showPage1 = !showPage1;  
  }

  sensors_event_t humidity, temp;
  aht.getEvent(&humidity, &temp);
  Serial.print("Temp:");
  Serial.print(temp.temperature);
  Serial.print(" ํC");
  Serial.print(" Humi:");
  Serial.print(humidity.relative_humidity);
  Serial.println("%Rh");
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);

  lcd.setCursor(0, 0);
  lcd.print("T:");
  lcd.print(temp.temperature, 1);
  lcd.setCursor(7, 0);
  lcd.write( 223);
  lcd.print("C");
  lcd.setCursor(10, 0);
  lcd.print("H:");
  lcd.print(humidity.relative_humidity, 1);
  lcd.print(" %");
  lcd.setCursor(0, 1);
  lcd.print("D:");
  lcd.print(distanceCm, 1);
  lcd.setCursor(7, 1);
  lcd.print("cm");
  
  if (temp.temperature <= 25) {
    digitalWrite(relay, 0);
    digitalWrite(ledgreen,1);
    lcd.setCursor(10, 1);
    lcd.print("R:ON ");
  } 
  
  if (temp.temperature >= 26) {
    digitalWrite(relay, 1);
    digitalWrite(ledgreen,1);
    lcd.setCursor(10, 1);
    lcd.print("R:OFF");
  }
  
}