Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- // Initialize LCD (16x2)
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- // LED Pins
- #define GREEN_LED 8
- #define YELLOW_LED 9
- #define RED_LED 10
- // Ultrasonic Sensor Pins
- #define TRIG_PIN 11
- #define ECHO_PIN 12
- // Buzzer Pin
- #define BUZZER_PIN 13
- // Bin Height (in cm)
- const int BIN_HEIGHT = 30; // Adjust based on the actual bin height
- void setup() {
- // Initialize Serial Monitor
- Serial.begin(9600);
- // Initialize LCD
- lcd.init();
- lcd.init();
- lcd.backlight();
- lcd.setCursor(0, 0);
- lcd.print("Trash Bin Level");
- // Set LED pins as OUTPUT
- pinMode(GREEN_LED, OUTPUT);
- pinMode(YELLOW_LED, OUTPUT);
- pinMode(RED_LED, OUTPUT);
- // Set Buzzer pin as OUTPUT
- pinMode(BUZZER_PIN, OUTPUT);
- // Set Ultrasonic pins
- pinMode(TRIG_PIN, OUTPUT);
- pinMode(ECHO_PIN, INPUT);
- delay(1000);
- }
- void loop() {
- // Measure distance using Ultrasonic sensor
- int distance = getUltrasonicDistance();
- int trashLevel = BIN_HEIGHT - distance; // Calculate filled level
- trashLevel = max(0, min(trashLevel, BIN_HEIGHT)); // Clamp value between 0 and BIN_HEIGHT
- // Calculate percentage of trash
- int percentage = map(trashLevel, 0, BIN_HEIGHT, 0, 100);
- // Display level and percentage on LCD
- lcd.setCursor(0, 0);
- lcd.print("Level: ");
- lcd.print(trashLevel);
- lcd.print(" cm ");
- lcd.setCursor(0, 1);
- lcd.print("Full: ");
- lcd.print(percentage);
- lcd.print("% ");
- // Update LED indicators
- updateLEDs(percentage);
- // Control Buzzer
- if (percentage >= 80) {
- //digitalWrite(BUZZER_PIN, HIGH); // Turn buzzer ON
- tone(BUZZER_PIN, 1000, 500); // Beep at 1000Hz for 500ms
- } else {
- digitalWrite(BUZZER_PIN, LOW); // Turn buzzer OFF
- }
- // Print details to Serial Monitor (for debugging)
- Serial.print("Distance: ");
- Serial.print(distance);
- Serial.print(" cm, Level: ");
- Serial.print(trashLevel);
- Serial.print(" cm, Full: ");
- Serial.print(percentage);
- Serial.println("%");
- delay(500); // Update every 500ms
- }
- // Function to measure distance using HC-SR04 with averaging
- int getUltrasonicDistance() {
- long totalDistance = 0;
- // Take 10 readings and sum them up
- for (int i = 0; i < 10; i++) {
- // Send trigger pulse
- digitalWrite(TRIG_PIN, LOW);
- delayMicroseconds(2);
- digitalWrite(TRIG_PIN, HIGH);
- delayMicroseconds(10);
- digitalWrite(TRIG_PIN, LOW);
- // Measure the duration of the echo pulse
- long duration = pulseIn(ECHO_PIN, HIGH);
- // Calculate distance (in cm)
- int distance = duration * 0.034 / 2;
- totalDistance += distance;
- delay(50); // Small delay between readings to stabilize
- }
- // Calculate the average
- int averageDistance = totalDistance / 10;
- return averageDistance;
- }
- // Function to update LED indicators based on trash percentage
- void updateLEDs(int percentage) {
- if (percentage < 10) {
- // Low level: Green LED ON
- digitalWrite(GREEN_LED, LOW);
- digitalWrite(YELLOW_LED, LOW);
- digitalWrite(RED_LED, LOW);
- } else if (percentage < 50) {
- // Low level: Green LED ON
- digitalWrite(GREEN_LED, HIGH);
- digitalWrite(YELLOW_LED, LOW);
- digitalWrite(RED_LED, LOW);
- } else if (percentage < 80) {
- // Medium level: Yellow LED ON
- digitalWrite(GREEN_LED, HIGH);
- digitalWrite(YELLOW_LED, HIGH);
- digitalWrite(RED_LED, LOW);
- } else {
- // High level: Red LED ON
- digitalWrite(GREEN_LED, HIGH);
- digitalWrite(YELLOW_LED, HIGH);
- digitalWrite(RED_LED, HIGH);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment