Exit from for function after detecting obstacles #include &lt;HCSR04.h&gt;

int red = 13;
int green = 12;
int blue = 11;
int trigger = 9;
int echo = 8;
int halt;

void setup(){
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(trigger, OUTPUT);
  pinMode(echo, INPUT);
  Serial.begin(9600);
}

void redLed(){
  rgb(255,0,0);
  delay(200);
}

void rgb(int r, int g, int b){
  analogWrite(red, r);
  analogWrite(green, g);
  analogWrite(blue, b);
}

void fadeBlu(){
  
    for (int i=0; i&lt;=255; i+=5){
    rgb(0,0,i);
    delay(40);

    Serial.print(&quot;halt in IF is &quot;);
    Serial.println(halt);
    }
    
    for(int i=255; i&gt;=0; i-=5) {
      rgb(0,0,i);
      delay(40);

      Serial.print(&quot;halt ELSE is &quot;);
      Serial.println(halt);
    } 
}

void human() {
  
  UltraSonicDistanceSensor distanceSensor(trigger, echo);
  double distance = distanceSensor.measureDistanceCm();
  halt = 0;

  // cover sensor
  if (distance&lt;=30) {
    halt=0;
    redLed();
  }
  // no cover
    halt=-1;
    fadeBlu();
}

void loop(){
  human();
}

I'm looking for a way to exit from &quot;for&quot; loop of fadeBlue() function if the hand is detected in front of sensor. I would like that in any case of fading and if there is hand in front of sensor the led became red. In my code the led doesn't stay red if I had hand in front of the sensor and also the value of &quot;halt&quot; doesn't change. I put some serial.println to make debugging.
First, try to avoid duplication of code, so make a sub function (I slightly changed the print statement, but I assume it's only for debugging):
void fadeBlu()
{
    fadeBlueCycle(0, 255, 5);
    fadeBlueCycle(255, 0, -5);
}

void fadeBlueCycle(uint8_t start, uint8_t end, int8_t step)
{
    for (int i = start; i != end; i += step)
    {
        rgb(0, 0, i);
        delay(40);

        Serial.print(&quot;halt in step &quot;);
        Serial.print(step);
        Serial.println(halt);
    }
}

Now to break from the for, you can use break, or simply return. It's not a good practice to have multiple return statements in one function, thus put it at the end, and use a break. Also, some people don't like a break and prefer using a `while. This would look like:
void fadeBlueCycle(uint8_t start, uint8_t end, int8_t step)
{
    uint8_t i = start;
    bool condition = true;

    while ((i != end) &amp;&amp; condition) // or !condition, depending on what you prefer
    {
        rgb(0, 0, i);
        delay(40);

        Serial.print(&quot;halt in step &quot;);
        Serial.print(step);
        Serial.println(halt);

        condition = `some condition`;
        i += step;
    }
}