#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, A4,A5);
int trigPin = 2;
int echoPin = 3;
long waktu;
int jarak;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(7, OUTPUT);
pinMode(10, OUTPUT);
lcd.init();
lcd.backlight();
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
waktu = pulseIn(echoPin, HIGH);
jarak= waktu*0.034/2;
if (jarak < 133 )
{
lcd.setCursor(0, 0);
lcd.print("Aman");
lcd.setCursor(1, 1);
lcd.print(jarak);
}
else if (jarak < 266 )
{
lcd.setCursor(0, 0);
lcd.print("Sedang");
lcd.setCursor(1, 1);
lcd.print(jarak);
}
else if (jarak > 266 )
{
lcd.setCursor(0, 0);
lcd.print("Bahaya banjir");
lcd.setCursor(1, 1);
lcd.print(jarak);
digitalWrite(7, HIGH);
}
delay(200);
}