Photoresistor, LED loop trouble!

Hi Guys!

I’m still fairly new to programming so I do apologize if this question sounds dumb, but I could really use some advice as to how to go about fixing this issue I’m having with my code. I’ll post it down below. I’ve tried going on multiple forums, references and have been rewriting my code for hours, but I honestly just can’t see where I’m going wrong.

So I’ll start off with what I wanted the code to do! I want to be able to read an input signal from a photo-resistor, and based off of that it would turn on/turn off an LED light. So when it’s dark, the LED turns on, but when it’s light, the LED turns off. I’m fine with that part, that was simple. To step it up a bit, I added some features:

1)when the photo-resistor doesn’t receive light, it turns on the LED and prints out “LED is ON” twice, but after that it just prints out “Stop” until the condition is changed to number 2
2)when the photo-resistor does receive light, it turns off the LED and prints out “LED off”

Here’s my code:

const int ledPin = 13;
const int ldrPin = A0;

void setup() {

Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(ldrPin, INPUT);


}

void loop() {

  int ldrStatus = analogRead(ldrPin);

if(ldrStatus>=800) {
  for(int x=0;x<2;x++){
digitalWrite(ledPin, HIGH);
Serial.println("LED is ON");
delay(3000);
  }  
  while(ldrStatus>=800){
	digitalWrite(ledPin, LOW);
   Serial.println("STOP");
    delay(3000); 
     }
  } 
  if (ldrStatus<800){
  digitalWrite(ledPin, LOW);
Serial.println("LED OFF");
delay(3000);
}
  analogRead(ldrPin);

}

Output:
LDR is DARK, LED is ON
LDR is DARK, LED is ON
STOP
STOP
STOP
STOP
STOP

Basically it keeps outputting “STOP” even if I shine a light on the photo-resistor, so it gets stuck in my While loop. But my question is, shouldn’t it be jumping to my other IF statement that states that the LED is off? I’m just confused as to why it gets stuck in my while loop, because the condition states that ldrStatus has to be greater than or equal to 800. If it becomes less than 800, it should jump to my last IF statement, right?

Again I apologize if this doesn’t make any sense, I can try and clarify some more if anyones confused by my post! I would just like some help as to figuring out what I may be doing wrong.

I’ll also attach a screenshot of the circuit I’m using!

Another method I tried was changing my while statement to an IF statement, and this was my output:

LDR is DARK, LED is ON
LDR is DARK, LED is ON
STOP
LED OFF
LED OFF
LED OFF

It just prints stop once, so I honestly don’t know where I’m going wrong!

If you didn't update the control variable in the while loop, it will never exit.

WHILE blocks the Arduino until the condition changes and your code has no means within the WHILE loop to change the condition.

Try changing the WHILE to IF and see what happens. Allow loop() to do the repetition.

The last line

analogRead(ldrPin);

does nothing useful - the value that is read is discarded.

...R