#include <LiquidCrystal_I2C.h>
#define echoPin 2 // define = membuat konstanta
#define trigPin 3 

long duration; //long = menampung angka yg lbh besar
int distance; //menyimpan jarak
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  lcd.init();
  lcd.backlight();
  lcd.println("Sensor HC-SR04"); //hny utk menampilkan tulisan
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2; //rumus menghitung jarak
  lcd.setCursor(0,1);
  lcd.print("Distance: ");
  lcd.print(distance);
  lcd.println(" cm");
}