#define BLYNK_TEMPLATE_ID "TMPL66jpxwyHm"
#define BLYNK_TEMPLATE_NAME "Waste Segregation System"
#define BLYNK_AUTH_TOKEN "skc_rf-1T84jqGzNM_Dote8prydxgyYN"

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

const char *ssid = "Wokwi-GUEST";
const char *password = "";


// define IO pins
#define INDUCTIVE_PIN 23
#define CAPACITIVE_PIN 4
#define BUZZER_PIN 13
#define LED_PIN 12
#define MOISTURE_PIN 27
#define ECHO_PIN 18
#define TRIG_PIN 19

#include "LCD_I2C_16X2.h"
#include "Multi_Servo.h"

void setup() {
  Serial.begin(115200);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, password);

  // define sensors pins as input
  pinMode(INDUCTIVE_PIN, INPUT);
  pinMode(CAPACITIVE_PIN, INPUT);
  pinMode(MOISTURE_PIN, INPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);

  // define led and buzzer as output
  digitalWrite(LED_PIN, LOW);
  digitalWrite(BUZZER_PIN, LOW);

  LCD_init();
  ServoInit();
}

void loop() {
  Blynk.run();

  int detect_trash = digitalRead(CAPACITIVE_PIN);
  if (detect_trash == 1) {
    showDetectTrash();
    int moisture = analogRead(MOISTURE_PIN);
    int mvalue = map(moisture, 0, 4095, 0, 100);
    int metal = digitalRead(INDUCTIVE_PIN);
    if (metal == 1) {
      Blynk.virtualWrite(V4, 1);
      showMetalDetected();
      showRotorMovement();
      callMetalBox();
      if (servo_reached == 1) {
        delay(1000);
        float distance = readDistanceCM();
        Blynk.virtualWrite(V0, int(distance));
        showDistance(distance);
        if (distance <= 50) {
          showBinStatus();
          showRotorHomePosition();
          homePosition(90);
        }
        else {
          showGateOpen();
          openGate();
          delay(2000);
          showGateClose();
          closeGate();
          showRotorHomePosition();
          homePosition(90);
        }
        lcd.clear();
        servo_reached = 0;
        digitalWrite(LED_PIN, LOW);
        Blynk.virtualWrite(V3, 0);
      }
      Blynk.virtualWrite(V4, 0);
    }
    else if ( mvalue >= 50) {
      Blynk.virtualWrite(V5, 1);
      showPaperDetected();
      showRotorMovement();
      callPaperBox();
      if (servo_reached == 1) {
        delay(1000);
        float distance = readDistanceCM();
        Blynk.virtualWrite(V1, int(distance));
        showDistance(distance);
        if (distance <= 50) {
          showBinStatus();
          showRotorHomePosition();
          homePosition(180);
        }
        else {
          showGateOpen();
          openGate();
          delay(2000);
          showGateClose();
          closeGate();
          showRotorHomePosition();
          homePosition(180);
        }
        lcd.clear();
        servo_reached = 0;
        digitalWrite(LED_PIN, LOW);
        Blynk.virtualWrite(V3, 0);
      }
      Blynk.virtualWrite(V5, 0);
    }
    else {
      Blynk.virtualWrite(V6, 1);
      showPlasticDetected();
      showRotorMovement();
      callPlasticBox();
      if (servo_reached == 1) {
        delay(1000);
        float distance = readDistanceCM();
        Blynk.virtualWrite(V2, int(distance));
        showDistance(distance);
        if (distance <= 50) {
          showBinStatus();
          showRotorHomePosition();
          homePosition(270);
        }
        else {
          showGateOpen();
          openGate();
          delay(2000);
          showGateClose();
          closeGate();
          showRotorHomePosition();
          homePosition(270);
        }
        lcd.clear();
        servo_reached = 0;
        digitalWrite(LED_PIN, LOW);
        Blynk.virtualWrite(V3, 0);
        Blynk.virtualWrite(V6, 0);
      }
    }
  }
  else if (detect_trash == 0) {
    showPutTrashOnBox();
  }
}

float readDistanceCM() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  int duration = pulseIn(ECHO_PIN, HIGH);
  return duration * 0.034 / 2;
}
Inductive Proximity Sensor
Capacitive Proximity Sensor
Trash Box Rotor Servo
Gate Servo
Buzzer
LED
Ultrasonic Sensor
Moisture Sensor
LCD Display