/*
Mehdi Sadraei
Note:
For writing Fade for a LED, the analogWrite(led,9) is used;
Since analogewrite uses 1-225 records, I have to use an "if clause" to
divide regions less than 1 and greater than 1. because LED does not detect (0-1)
voltages and in this region would be off.
*/
// These constants should match the photoresistor's "gamma" and "rl10" attributes
const float GAMMA = 0.7;
const float RL10 = 50;
int led = 9; // the PWM pin the LED is attached to
int Daylight=0;
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
// Convert the analog value into lux value:
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));
if ((0 <lux) && (lux < 445 ))
{
float Daylight = (lux / 100000.)+1;
analogWrite(led, Daylight);
Serial.println(Daylight);
}
else
{
float Daylight = lux / 100000. * 223;
analogWrite(led, analogValue);
Serial.println(Daylight);
}
}