#include <LiquidCrystal_I2C.h>
#define Led 2
// LDR Characteristic
const float GAMMA = 0.7;
const float RL10 = 50;
LiquidCrystal_I2C lcd (0x27, 20, 4);
int inputPin = 3;
int pirState = LOW;
int val = 0;
void setup() {
pinMode (Led, OUTPUT);
pinMode (inputPin, INPUT);
lcd.init();
lcd.backlight();
}
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));
lcd.setCursor (2, 0);
lcd.print ("Cahaya :");
if (lux > 50){
lcd.print ("Terang!");
digitalWrite (Led, LOW);
lcd.setCursor (0, 1);
lcd.print("LUX :");
lcd.print(lux);
lcd.setCursor (10, 1);
lcd.print(" ");
}
else{
lcd.print ("Gelap ");
digitalWrite (Led, HIGH);
val = digitalRead (inputPin);
if (val == HIGH)
{
if (pirState == LOW)
{
lcd.setCursor (0, 1);
lcd.print ("Ada Gerakan ");
pirState = HIGH;
}
}
else
{
if (pirState == HIGH)
{
lcd.setCursor (0, 1);
lcd.print ("Tidak ada Gerak ");
pirState = LOW;
}
}
}
}