#include <LiquidCrystal_I2C.h>
#define LED 2
int pinPir = 3;
int pirState = LOW;
int val = 0;
const float GAMMA = 0.7;
const float RL10 = 50;
LiquidCrystal_I2C lcd (0x27, 20, 4);
void setup(){
pinMode(LED, OUTPUT);
pinMode(pinPir, INPUT);
lcd.init();
lcd.backlight();
Serial.begin(9600);
}
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));
val = digitalRead(pinPir);
lcd.setCursor(2, 0);
lcd.print("Cahaya: ");
if(lux > 50){
lcd.print("Terang!");
digitalWrite(LED, LOW);
} else {
lcd.print("Gelap ");
digitalWrite(LED, HIGH);
if(val == HIGH){
if(pirState == LOW){
Serial.println("Gerakkan Terdeteksi");
pirState = HIGH;
}
} else if (pirState == HIGH) {
Serial.println("Tidak Ada Gerakkan!");
pirState = LOW;
}
}
lcd.setCursor(0, 1);
lcd.println("Lux :");
lcd.print(lux);
}