#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#define servoPin 3
#define pirPin 2
#define redLedPin 10
#define greenLedPin 13
#define triggerPin 12
#define echoPin 11
#define buzPin 7
LiquidCrystal_I2C lcd(0x27, 20, 4);
Servo myservo;
int servoPos = 0; // variable to store the servo position
void setup()
{
Serial.begin(9600);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(pirPin, INPUT);
pinMode(greenLedPin, OUTPUT); //initialize the Street Lamp pin as an output
pinMode(redLedPin, OUTPUT);
pinMode(buzPin, OUTPUT);
myservo.attach(3); // attaches the servo on pin 3 to the servo object
lcd.init();
lcd.backlight();
}
void loop()
{
lcd.clear();
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
float dustBinSpace = (pulseIn(echoPin, HIGH))/58.0; // Calculates the Dust Bin left space in cm
if(dustBinSpace > 10.0) // Taking 10cm as cutoff Mark to determine DustBin full or empty
{
Serial.println("Dust Bin Not Full");
lcd.setCursor(0, 0);
lcd.print("DustBin Not Full");
digitalWrite(greenLedPin, HIGH);
digitalWrite(redLedPin, LOW);
noTone(buzPin);
}
else
{
Serial.println("Dust Bin Full");
lcd.setCursor(0, 0);
lcd.print("DustBin Full");
digitalWrite(greenLedPin, LOW);
digitalWrite(redLedPin, HIGH);
//digitalWrite(buzPin, HIGH);
tone(buzPin, 10000,500);
}
delay(300);
int pirStatus = digitalRead(pirPin);
if((pirStatus == HIGH) && (dustBinSpace > 10.0))
{
Serial.println("Motion Detected, Opening DustBin Cover");
noTone(buzPin);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Motion Detect");
lcd.setCursor(0, 1);
lcd.print("DustBin Cover");
lcd.setCursor(0, 2);
lcd.print("Opening");
myservo.write(90);
delay(5000); // Opening DustBin Cover only for 5 Seconds
myservo.write(0);
lcd.clear();
}
else if(pirStatus == HIGH)
{
Serial.println("Motion Detected, But DustBin Full");
Serial.println("Please Empty the Dust Bin");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Motion Detect");
lcd.setCursor(0, 1);
lcd.print("But DustBin Full");
lcd.setCursor(0, 2);
lcd.print("Please Empty DustBin");
tone(buzPin, 10000,500);
delay(5000);
}
else
{
Serial.println("No Motion Detected");
}
}