// #define LED 2
#include <ESP32Servo.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Can use #define instead of const
// const char *ssid = "AndroidAP3405"; //ENTER YOUR WIFI ssid
// const char *password = "xrbo0123"; //ENTER YOUR WIFI password
const int ServoPin = 2; // declare the servo pin
const int PirPin = 4; // declare the pir pin
const int ledPin1 = 5; // declare the motion indicator led pin
const int ledPin2 = 14; // declare the bin full indicator led pin
const int PIN_TRIG = 13; // declare the trigger pin
const int PIN_ECHO = 12; // declare the echo pin
const int speed = 30; // constant for lid speed
int lidState = LOW; // Initialize assuming the lid is closed
int pirState = LOW; // Initialize the sensor assuming no motion is detected
int val = 0; // declare variable for getting sensor state
int pos = 90; // declare variable for lid position
int threshold = 15;
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
float duration_us,distance_cm;
Servo servo;
void setup() {
// put your setup code here, to run once:
servo.attach(ServoPin, 500, 2400);
pinMode(PirPin, INPUT); // Declare the pir as input
pinMode(ledPin1, OUTPUT); // Declare the led as output
pinMode(ledPin2, OUTPUT); // Declare the led as output
pinMode(PIN_TRIG, OUTPUT); // Declare the trigger pin as output
pinMode(PIN_ECHO, INPUT); // Declare the echo pin as input
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.setCursor(0,0);
Serial.begin(115200);
}
void loop() {
val = digitalRead(PirPin); // Read input value
// Check if the bin is full
// take a new measurement
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
//measure duration of pulse form echo pin
duration_us = pulseIn(12,HIGH);
// calculate the distance
distance_cm = (0.017*duration_us)/4;
lcd.clear();
lcd.print("Bio degradable");
lcd.setCursor(0,1);
lcd.print("Drop Here: ");
lcd.print(distance_cm);
// Read the result
int duration = pulseIn(PIN_ECHO, HIGH);
int distance = duration / 58;
Serial.print("Bio-degradable: ");
Serial.println(distance_cm);
bool is_full = distance <= threshold; // checks if the bin is full
Serial.println(is_full);
if (val == HIGH) {
if (!is_full) {
// if the bin is not full
// When motion is detected, turn on the led and open the lid.
digitalWrite(ledPin1, HIGH);
Serial.println("motion detected");
if (pirState == LOW) {
pirState == HIGH;
}
open();
} else {
// Blink the full indicator light if the bin is full
digitalWrite(ledPin2, HIGH);
delay(1000);
digitalWrite(ledPin2, LOW);
}
} else {
digitalWrite(ledPin1, LOW);
//Serial.println("no motion");
if (pirState == HIGH) {
pirState == LOW;
}
close();
}
}
void open () {
if (lidState == LOW) {
while(pos >= 1) {
servo.write(pos);
pos -= 1;
delay(speed);
}
// pos = 0;
lidState = HIGH;
}
}
void close() {
if (lidState == HIGH) {
while(pos <= 89) {
servo.write(pos);
pos += 1;
delay(speed);
}
// pos = 90;
lidState = LOW;
}
}