[cpp] dfe
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- #include <NewPing.h>
- #include <LiquidCrystal_I2C.h> //memanggil library LCD
- #include <Servo.h> //memanggil library servo
- #define led1 6
- #define led2 7
- LiquidCrystal_I2C lcd(0x27, 20, 4);
- Servo servo;
- const int trigPin1 = 9;
- const int echoPin1 = 10;
- const int trigPin2 = 11;
- const int echoPin2 = 12;
- // Definisikan objek sensor ultrasonik dengan NewPing
- NewPing sonar1(trigPin1, echoPin1);
- NewPing sonar2(trigPin2, echoPin2);
- int distanceHand;
- int distanceTrash;
- const int maxHeight = 100;
- String lastStatus = "Tertutup"; // Mengasumsikan posisi awal servo adalah tertutup
- int lastCapacityPercent = -1; // Nilai awal kapasitas tong sampah belum di ketahui
- void setup() {
- servo.attach(3);
- lcd.init();
- lcd.backlight();
- pinMode(led1, OUTPUT);
- pinMode(led2, OUTPUT);
- servo.write(0); // Pastikan servo dalam posisi tertutup pada awal
- lcd.setCursor(0, 0);
- lcd.print("Status: Tertutup");
- }
- void loop() {
- distanceHand = sonar1.ping_cm();
- String currentStatus = (servo.read() != 0) ? "Terbuka" : "Tertutup";
- // Deteksi tangan dan buka/tutup servo hanya jika perlu
- if (distanceHand > 1 && distanceHand < 10 && currentStatus != "Terbuka") {
- servo.write(90);
- currentStatus = "Terbuka";
- } else if ((distanceHand < 1 || distanceHand > 10) && currentStatus != "Tertutup") {
- servo.write(0);
- currentStatus = "Tertutup";
- }
- distanceTrash = sonar2.ping_cm();
- int capacityLeftPercent = calculateCapacity(distanceTrash);
- // Perbarui LCD hanya jika ada perubahan
- if (currentStatus != lastStatus || lastCapacityPercent != capacityLeftPercent) {
- lcd.clear();
- lcd.setCursor(1, 1);
- lcd.print("Status:");
- lcd.print(currentStatus);
- lcd.setCursor(1, 2);
- if (capacityLeftPercent <= 20) {
- lcd.print("Kapasitas: FULL");
- digitalWrite(led1, LOW);
- digitalWrite(led2, HIGH);
- } else {
- lcd.print("Kapasitas sisa:");
- lcd.print(capacityLeftPercent);
- lcd.print("%");
- digitalWrite(led1, HIGH);
- digitalWrite(led2, LOW);
- }
- lastStatus = currentStatus;
- lastCapacityPercent = capacityLeftPercent;
- }
- delay(1000); // Kurangi atau sesuaikan delay ini sesuai kebutuhan
- }
- int calculateCapacity(int distance) {
- int heightFilled = maxHeight - distance;
- return 100 - ((heightFilled * 100) / maxHeight);
- }
Editor
You can edit this paste and save as new: