#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Pines para sensores ultrasónicos
#define TRIG1 6
#define ECHO1 5
#define TRIG2 8
#define ECHO2 7
#define TRIG3 10
#define ECHO3 9
// Inicialización LCD I2C (dirección 0x27, 16x2)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Variables de tiempo
unsigned long detectionTime1 = 0;
unsigned long detectionTime2 = 0;
unsigned long detectionTime3 = 0;
const unsigned long detectionThreshold = 5000; // 5 segundos
void setup() {
// Inicializar sensores
pinMode(TRIG1, OUTPUT); pinMode(ECHO1, INPUT);
pinMode(TRIG2, OUTPUT); pinMode(ECHO2, INPUT);
pinMode(TRIG3, OUTPUT); pinMode(ECHO3, INPUT);
// Inicializar LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Sistema iniciado");
delay(2000);
lcd.clear();
// Comunicación Serial opcional
Serial.begin(9600);
}
float getDistance(int trigPin, int echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
return duration * 0.034 / 2;
}
void loop() {
unsigned long currentTime = millis();
float d1 = getDistance(TRIG1, ECHO1);
float d2 = getDistance(TRIG2, ECHO2);
float d3 = getDistance(TRIG3, ECHO3);
Serial.print("D1: "); Serial.print(d1);
Serial.print(" D2: "); Serial.print(d2);
Serial.print(" D3: "); Serial.println(d3);
// Comprobar si cada sensor detecta algo cerca
if (d1 < 5) {
if (detectionTime1 == 0) detectionTime1 = currentTime;
} else {
detectionTime1 = 0;
}
if (d2 < 5) {
if (detectionTime2 == 0) detectionTime2 = currentTime;
} else {
detectionTime2 = 0;
}
if (d3 < 5) {
if (detectionTime3 == 0) detectionTime3 = currentTime;
} else {
detectionTime3 = 0;
}
lcd.setCursor(0, 0);
// Evaluar estados con prioridad al más lleno
if (detectionTime3 != 0 && currentTime - detectionTime3 >= detectionThreshold) {
lcd.print("100% PAPELERA LLENA");
} else if (detectionTime2 != 0 && currentTime - detectionTime2 >= detectionThreshold) {
lcd.print("65% de basura ");
} else if (detectionTime1 != 0 && currentTime - detectionTime1 >= detectionThreshold) {
lcd.print("25% de basura ");
} else {
lcd.print("Papelera vacia ");
}
delay(200);
}