#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#define IC2_ADDR 0*27
#define LCD_COLUMNS 16
#define LCD_lINES 2
LiquidCrystal_I2C lcd(IC2_ADDR, LCD_COLUMNS, LCD_lINES);
#define PIN_TRIG 26
#define PIN_ECHO 25
#define greenLight 18
#define RedLight 5
#define height = 0;
void setup()
{
Serial.begin(115200);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
pinMode(greenLight, OUTPUT);
pinMode(RedLight, OUTPUT);
Wire.begin(2,3);
}
float readDistanceCM(){
digitalWrite(PIN_TRIG, LOW);
delayMicroseconds(2);
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
int duration =pulseIn(PIN_ECHO, HIGH);
return duration * 0.034 / 2;
}
void loop() {
float distance = readDistanceCM();
//to know if the bin > 122 or not
bool notNearby = distance < 112;
digitalWrite(greenLight, notNearby);
//to know if the bin > 122 or not
bool almostFull = distance > 112;
digitalWrite(RedLight, almostFull); //if the bin is full, red light = 1
//Read the result:
int duration= pulseIn(PIN_ECHO, HIGH);
Serial.print("Distance in CM: ");
Serial.println(readDistanceCM());
// LCD printing
lcd.clear(); // Clear the LCD before printing
if(notNearby) {
lcd.setCursor(0, 0);
lcd.print(" >>BIN STILL HAS SPACE <<");
}else{
lcd.setCursor(0, 0);
lcd.print(">> BIN IS NEAR FULL <<");
}
delay(100);
}