Mystery of escaping a while loop?

I have written this function that I have isolated into an infinite while loop. I have no idea how but somehow it is able to escape the loop (while(pulse_complete == false)) without the condition being met!? I must be doing something very obviously daft! However, I cannot see it.

Any suggestions?

  while(1){
    PulseLEDsBlue();
  }
void PulseLEDsBlue(){
  
  unsigned long timer = millis();  
  uint8_t intensity = 99;
  uint8_t charge_up = false;
  pulse_complete = false;
  Serial.println("point 2.0 ");
  while(pulse_complete == false){
    Serial.print("point 2.1 ");
    Serial.println(intensity);
    pulse_complete = true;
    Serial.println(pulse_complete);
    if(millis()-timer>delay_pulsing){
      for(uint8_t i = 0; i<NUM_LEDS_SIDE; i++){
        LedsSide[i] = LedsSide[i].setRGB(blue_r_ring_d[intensity],blue_g_ring_d[intensity],blue_b_ring_d[intensity]);
      }
      for(uint8_t i = 0; i<NUM_LEDS_RING ; i++){
        LedsRingR[i] = LedsRingR[i].setRGB(blue_r_ring_d[intensity],blue_g_ring_d[intensity],blue_b_ring_d[intensity]);
        LedsRingL[i] = LedsRingL[i].setRGB(blue_r_ring_d[intensity],blue_g_ring_d[intensity],blue_b_ring_d[intensity]);
      }
      if(intensity>=99){
        Serial.println("point 2.2");
        Serial.println(pulse_complete);
        pulse_complete = true;
      } else {
        Serial.println("point 2.3");
        Serial.println(pulse_complete);
        pulse_complete = false;
      }
      if(intensity==0){
        Serial.println("point 2.4");
        Serial.println(pulse_complete);
        charge_up = true;
      }
      if(intensity>=99){
        Serial.println("point 2.5");
        Serial.println(pulse_complete);
        charge_up = false;
      }
      if(charge_up){
        Serial.println("point 2.6");
        Serial.println(pulse_complete);
        intensity++;
      }
      if(!charge_up){
        Serial.println("point 2.7");
        Serial.println(pulse_complete);
        intensity--;
      }
      FastLED.show();
      timer = millis();
    }
  }
}

The output from the serial looks like this.

point 2.0
point 2.1 99
1
point 2.2
1
point 2.5
1
point 2.7
1

And then keeps repeating.

What is the code supposed to do?

What would you like/expect to happen?
Maybe a cause of your problem: variables declared within a function will be lost at the end of the function.
If you want them to persist, you should declare them static or make them global.

Also there is a statement pulse_complete= true; in your function body...

It just cycles through led brightness level from the end of an array to the start of it and then back up again. The issue is that it somehow escapes and I cannot see why it would meet the condition


while(pulse_complete == false)

on its time going through the code and escapes immediately.

A while executes while the condition is true,
if it does not meet the condition, it will never enter the loop.

Hey, Build_1971 I don't understand why the loop isn't looping? the Serial output just repeats as if it just escapes the while loop without pulse_complete being true.

Here is an extended output.

point 2.0
point 2.1 99
1
point 2.2
1
point 2.5
1
point 2.7
1
point 2.0
point 2.1 99
1
point 2.2
1
point 2.5
1
point 2.7
1
point 2.0
point 2.1 99
1
point 2.2
1
etc...

There's no loop because pulse_complete is immediately set to true.

Yes, I was hoping that pulse_complete= true; would keep it inside the while loop. when the Serial is printing 1 it is confirming that it is true however it still manages to escape the loop as if it was false. which it isnt.

It won't because you're immediately setting it to true again

At this point intensity is 99

Those lines of '1' in your output is "pulse_complete being true". You set it to true at the top of the loop, just before printing the value after "point 2.1". At "point 2.7" you show that the value is still 'true' (1) just before the bottom of the 'while' loop so the condition is tested and the 'while' loop ends.

Sorry for wasting your time guys and thank you very much for the help. It was so obvious :frowning: