#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
#define trig 5
#define echo 18
#define SOUND_SPEED 0.034
long duration;
float distanceCm;
void setup()
{
Serial.begin(115200);
lcd.init();
lcd.backlight();
lcd.setCursor(4,0);
lcd.print("~POLNES~");
lcd.setCursor(2,1);
lcd.print("~INTALUONES~");
delay(500);
lcd.clear();
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
}
void loop()
{
ultrasonik();
display();
}
void ultrasonik()
{
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distanceCm = duration * SOUND_SPEED/2;
Serial.print("Distance (cm): ");
Serial.println(distanceCm);
delay(10);
}
void display()
{
lcd.setCursor(0,0);
lcd.print("Jarak : ");
lcd.print(distanceCm);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("Status : ");
}