#include <LiquidCrystal_I2C.h>
#define ECHO_PIN 2
#define TRIG_PIN 3
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(13, OUTPUT);
lcd.init();
lcd.backlight();
//lcd.scrollDisplayleft();
}
void loop() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2); //dilai untuk low
digitalWrite(TRIG_PIN, HIGH); //menembak gelombang
delayMicroseconds(10); //selama 10 ms
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH); //membaca gelombang
float distance = duration * 0.034 / 2; // s = 340*t/2
float jarak = (1E-06*distance*distance) + (1.0055*distance) + 0.0012; //kalibrasi sensor
lcd.setCursor(2, 0); //2 itu adalah jumlah kolom 0 adalah barisnya
lcd.print("Jarak: ");
lcd.print(jarak);
if (jarak < 200){
lcd.setCursor(0, 1);
lcd.print("Hati-hati!");
//lcd.scrollDisplayleft();
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
}
else if (jarak > 200){
lcd.setCursor(0, 1);
lcd.print("jarak aman ");
digitalWrite(10, HIGH);
delay(100);
digitalWrite(10,LOW);
delay(100);
}
else {
lcd.setCursor(0, 1);
lcd.print(" ");
}
delay(100);
}