#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // Alamat LCD I2C, jumlah kolom, jumlah baris

const int trigPin = 12; // Pin trigger sensor HCSR04
const int echoPin = 14; // Pin echo sensor HCSR04
const int relayPin = 2; // Pin relay
 
void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(relayPin, OUTPUT);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Sistem Air Otomatis");
  delay(2000);
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;
  
  if (distance >= 7 && distance <= 400) {
    digitalWrite(relayPin, HIGH); // Menghidupkan relay
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("SEDANG MENGISI");
  } else if (distance >= 2 && distance < 7) {
    digitalWrite(relayPin, LOW); // Mematikan relay
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("AIR PENUH");
  }
  
  delay(500);
}