#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <DHT.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Alamat I2C LCD: 0x27
Servo myservo;
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define pinTrig 7
#define pinEcho 6
#define ledHijau 8
#define ledMerah 9
#define ledBiru 10
const int threshold = 50; // Ambil nilai ambang batas untuk membedakan organik dan anorganik
void bukatutup()
{
long duration, distance;
digitalWrite(pinTrig, LOW);
delayMicroseconds(2);
digitalWrite(pinTrig, HIGH);
delayMicroseconds(10);
digitalWrite(pinTrig, LOW);
duration = pulseIn(pinEcho, HIGH);
distance = duration / 58.2;
if (distance > 15)
{
myservo.write(90); // Posisi servo normal jika tidak ada sampah di dekat
delay(1000);
}
else if (distance <= 15)
{
float humidity = dht.readHumidity(); // Baca nilai kelembapan
if (humidity > threshold)
{
myservo.write(0); // Mendorong sampah ke kiri jika terdeteksi sampah organik
delay(1000);
}
else
{
myservo.write(180); // Mendorong sampah ke kanan jika terdeteksi sampah non-organik
delay(1000);
}
// Tambahkan logika untuk menahan servo di posisi awal ketika kedua jenis sampah terdeteksi bersamaan
long secondDuration, secondDistance;
digitalWrite(pinTrig, LOW);
delayMicroseconds(2);
digitalWrite(pinTrig, HIGH);
delayMicroseconds(10);
digitalWrite(pinTrig, LOW);
secondDuration = pulseIn(pinEcho, HIGH);
secondDistance = secondDuration / 58.2;
if (secondDistance <= 15)
{
myservo.write(90); // Tahan servo di posisi awal jika kedua jenis sampah terdeteksi bersamaan
}
}
}
void ledorganik()
{
digitalWrite(ledHijau, HIGH);
digitalWrite(ledMerah, LOW);
digitalWrite(ledBiru, LOW);
}
void lednonorganik()
{
digitalWrite(ledHijau, LOW);
digitalWrite(ledMerah, LOW);
digitalWrite(ledBiru, HIGH);
}
void ledtidakterdeteksi()
{
digitalWrite(ledHijau, LOW);
digitalWrite(ledMerah, HIGH);
digitalWrite(ledBiru, LOW);
}
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2); // Inisialisasi LCD 16x2
lcd.backlight(); // Aktifkan backlight LCD
pinMode(pinTrig, OUTPUT);
pinMode(pinEcho, INPUT);
pinMode(ledHijau, OUTPUT);
pinMode(ledMerah, OUTPUT);
pinMode(ledBiru, OUTPUT);
myservo.attach(3);
dht.begin();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" - BINSENSE -");
lcd.setCursor(0, 1);
lcd.print(" -SYSTEM READY-");
delay(2000);
}
void loop()
{
long duration, distance;
digitalWrite(pinTrig, LOW);
delayMicroseconds(2);
digitalWrite(pinTrig, HIGH);
delayMicroseconds(10);
digitalWrite(pinTrig, LOW);
duration = pulseIn(pinEcho, HIGH);
distance = duration / 58.2;
if (distance <= 15)
{
bukatutup();
float humidity = dht.readHumidity(); // Baca nilai kelembapan
if (humidity > threshold)
{
ledorganik();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" - BINSENSE -");
lcd.setCursor(0, 1);
lcd.print("Organik");
delay(1000);
}
else
{
lednonorganik();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" - BINSENSE -");
lcd.setCursor(0, 1);
lcd.print("Non-Organik");
delay(1000);
}
}
else
{
bukatutup();
ledtidakterdeteksi();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" - BINSENSE -");
lcd.setCursor(0, 1);
lcd.print("Tidak Terdeteksi");
delay(1000);
}
}