#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);  // Alamat I2C LCD dapat berbeda, ganti dengan alamat yang sesuai
//ULTRASONIK
const int trigPin = 2;  // Pin Trigger sensor ultrasonik
const int echoPin = 4;  // Pin Echo sensor ultrasonik
const int led1Pin = 5;
const int led2Pin = 6;
//PIR
const int PirPin = 7;

void setup() {
  lcd.init();
  lcd.backlight();
// HC-SR04
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
//PIR
  pinMode(PirPin, INPUT);
}

void loop() {
//ULTRASONIK
 long duration, jarak;
  
  // Mengirim pulse ultrasonik
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Menerima waktu perjalanan gelombang ultrasonik
  duration = pulseIn(echoPin, HIGH);
  
  // Menghitung jarak
  jarak = duration * 0.0342 / 2;

  //menampilkan pada lcd
  lcd.setCursor(0, 1);
  lcd.print("Jarak : ");
  lcd.print(jarak);
  lcd.setCursor(12, 1);
  lcd.print("Cm");
  if(jarak >50){
    digitalWrite(led1Pin, HIGH);
    digitalWrite(led2Pin, LOW);
  }
  else if (jarak <50){
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, HIGH);
  }
  delay(1000);
//PIR
if(digitalRead(PirPin)==HIGH){
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("ADA GERAKAN");
  delay(500);
}
else if(digitalRead(PirPin)==LOW){
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("AMAN");
  delay(500);
}

}