-1
#include <HCSR04.h>

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<=255; i+=5){
    rgb(0,0,i);
    delay(40);

    Serial.print("halt in IF is ");
    Serial.println(halt);
    }

    for(int i=255; i>=0; i-=5) {
      rgb(0,0,i);
      delay(40);

      Serial.print("halt ELSE is ");
      Serial.println(halt);
    } 
}

void human() {

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

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

void loop(){
  human();
}

I'm looking for a way to exit from "for" 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 "halt" doesn't change. I put some serial.println to make debugging.

tommy
  • 29
  • 6

1 Answers1

0

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;
}

}

Michel Keijzers
  • 12,954
  • 7
  • 40
  • 56
  • 1
    Good answer as usual. Note that the for loop in C/C++ allows you to add additional checks, so you could also write the for loop as for (int i = start; i < end && condition; i += step) and get the same effect as the while loop, while also having the for loop manage the counter. – Duncan C Jun 17 '20 at 14:13
  • @DuncanC True ... and personally I don't like this concept, as it's like a while loop, written as a for loop. But it's personal. I have seen many professional projects using it this way (with the for). Personally I consider a for loop like an unconditional loop where beforehand the number of iterations is known, and a while loop as a conditional loop, where the number of iterations is not known beforehand. The break statement is kind of discussable in this sense. (But everything is better than a goto). – Michel Keijzers Jun 17 '20 at 14:27
  • The condition of the while in my case should be if there is obstacles in front of sensor? – tommy Jun 17 '20 at 14:30
  • That depends on what your requirements are. If you want to break the loop based on some condition, use the condition you need. The while loop will stop when condition is true, or you can use the not (!) operator (thus !condition) to stop thewhile` loop when the condition is false. – Michel Keijzers Jun 17 '20 at 14:33
  • 1
    I noticed that the break happens ONLY after the end of the while loop and not at any time when there is obstacle in front of sensor, why this happens? – tommy Jun 17 '20 at 14:37
  • Assign the condition you implemented to the variable condition and print it... It should change according to what you implemented. – Michel Keijzers Jun 17 '20 at 14:42
  • 1
    No, i can't understand what kind of condition i need there. The only condition that stop the while loop should be something related to the distance from the sensor, (distance<=30) but i have used that on human() function... – tommy Jun 17 '20 at 14:51