#define PIN_RELAIS 3
#define PIN_LDR_DO 2
#define PIN_LDR_AO A0
// These constants should match the photoresistor's "gamma" and "rl10" attributes
const float GAMMA = 0.7;
const float RL10 = 50;
float analogLux;
void setup() {
pinMode(PIN_RELAIS, OUTPUT);
pinMode(PIN_LDR_DO, INPUT);
pinMode(PIN_LDR_AO, INPUT);
Serial.begin(115200);
}
void loop() {
analogLux = leesLuxWaardeVanSensor ();
stuurRelaisAanOpBasisVanLux();
}
void stuurRelaisAanOpBasisVanLux() {
Serial.println(analogLux);
if (analogLux > 100) {
digitalWrite(PIN_RELAIS, HIGH);
} else {
digitalWrite(PIN_RELAIS, LOW);
}
}
float leesLuxWaardeVanSensor () {
// Convert the analog value into lux value:
int analogValue = analogRead(PIN_LDR_AO);
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
return lux;
}