#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <math.h>

#define LDR_PIN A0
#define RELAY_PIN 7

// LDR Characteristics
const float GAMMA = 0.7;
const float RL10 = 50;

LiquidCrystal_I2C lcd(0x27, 20, 4);

void setup() {
  pinMode(LDR_PIN, INPUT);
  pinMode(RELAY_PIN, OUTPUT);

  lcd.init();
  lcd.backlight();
}

void loop() {
  int analogValue = analogRead(LDR_PIN);
  float voltage = analogValue / 1024.0 * 5.0;
  float resistance = 2000 * voltage / (1 - voltage / 5.0);
  float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));

  lcd.setCursor(2, 0);
  lcd.print("Room: ");
  if (lux > 50) {
    lcd.print("Light!");
    digitalWrite(RELAY_PIN, LOW); // Turn off the street lamp (relay inactive)
  } else {
    lcd.print("Dark  ");
    digitalWrite(RELAY_PIN, HIGH); // Turn on the street lamp (relay active)
  }

  lcd.setCursor(0, 1);
  lcd.print("Lux: ");
  lcd.print(lux);
  lcd.print("          ");

  delay(1000); // Delay for stability and to reduce LCD flickering
}
NOCOMNCVCCGNDINLED1PWRRelay Module