#include <LiquidCrystal_I2C.h>
#include <Servo.h>
LiquidCrystal_I2C lcd(0x27,16,2);
Servo Servo1;
const int servoPin = 3;
#include <NewPing.h> //this a library for ultrasound sensor
#define TRIGGER_level 6 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_level 7 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define TRIGGER_lid 9 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_lid 10 // Arduino pin tied to echo pin on the ultrasonic sensor.
NewPing level(TRIGGER_level, ECHO_level, 40); // NewPing setup of pins and maximum distance.
NewPing lid(TRIGGER_lid, ECHO_lid, 5); // NewPing setup of pins and maximum distance.
int lid_distance;//the distance measure by lid sensor is store in this variable
int level_distance_now=-1; // I have decleare two variable for it because it note changes in level if there is a change between prev and now value then it do some otherwise it will ignore every its function
int level_distance_prev=-3;// the values are arbitery any value can given but not should be equal so that first value can be detected
const int buzzer = 4;
const int green_led=5;
const int white_led=11;
const int orange_led= 12;
const int red_led=13;
void setup() {
Servo1.attach(servoPin); // just to inform the arduino that a servo motor is attached to this pin and then it should care of its related when its function is needed
lcd_setup();
pinMode(green_led, OUTPUT);
pinMode(orange_led, OUTPUT);
pinMode(white_led, OUTPUT);
pinMode(red_led, OUTPUT);
}
void loop()
{
sensors_data();// just to gain updated data from both sensors now the variable lid_distance and level_distance_now will have updated data after this function
if((lid_distance<=5)&&(lid_distance>0))//this is the format of code of maths relation "0<distance<5".I have putted the zero because now the maximuim distance is 5 and if detected no thing then for value more than 5 will be consider 0 so we have put condition more than zero not a zero this also a problem to me to it continuesly detect garbish when no thing was there because value more than 5 is consider so and thus I solve this
open_lid();
if((level_distance_now-level_distance_prev>=2)||(level_distance_now-level_distance_prev<=-2))//this is for relation "a differnce of 2 cm has occur " I have keep this difference two cm because 1 cm can occur due to some envirmental change or by insect can come in its fornt so just to insure that a real change in level occur I put 2cm difference "|| pronounce as OR"
level_related_function(); // if real change has occur then do this function other wise lcd should show no display
else
lcd.noDisplay();
}
void open_lid(){
digitalWrite(green_led, HIGH);
Servo1.write(150);
delay(10000);
digitalWrite(green_led, LOW);
Servo1.write(0);
delay(500);
}
void lcd_setup()
{
lcd.init(); // the function nessary for lcd to become on
lcd.clear();// if it has some prev writting then make sure it may not overwrite
lcd.backlight(); // Make sure backlight is on
}
void level_related_function() // the function which deals with change occur in level
{
level_distance_prev=level_distance_now; // level_distance_prev has done its work now it should be updated
if (level_distance_now>33)
{
lcd.display(); // the lcd should show writing
digitalWrite(orange_led, LOW);
digitalWrite(red_led, LOW);
digitalWrite(white_led, HIGH);// you do not need buzzer be low because it already low
lcd.setCursor(2,0); //Set cursor to character 0 on line 0
lcd.clear();
lcd.print("Dustbin is");
lcd.setCursor(3,1);
lcd.print("20% FULL");
delay(30000);
}
if ((20<level_distance_now)&&(33>level_distance_now))// code format for relation"20<level_distance_now<33" "&& can be pronounce as AND"
{
lcd.display();// lcd should show display
digitalWrite(orange_led, HIGH);
digitalWrite(red_led, LOW);
digitalWrite(white_led, LOW);
lcd.clear();// prev should not overwrite
lcd.setCursor(2,0); //Set cursor to character 0 on line 0
lcd.print("Dustbin is");
lcd.setCursor(3,1);
lcd.print("50% FULL");
delay(30000);
}
if ((8<level_distance_now)&&(20>level_distance_now))
{
lcd.display();
digitalWrite(orange_led, LOW);
digitalWrite(red_led, HIGH);
digitalWrite(white_led, LOW);
lcd.clear();
lcd.setCursor(2,0); //Set cursor to character 0 on line 0
lcd.print("Dustbin is");
lcd.setCursor(3,1);
lcd.print("80% FULL");
delay(30000);
}
if ((3<level_distance_now)&&(8>level_distance_now))
{
lcd.display();
digitalWrite(orange_led, LOW);
digitalWrite(red_led, HIGH);
digitalWrite(white_led, LOW);
digitalWrite(buzzer,HIGH);
lcd.clear();
lcd.setCursor(2,0); //Set cursor to character 0 on line 0
lcd.print("Dustbin is");
lcd.setCursor(3,1);
lcd.print("FULL");
delay(30000);
digitalWrite(buzzer,LOW);
}
}
void sensors_data()
{
delay(50); // wait for some time so that prev effects may stable
level_distance_now=level.ping_cm();
lid_distance=lid.ping_cm(); // lid.ping_cm() is the function to detect distance of lid in cm and then we assign it lid distance and from this line it lid_distance is updeted
}