#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd (0x27, 16, 2);
const int trigPin = 6;
const int echoPin = 7;
int buzzer = 8;
long duration;
int jarakCm;
int jarakInch;
void setup () {
lcd.begin(12,2);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn('echoPin', HIGH);
jarakCm = duration*0.034/2;
jarakInch = duration*0.0133/2;
lcd.setCursor (0,0);
lcd.print("Jarak: ");
lcd.print(" cm ");
delay(10);
lcd.setCursor(0,1);
lcd.print("JarakInch: ");
lcd.print(" inch ");
delay(10);
if (jarakCm <=5) {
tone(buzzer, 1030);
delay(400);
noTone(8);
delay(100);
}
}