#include <LiquidCrystal.h>
#define trig_PIN 2
#define echo_PIN 3
const int buzzer_PIN = 10;
const int rs = 4;
const int en = 5;
const int d4 = 6;
const int d5 = 7;
const int d6 = 8;
const int d7 = 9;
LiquidCrystal lcd (rs, en, d4, d5, d6, d7);
long int duration;
long int distance;
int hasil;
void setup()
{
lcd.begin(16, 2);
pinMode(trig_PIN, OUTPUT);
pinMode(echo_PIN, INPUT);
pinMode(buzzer_PIN, OUTPUT);
}
void buzerFungsi()
{
delay(hasil);
tone(buzzer_PIN, 10);
delay(hasil);
tone(buzzer_PIN, 0);
delay(hasil);
}
void loop()
{
digitalWrite(trig_PIN, LOW);
delayMicroseconds(2);
digitalWrite(trig_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(trig_PIN, LOW);
duration = pulseIn(echo_PIN, HIGH);
distance = duration/58;
lcd.setCursor(0,0);
lcd.print("Distance: ");
lcd.println(distance);
int dis = distance;
Serial.println(distance);
hasil = dis + 50;
buzerFungsi();
}