Hi,
I am trying to read the value from a LDR and compare that reading until it changes. A little like the automatic Nightlight but instead of setting the Analog Value to a set sum (say 400) i want it to read the value on a button press and turn a LED on when the light changes ie gets lighter or darker from when the button was pressed. i would also like a little Le-way in how sensitive it is state the < and > equations in the code.
My problem is that i cannot for the life of me find out how to read the sensor and store that reading in a Variable without it consistently changing (reading the sensor).
The LED comes on for a split second but then goes off again i have now put the "button press" variable on the serial print and i can watch it change as the light changes therefore i have messed up somewhere.
can any one help? i am sure it is simple but i don't know what i am looking for.
My code is below.
Apologies if it is horrendous i am relatively new to this.
int LDR_Pin = A3; //analog pin 3
int Setre = 2; // reset button
int Pin3Start = LDR_Pin; // Start Value for pin 3
void setup(){
Serial.begin(9600);
pinMode(3, OUTPUT);
pinMode(A3, INPUT);
pinMode(2, INPUT);
}
void loop(){
int LDRReading = analogRead(LDR_Pin);
int Setre = digitalRead(13);
if (LDRReading < Pin3Start - 100){
digitalWrite(3, HIGH);
}
else if (LDRReading > Pin3Start + 100) { digitalWrite(3, HIGH);
}
else { digitalWrite(3, LOW);
}
if (Setre = HIGH){
Pin3Start = LDR_Pin;
}
Serial.println(Pin3Start);
delay(500);
}
The LDR is linked to 3.3V and Ground and pin A3 using another resistor. (Voltage divider style)
The Button is linked to Ground to hold it at LOW
LED is a DFRobot 3 wire Componant linked to Pin 2 Ground and 3.3V