#include <LiquidCrystal_I2C.h>
#define LED 2
#define inputPin 5

const float GAMMA = 0.7;
const float RL10 = 50;

LiquidCrystal_I2C lcd(0x27, 16, 2);

int pirState = LOW;
int val= 0;

void sensor_PIR() {
   val = digitalRead(inputPin);
if (val == HIGH){
  digitalWrite(LED, HIGH);
  if (pirState == LOW){
 
    lcd.setCursor(0,1);
    lcd.print ("   Ada gerakan  ");
    pirState = HIGH;
  }
} else {
  digitalWrite(LED, LOW);
  if (pirState == HIGH){
   lcd.setCursor(1,1);
   lcd.print ("Gak ada gerakan");
    pirState = LOW;
      }
     
    }
}


void setup() {
 pinMode(LED, OUTPUT);
 pinMode(inputPin, INPUT);
 lcd.init();
 lcd.backlight();
 lcd.setCursor(2,0);
lcd.print("SENSOR PIR AKAN AKTIF PADA MALAM HARI");
for (int i=0; i<25; i++){
lcd.scrollDisplayLeft();
    delay(500);
}
 lcd.clear();
}

void loop() {
int analogValue=analogRead(A0);
float voltage=analogValue/1024.*5;
float resistance=2000*voltage/(1-voltage/5);
float lux=pow(RL10*1e3*pow(10,GAMMA)/resistance,(1/GAMMA));


if (lux>50){
lcd.setCursor(4,0);
  lcd.print("PIR OFF");
   lcd.setCursor(1,1);
 lcd.print("               ");
  
  }
  else{
  lcd.setCursor(4,0);
  lcd.print("PIR ON ");
  sensor_PIR();   
} 
  }