#include <LiquidCrystal_I2C.h>
#include <HCSR04.h>
#include <DHT22.h>

#define pinDATA 6 

DHT22 dht22(pinDATA); 

#define ldrDigital 10
#define ldrAnalog A1
#define tempAnalog A2
#define pirSensor 12
#define ledpin 11

LiquidCrystal_I2C lcd(0x27,16,2);
UltraSonicDistanceSensor distanceSensor(7, 8);

void setup() {
  Serial.begin(115200);
  pinMode(ldrAnalog, INPUT);
  pinMode(ldrDigital, INPUT);
  pinMode(tempAnalog, INPUT);
  pinMode(pirSensor, INPUT);
  pinMode(ledpin, OUTPUT);
  lcd.init();
  lcd.backlight();
}


void loop() {
  float t = dht22.getTemperature();
  float h = dht22.getHumidity();

  if (dht22.getLastError() != dht22.OK) {
    Serial.print("last error :");
    Serial.println(dht22.getLastError());
  }
  delay(500);
  if ((digitalRead(pirSensor)) && (analogRead(ldrAnalog) > 500)) {
    digitalWrite(ledpin, HIGH);
  } else {
    digitalWrite(ledpin, LOW);
  }
  
  lcd.setCursor(0,0);
  lcd.print("T:");
  lcd.print(t);
  lcd.print(" H:");
  lcd.print(h);
  
  lcd.setCursor(0,1);
  lcd.print("Distance:");
  lcd.print(distanceSensor.measureDistanceCm());
}