#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int trigPin = 9;
const int echoPin = 8;
long duration;
int distance;
int maxDistance = 100; // Maximum distance for the trash bin height in cm
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Garbage Level:");
}
void loop() {
// Clear the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Set the trigPin HIGH for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distance = duration * 0.034 / 2;
// Calculate the fill level percentage
int fillLevel = map(distance, 0, maxDistance, 100, 0);
// Display the fill level on the LCD
lcd.setCursor(0, 1);
lcd.print("Level: ");
lcd.print(fillLevel);
lcd.print("% ");
delay(500);
}