#include <LiquidCrystal_I2C.h>
#define LCD_ADD 0X27
const uint8_t ROWS = 2;
const uint8_t COLS = 16;
const float GAMMA = 0.7;
const float RL10 = 50;
LiquidCrystal_I2C lcd(LCD_ADD, COLS, ROWS);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(11, OUTPUT);
pinMode(13, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int analogValue = analogRead(A0);
Serial.println(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 lux = map(analogValue,0,1024,0,255);
// int lux2 = map(lux1,0,)
analogWrite(11,lux);
// Serial.println("lux:" +String(lux));
// Serial.println(lux1);
if(isfinite(lux)){
if(digitalRead(13) == LOW){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Room : Light");
lcd.setCursor(0,1);
lcd.print("LUX: "+String(lux));
}else {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Room: Dark");
lcd.setCursor(0,1);
lcd.print("LUX: "+String(lux));
// analogWrite(11,lux1);
// digitalWrite(11,HIGH);
}
}else{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Too bright");
}
delay(500);
}