#include <LiquidCrystal_I2C.h>

// The below are constants, which cannot be changed
#define LIGHT_SENSOR_PIN  33  // ESP32 pin GIOP36 (ADC0) connected to light sensor
#define LED_PIN           13  // ESP32 pin GIOP22 connected to LED
#define buzzer           27
#define echoPin 4 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 19  //attach pin D3 Arduino to pin Trig of HC-SR04

// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
  
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);

void setup() {
  Serial.begin(115200);
  pinMode(LED_PIN, OUTPUT); // set ESP32 pin to output mode
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
   pinMode(buzzer, OUTPUT);

  LCD.init();
  LCD.backlight();
  LCD.setCursor(1, 0);
  LCD.print("IOT SMART HOME");
  LCD.setCursor(3, 1);
  LCD.print("Light sensor");
  delay(5000);
  LCD.clear();

}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)

  int LDRValue = analogRead(LIGHT_SENSOR_PIN); // read the value on analog pin

  if (LDRValue < 600)
    digitalWrite(LED_PIN, HIGH); // turn on LED
  else
    digitalWrite(LED_PIN, LOW);  // turn off LED
  
   if (distance< 200)
    digitalWrite(buzzer, HIGH); // turn on LED
  else
    digitalWrite(buzzer, LOW);  // turn off LED


   LCD.setCursor(0,0);
   LCD.print("Distance: ");
   LCD.print(distance);
   LCD.println(" cm");
   LCD.setCursor(0,1);
   LCD.print("LDRValue: ");
   LCD.println(LDRValue);
   

   Serial.print("Distance: ");
   Serial.print(distance);
   Serial.println(" cm");
  
   Serial.print("LDRValue: ");
   Serial.println(LDRValue);

}



esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
ultrasonic1:VCC
ultrasonic1:TRIG
ultrasonic1:ECHO
ultrasonic1:GND
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL
ldr1:VCC
ldr1:GND
ldr1:DO
ldr1:AO
led1:A
led1:C
r1:1
r1:2
bz1:1
bz1:2