#define BLYNK_TEMPLATE_ID "TMPL6UhsR9nzY"
#define BLYNK_TEMPLATE_NAME "Automatic Dustbin"
#define BLYNK_AUTH_TOKEN "zVmgV_yPStNhB2Vf8h3w1XoFFDmVWaAO"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#define BLYNK_PRINT Serial

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "**********";
char pass[] = "**********";

BlynkTimer timer;
int MaxLevel = 400;
int Level1 = 350;
int Level2 = 250;
int Level3 = 100;
int Level4 = 2;

LiquidCrystal_I2C lcd(0x27, 16, 2);  

const int trigPin = 12;  
const int echoPin = 13;  

void setup() {
  lcd.init();                      
  lcd.backlight();
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT); 
  Blynk.begin(auth, ssid, pass);
  delay(2000);
        
}

void ultrasonic() {
  digitalWrite(trigPin, LOW);      
  delayMicroseconds(2);            
  digitalWrite(trigPin, HIGH);     
  delayMicroseconds(10);           
  digitalWrite(trigPin, LOW);      
  
  long t = pulseIn(echoPin, HIGH);
  int distance = t / 29 / 2;

  long duration = pulseIn(echoPin, HIGH);  
  float distance_cm = duration * 0.034 / 2;

  Serial.println(distance);
    if (distance <= MaxLevel+10) {}

    lcd.setCursor(0, 0);
    lcd.print("Level :");

  if (Level1 <= distance) {
    lcd.setCursor(8, 0);
    lcd.print("Very Low");
    lcd.setCursor(0, 1);}

  else if (Level2 <= distance && Level1 > distance) {
    lcd.setCursor(8, 0);
    lcd.print("Low");
    lcd.print("      ");
    lcd.setCursor(0, 1);}

  else if (Level3 <= distance && Level2 > distance) {
    lcd.setCursor(8, 0);
    lcd.print("Medium");
    lcd.print("      ");
    lcd.setCursor(0, 1);}

  else if (Level4 <= distance && Level3 > distance) {
    lcd.setCursor(8, 0);
    lcd.print("Full");
    lcd.print("      ");}

  else if (distance < Level4) {
    lcd.setCursor(8, 0);
    lcd.print("Full !");
    lcd.print("      ");}

    Blynk.virtualWrite(V0, MaxLevel);
}

void loop (){
ultrasonic();
Blynk.run();
timer.run();
}