#include <ESP32Servo.h>
Servo servo;
const int trigP = 19;
const int echoP = 18;
long duration;
int distance;
float y;
void setup() {
  pinMode(2, OUTPUT);
  pinMode(trigP, OUTPUT);
  pinMode(echoP, INPUT);
  pinMode(17, INPUT);
  servo.attach(15);
  Serial.begin(9600);
}
void loop() {
  int x = digitalRead(17);
  digitalWrite(trigP, LOW);
  delayMicroseconds(2);
  digitalWrite(trigP, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigP, LOW);
  duration = pulseIn(echoP, HIGH);
  distance = duration*0.034/2;
  y = constrain((distance/400.0)*100, 0, 100);
  servo.write(0);

  if (distance<10){
    digitalWrite(2, HIGH);
    Serial.println(" Dustbin Full ");
    Serial.print("Dustbin Space: ");
    Serial.print(y);
    Serial.print("%");

    delay(1000);
  }
  else{
      if(x==HIGH)
  {
    servo.write(90);
    Serial.println(" Dustbin Open ");
    delay(5000);
  }
  else{
    servo.write(0);
    }
    digitalWrite(2, LOW);
    Serial.println(" Dustbin Close ");
    Serial.print("Dustbin Space: ");
    Serial.print(y);
    Serial.print(" %");
    delay(1000);

  }
}