/*
  Tempat Sampah Sensor Versi ke-2!
  Maker: Raffa Azka Ghifari (Kelas 8-A)
  Collab dengan Ryhanna Afiza Desky (Kelas 8-B)

  Peran:
  Raffa Azka Ghifari (Programmer, Circuit Designer)
  Ryhanna Afiza Desky (Designer, Feature Enhancer)
*/

/*
  (!) Circuit Designer's note (!)
  - lihat Google Keep Notes hehehehe -
*/


#include <Servo.h>
Servo servo;

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);


// Variables
int trig = 8;
int echo = 9;
int servoPin = 3;
int angle = 10;
int angleStep = 50;
int x = 0;


float duration_us, distance_cm;

void setup() {
  Serial.begin(9600);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  servo.attach(servoPin);
  servo.write(angle);

  lcd.init();
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("Tempat Sampah Pintar");

}

void loop() {
  measureDistance();
  if (distance_cm < 20) {
    servo.write(180);
    lcd.print(x = x + 1);

  }
  else {
    servo.write(0);
  }

}


void measureDistance() {
  digitalWrite(trig, LOW);
  delayMicroseconds(5);
  digitalWrite(trig, HIGH);
  delayMicroseconds(15);
  digitalWrite(trig, LOW);

  duration_us = pulseIn(echo, HIGH);
  distance_cm = (duration_us/2) / 29.1;
}