/* ALAT SORTIR BARANG */
//MUHAMMAD FAUZI AKBAR//
//KELAS X TE A

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);
int trig=9;
int echo=2;
long jarak,waktu;
int ledmerah=8;
int pbmerah=7;
int statuspb;
int hitungpb=0;
int tinggiMax=200;
int tinggibadan;
int normal=3;




void setup() {
pinMode(ledmerah, OUTPUT);
pinMode(pbmerah, INPUT);
  pinMode (trig, OUTPUT);
pinMode (echo, INPUT);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(3, 0);
  lcd.print("ALAT SORTIR");
  lcd.setCursor(5,1);
  lcd.print("BARANG");
}

void loop() {
  statuspb=digitalRead(pbmerah);
if (statuspb==1){
  hitungpb++;
  delay(300);
}
  if (hitungpb==1)
{
  lcd.setCursor(1, 0);
  lcd.print("LETAKAN BARANG");
  lcd.setCursor(1,1);
  lcd.print("DIBAWAH SENSOR");

}
statuspb=digitalRead(pbmerah);
  if(hitungpb==2)
  {
    lcd.clear();
    delay(300);
  
  digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite (trig, HIGH);
delayMicroseconds (8);
digitalWrite(echo, HIGH);
delayMicroseconds(5);
digitalWrite (trig, LOW);
waktu = pulseIn(echo, HIGH);
jarak = (waktu/2)/29.1;
tinggibadan =  tinggiMax-jarak;
}
if(hitungpb==3)
{
  digitalRead(tinggibadan);
  if (tinggibadan<=normal)
  {
lcd.setCursor(1,0);
lcd.print("TINGGI  BARANG");
lcd.setCursor(5,1);
lcd.print(tinggibadan);
lcd.println("cm");
  }
else{

digitalWrite(ledmerah,HIGH);
lcd.setCursor(1,0);
lcd.print("BARANG TERLALU");
lcd.setCursor(5,1);
lcd.print("TINGGI");
}
}
digitalWrite(ledmerah,LOW);
if(hitungpb==4)
{
  digitalWrite(ledmerah, LOW);
lcd.clear();
hitungpb=0;


}

}