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

#define ECHO_PIN 13
#define TRIG_PIN 12
void setup(){
	 lcd.init();
  lcd.backlight();
  
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(14, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  for(int pin = 2; pin <12;pin++){
    pinMode(pin, OUTPUT);
  }
}
void loop(){
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  int duration = pulseIn(ECHO_PIN, HIGH);
  float distance = duration * 0.034 / 2;
  int y = (-2E-06*distance*distance) + (1.0068*distance) - 0.036;
  lcd.setCursor(3, 0);
  lcd.print("jarak:");
  lcd.print(y);
  lcd.print(" cm");
  int ledlvl = map(y, 10, 399, 2, 12);
  for(int pin = 2; pin <12;pin++){
    if(pin >= ledlvl){
    digitalWrite(pin, HIGH);
      if(y < 40 ){
        tone(14, 500);
        delay(100);
        noTone(14);
        delay(100);
      }
    }
    else if(pin < ledlvl){
      digitalWrite(pin, LOW);
    }
  }
  delay(10);
}