/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: Waste Management
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2025-11-06 13:18:22

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* This Smart Waste Segregation Bin uses an Arduino */
	/* Uno, IR sensor, ultrasonic sensors,servo motors */
	/* and detacts waste based on relection */
/****** END SYSTEM REQUIREMENTS *****/


/* START CODE */

// System Requirements: Control waste sorting system using IR sensor and ultrasonic sensors with servo motors

#include <Servo.h>  // Include servo library for controlling servo motors

// Define PWM output pin for servo
const uint8_t kie_Servomotor_PWMSignal_PIN_D3 = 3;

// Define pins for IR sensor and ultrasonic sensors
#define IR_SENSOR A0
#define SERVO_1 3
#define SERVO_2 5
#define SERVO_3 6

#define ORGANIC_TRIG 8
#define ORGANIC_ECHO 9
#define RECYCLABLE_TRIG 10
#define RECYCLABLE_ECHO 11
#define OTHER_TRIG 12
#define OTHER_ECHO 13

// Create servo objects
Servo servo1;
Servo servo2;
Servo servo3;

// Variables to hold ultrasonic sensor readings
int organicDistance;
int recyclableDistance;
int otherDistance;

// Threshold distance to determine if bin is full (in cm)
int threshold = 5;

void setup() {
  Serial.begin(9600); // Initialize serial communication

  // Attach servo objects to PWM pins
  servo1.attach(SERVO_1);
  servo2.attach(SERVO_2);
  servo3.attach(SERVO_3);

  // Initialize ultrasonic sensor pins
  pinMode(ORGANIC_TRIG, OUTPUT);
  pinMode(ORGANIC_ECHO, INPUT);
  pinMode(RECYCLABLE_TRIG, OUTPUT);
  pinMode(RECYCLABLE_ECHO, INPUT);
  pinMode(OTHER_TRIG, OUTPUT);
  pinMode(OTHER_ECHO, INPUT);
}

void loop() {
  // Read IR sensor value
  int reflectivity = analogRead(IR_SENSOR);

  // Determine waste category based on reflectivity
  String wasteCategory;
  if (reflectivity > 800) {
    wasteCategory = "Organic";
  } else if (reflectivity > 400) {
    wasteCategory = "Recyclable";
  } else {
    wasteCategory = "Other";
  }

  // Print waste category
  Serial.print("Waste Category: ");
  Serial.println(wasteCategory);

  // Process waste based on category
  if (wasteCategory == "Organic") {
    organicDistance = getDistance(ORGANIC_TRIG, ORGANIC_ECHO);
    if (organicDistance > threshold) {
      servo1.write(90); // Open lid
      delay(2000); // Wait
      servo1.write(0); // Close lid
    } else {
      Serial.println("Bin Full");
    }
  } else if (wasteCategory == "Recyclable") {
    recyclableDistance = getDistance(RECYCLABLE_TRIG, RECYCLABLE_ECHO);
    if (recyclableDistance > threshold) {
      servo2.write(90);
      delay(2000);
      servo2.write(0);
    } else {
      Serial.println("Bin Full");
    }
  } else {
    otherDistance = getDistance(OTHER_TRIG, OTHER_ECHO);
    if (otherDistance > threshold) {
      servo3.write(90);
      delay(2000);
      servo3.write(0);
    } else {
      Serial.println("Bin Full");
    }
  }
  delay(100); // Small delay
}

// Function to get distance from ultrasonic sensor
int getDistance(int trigPin, int echoPin) {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  long duration = pulseIn(echoPin, HIGH);
  int distance = duration * 0.034 / 2;
  return distance;
}

/* END CODE */
