#include <LiquidCrystal_I2C.h>
#define LED 2
#define LED1 4
//LDR
const float GAMMA = 0.7;
const float RL10 = 50;
//PIR
int inputPin = 3;
int pirState = LOW;
int val = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// put your setup code here, to run once:
pinMode(LED1, OUTPUT);
pinMode(inputPin, INPUT);
Serial.begin(9600);
lcd.init();
lcd.backlight();
}
void loop() {
// put your main code here, to run repeatedly:
//PIR
val = digitalRead(inputPin);
if (val == HIGH) {
digitalWrite(LED1, HIGH);
if (pirState == LOW) {
Serial.println("Gerakan Terdeteksi!");
pirState = HIGH;
}
} else {
digitalWrite(LED1, LOW);
if (pirState == HIGH) {
Serial.println("Tidak Ada Gerakan!");
pirState = LOW;
}
}
//LDR & LCD
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);
} else {
lcd.print("Gelap ");
digitalWrite(LED, HIGH);
}
lcd.setCursor(0, 1);
lcd.print("lux: ");
lcd.print(lux);
lcd.print(" ");
delay(100);
}