#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
long bacaJarak (int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT);
digitalWrite(triggerPin, LOW);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
return pulseIn(echoPin, HIGH);
}
void setup()
{
lcd.init();
lcd.backlight();
lcd.clear();
}
void loop()
{
int cm = 0; //reset cm ke 0
cm = (0.03446 * bacaJarak(8, 7))/2;
Serial.print(cm);
lcd.setCursor(0,0);
lcd.print("Jarak : ");
lcd.print(cm);
lcd.print(" cm");
Serial.println("cm");
delay(100);
}