// SENSOR BANJIR
// Pin dan variabel sensor HC-SR04
const int trigPin = 13;
const int echoPin = 12;
// variabel jarak
long durasi;
int jarak;
// Library dan Address LCD
#include <Wire.h> // Library komunikasi I2C
#include <LiquidCrystal_I2C.h> // Library modul I2C LCD
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
// Pin Buzzer
// pin LED
int ledMerah = 8;
int ledKuning = 9;
int ledHijau = 10;
void setup() {
// Pinmode sensor HC-SR04
pinMode(trigPin, OUTPUT); // trigPin sebagai Output
pinMode(echoPin, INPUT); // echoPin sebagai Input
// inisialisasi LCD:
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Status Banjir:");
// Buzzer
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
durasi = pulseIn(echoPin, HIGH);
// Calculating the distance
jarak = durasi * 0.034 / 2;
if (jarak<50){
lcd.setCursor(0,1);
lcd.print("BAHAYA ");
digitalWrite(ledMerah, HIGH);
digitalWrite(ledKuning, LOW);
digitalWrite(ledHijau, LOW);
}
if ((jarak>50)&&(jarak<250)){
lcd.setCursor(0,1);
lcd.print("WASPADA ");
digitalWrite(ledMerah, LOW);
digitalWrite(ledKuning, HIGH);
digitalWrite(ledHijau, LOW);
}
if (jarak>250){
lcd.setCursor(0,1);
lcd.print("AMAN ");
digitalWrite(ledMerah, LOW);
digitalWrite(ledKuning, LOW);
digitalWrite(ledHijau, HIGH);
}
else{
lcd.setCursor(0,1);
lcd.print("");
digitalWrite(ledMerah, LOW);
digitalWrite(ledKuning, LOW);
digitalWrite(ledHijau, LOW);
}
}