// Including the LiquiCrystal library
#include <LiquidCrystal.h>
// Assigning PINs for LCD to print data
const int rs = 12;
const int en = 11;
const int d4 = 5;
const int d5 = 4;
const int d6 = 3;
const int d7 = 2;
// Assigning PIN to PhotoResistor to read values
const int photoresistorPin = A0;
//Assinging LED PINs
const int ledWarm = 9;
const int ledCold = 8;
// defining "lcd" as LiquidCrystal object with its constructor
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// For values for LUX calculation
const float GAMMA = 0.7;
const float RL10 = 50;
// Condition-LUX pairs
const int twilight = 10;
const int fullDaylight = 10000;
// Variable to set lightning management mode:
bool lightningAuto = true;
// Assigning button reading PIN
const int buttonPin = 6;
// Default setup when the defice starts up
void setup() {
// Determining the LCD display dimensions (16 columns, 2 rows)
lcd.begin(16, 2);
// Initializing the analog communication for photoresistor
analogReference(DEFAULT);
// Initializing LED pins and their default state
pinMode(ledWarm, OUTPUT);
digitalWrite(ledWarm, LOW);
pinMode(ledCold, OUTPUT);
digitalWrite(ledCold, LOW);
// Initializing Button pin to be input
pinMode(buttonPin, INPUT);
}
// Looping code from device start until shutdown
void loop() {
// Reading the photoresistor sensor value
int sensorValue = analogRead(photoresistorPin);
// If button pressed then switch auto/always off light management
if( digitalRead(buttonPin) == 0 ){
lightningAuto = !lightningAuto;
}
//Converting the analog value to LUX (illumination)
float voltage = sensorValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
// Clearing the previous values at the beginning of every loop
lcd.clear();
// Printing out illumination in LUX in the 1st row:
lcd.setCursor(0, 0);
lcd.print("LUX: ");
lcd.print(lux);
// Printing out lightning management mode (Auto / always OFF):
lcd.setCursor(0,1);
lcd.print("Mode: ");
if( lightningAuto ){
lcd.print("AUTO");
} else {
lcd.print("Always OFF");
}
// if the management mode is AUTO then manage lights
if(lightningAuto){
// manage lights based on LUX level
if ( lux < twilight ) {
digitalWrite(ledCold, HIGH);
digitalWrite(ledWarm, LOW);
} else if ( lux >= twilight && lux < fullDaylight) {
digitalWrite(ledCold, LOW);
digitalWrite(ledWarm, HIGH);
} else {
digitalWrite(ledCold, LOW);
digitalWrite(ledWarm, LOW);
}
} else {
// if auto management is off then no light works
digitalWrite(ledCold, LOW);
digitalWrite(ledWarm, LOW);
}
// "Stopping" the loop for 0.25 second
delay(250);
}