#include <LiquidCrystal_I2C.h>
#define LDR_PIN A0
// LDR Characteristics
const float GAMMA = 0.7;
const float RL10 = 50;
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
pinMode(LDR_PIN, INPUT);
lcd.init();
lcd.backlight();
}
int getLightState(float lux) {
if (lux <= 50 ) {
return 0;
} else if (lux <= 200) {
return 1;
} else if (lux <= 400) {
return 2;
} else {
return 3;
}
}
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));
int currentState = getLightState(lux);
lcd.setCursor(0, 0);
lcd.print("Room: ");
if (currentState == 0) {
lcd.print("Dark ");
}
else if (currentState == 1) {
lcd.print("Low ");
}
else if (currentState == 2) {
lcd.print("Dim ");
}
else if (currentState == 3) {
lcd.print("Bright ");
}
lcd.setCursor(0, 1);
lcd.print("Lux: ");
lcd.print(lux);
lcd.print(" ");
delay(10);
}