I've been trying to make an led turn on when it is dark and off when it is bright. The threshold at where it turns on or off is changed by using a potentiometer connected to an analog pin. But I've been having a problem with the led flashing on and off when the value of the ldr is close to the threshold value.
int sensorPin = A2; // select the input pin for ldr
int sensorValue = 0; // variable to store the value coming from the sensor
int calibratePin = A3;
int calibrateVal = 0;
int relayPin = 0;
void setup() {
pinMode(relayPin,OUTPUT);
}
void loop() {
sensorValue = analogRead(sensorPin); // read the value from the sensor
calibrateVal = analogRead(calibratePin);
if(sensorValue >= calibrateVal){
digitalWrite(relayPin,HIGH);
delay(10000);
}
else{
digitalWrite(relayPin,LOW);
}
delay(100);
}
At the moment I decided that I could add a 10sec delay to the code so that the led won't flash, but if possible I'd rather use a different method without delaying the code. I've read that a hysteresis could be used, but I don't know how to implement it into my code.