#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

Servo lidServo;
LiquidCrystal_I2C lcd(0x27, 16, 2);  // Initialize LCD

const int triggerPin = 11;  // Digital pin connected to the ultrasonic sensor trigger
const int echoPin = 10;     // Digital pin connected to the ultrasonic sensor echo
const int buzzerPin = 2;    // Digital pin connected to the buzzer
int gameSpeed = 200;        // Adjust the game speed (delay in milliseconds) for lid movement

int currentWasteType = -1;  // Initialize with an invalid value

void setup() {
  lidServo.attach(3);
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzzerPin, OUTPUT);

  lcd.begin(16, 2);
  lcd.print("Sorting Dustbin");
}

void loop() {
  // Trigger ultrasonic sensor
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);

  // Read the echo time
  long duration = pulseIn(echoPin, HIGH);

  // Calculate distance in centimeters
  int distance = duration * 0.034 / 2;

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Size: ");
  lcd.print(distance);
  lcd.print(" cm");

  int newWasteType = -1;  // Initialize with an invalid value

  if (distance < 10) {
    // Type 1 waste detected
    newWasteType = 1;
    lcd.setCursor(0, 1);
    lcd.print("Type: Dust");
  } else if (distance >= 10 && distance < 20) {
    // Type 2 waste detected
    newWasteType = 2;
    lcd.setCursor(0, 1);
    lcd.print("Type: Paper");
  } else if (distance >= 20) {
    // Type 3 waste detected
    newWasteType = 3;
    lcd.setCursor(0, 1);
    lcd.print("Type: Garbage");
  }

  if (newWasteType != currentWasteType) {
    beepBuzzer(3);  // Beep the buzzer 3 times
    openLid();
    currentWasteType = newWasteType;
  }

  // Close the lid after a delay
  closeLid();
}

void openLid() {
  lidServo.write(90);  // Adjust the angle for your servo motor
  delay(3000);  // Keep the lid open for 3 seconds
  lidServo.write(0);  // Close the lid
  delay(1000);  // Delay to allow the lid to close
}

void closeLid() {
  // Add any additional actions when closing the lid
}

void beepBuzzer(int times) {
  for (int i = 0; i < times; i++) {
    digitalWrite(buzzerPin, HIGH);
    delay(200);
    digitalWrite(buzzerPin, LOW);
    delay(200);
  }
}
$abcdeabcde151015202530fghijfghij