#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
int RelePin = 13;
int inputPin = 9;
int pirState = LOW;
int val = 0;
#define SPEAKER_PIN 8
const int photoResistorPin = A0;
const float GAMMA = 0.7;
const float RL10 = 50;
float lux;
void setup() {
pinMode(RelePin, OUTPUT);
pinMode(SPEAKER_PIN, OUTPUT);
pinMode(inputPin, INPUT);
lcd.init();
lcd.backlight();
lcd.clear();
}
void loop() {
int analogValue = analogRead(photoResistorPin);
float voltage = analogValue / 1024.0 * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
lcd.setCursor(0, 0);
lcd.print("Light: ");
lcd.print(lux);
val = digitalRead(inputPin);
if (val == HIGH && lux < 300) {
digitalWrite(RelePin, HIGH);
if (pirState == LOW) {
pirState = HIGH;
}
} else {
digitalWrite(RelePin, LOW);
if (pirState == HIGH) {
pirState = LOW;
}
}
}