#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize I2C LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // Replace 0x27 with your LCD's I2C address
// Ultrasonic sensor pins
const int trigPin = 9;
const int echoPin = 10;
// Buzzer pin
const int buzzerPin = 11;
// Maximum bin height (in cm)
const int binHeight = 300; // Adjust this based on bin size
// Threshold for full bin (in cm)
const int threshold = 50; // Distance indicating the bin is full
void setup() {
// Initialize pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
// Initialize LCD
lcd.begin(16, 2);
lcd.backlight();
//Serial.begin(9600);
}
void loop() {
// Measure distance
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
int distance = duration * 0.034 / 2;
//Check for valid distance
//if (distance <= 0 || distance > binHeight) {
// return; // Skip this loop if distance is out of range
//}
// Calculate fullness percentage
int fullness = 100 - (distance * 100 / binHeight);
//fullness = constrain(fullness, 0, 100); // Ensure fullness is between 0 and 100
// Display fullness percentage on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Waste Level:");
lcd.setCursor(0, 1);
lcd.print(fullness);
lcd.print("%");
// Check if bin is full
if (distance <= threshold) {
digitalWrite(buzzerPin, HIGH); // Activate buzzer
lcd.setCursor(10, 1);
lcd.print("Full!");
delay(500); // Slight delay to prevent buzzer from toggling rapidly
} else {
digitalWrite(buzzerPin, LOW); // Deactivate buzzer
}
delay(1000); // Refresh rate of 1 second
}