Variable problem

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

if (LDRReading < Pin3Start - 100){ Explain, please.

if (Setre = HIGH){

Oops

I love code tags, don't you?

Ok, so

if (LDRReading < Pin3Start - 100)

my thinking on this is that if the the LDRReading value drops below 100 (points or whatever you call the 0 -1023 reading from the analog PIN) stored in Pin3Start (my consistent Value ie. the one i want to read and not change(but it wont do that)) then the LED is turned on.

if (Setre == HIGH){

sorry what a plonker.

Thank You, Sorted. Now to get on with the build. :slight_smile: