#include <LiquidCrystal.h>
const float GAMMA = 1.2;
const float RL10 = 20.0;
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
lcd.begin(16, 2);
pinMode(2, OUTPUT);
}
void loop()
{
// Convert the analog value into lux value:
int analogValue = analogRead(A0);
float voltage = float(analogValue) / 1024.0 * 5.0;
float resistance = 2000.0 * voltage / (1.0 - voltage / 5.0);
float lux = pow(RL10 * 1e3 * pow(10.0, GAMMA) / resistance, (1.0 / GAMMA));
if(isfinite(lux))
{
if(lux >= 20.00)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Light!");
digitalWrite(2, LOW);
}
else
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Dark!");
digitalWrite(2, HIGH);
}
}
else
{
Serial.println("Too bright to measure");
}
delay(1000);
}