Please tell me what i have done wrong?

Hello Arduino geniuses.

can you please identify why my loop is stuck and not repeating and i can make it work…

kind regards
lordStark

/*
 * IN THIS SKETCH WE WILL  
 *  1. GET VALUE FOR INFRA RED SENSOR
 *  2. TURN ON RED LED

 */




const int irSensor = A0;
int irValue; 
const int ldSensor = A5;
const int redLed = 10;
const int yellowLed = 13; 
int count = 1;
int countNumber= 5;
String readingir = "The Reading from IR SENSOR #  ";
String ledoffcount = ".......led blink Off count#-  ";
String irledmsg = readingir + ledoffcount; 

void setup ()
{
  Serial.begin (9600);
  pinMode (A0, INPUT);
  pinMode (A1, OUTPUT);
  pinMode (A2, OUTPUT);
  digitalWrite (A2, HIGH);
  digitalWrite (A1, LOW);
  pinMode (A5, INPUT);
  pinMode (redLed, OUTPUT);
  pinMode (yellowLed, OUTPUT); 
} 

void loop () 
{ // start loop
  digitalWrite (yellowLed, HIGH);  
  int val = analogRead (A0);
  Serial.print (readingir);
  Serial.println (analogRead (A0));
  delay (250); 
  
  if (val <=250)
{ digitalWrite (yellowLed, LOW);
  delay (3000);
  Serial.print (readingir);
  Serial.print (analogRead (A0));
  Serial.print (ledoffcount);
  Serial.println (count);
  digitalWrite (yellowLed, HIGH);
  delay (2000);
  count++;

while (count == 4)
{
 for( int j=1; j<=5; j++)
 { Serial.println (j); 
 delay (250); 
 while (j == 5)
 { digitalWrite (yellowLed, LOW);
 delay (1000);
 }
 }
}
}
} // finish loop

In while j==5 j never became different. And more, in while count==4 count never became different, so they are loops. Specially the frst one, in witch nothing changes, and so looks like the program is freezed.
What do you want to do?

       while (j == 5)
        {
          digitalWrite (yellowLed, LOW);
          delay (1000);
        }

Once j equals 5 the while condition will become true. Nothing in the while loop changes the value of j. How will the whole loop ever be exited ?

Perhaps you meant

if (j == 5)

Thank you Silente and UKHeliBob for your quick response.

thanks for identifying the problem in my code.

Silente: You are right. i have not entered an exit code.
what can i write to tell this program that
when count == something,
{ do the following }
and then start from the top again??

im trying to write the code
so that when val < 250
the code runs the light sequence…
but when it reaches count = 4 it exits that and start the next function in the loop…
and
when j ==5, it stops the loop and start from top of the loop…

hope that makes sense…
thank you in advance

Thank you UKHeliBob..

just a quick question.. if i use if count == 4 { do something}
do i need to enter break function;; to exit the loop and start again?

kind regards
LordStark

lordstark:
Thank you UKHeliBob..

just a quick question.. if i use if count == 4 { do something}
do i need to enter break function;; to exit the loop and start again?

kind regards
LordStark

The function loop() executes over and over really fast. So just put in your code. It should not have infinite loops or long delays. You don't need to break out of loop().

I'd recommend studying state-machines and the blinkWithoutDelay example, since this kind
of approach is needed for anything more complex. loop() is intended to be an event loop,
called frequently always, so no part of the sketch is blocked by other parts.

If you want the loop() function to start over at the top, use the statement "return;". That will exit the current run of the function. When loop() exits, it is called again, starting again at the top.