0
#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.

  • Did you try to break out of the loop? – Edgar Bonet Jun 17 at 13:28
  • No, i don't know how to use break here.. i have to break in for? – tommy Jun 17 at 13:31
  • if( [some-condition] ) return; – Gerben Jun 17 at 13:36
  • Alternatively, calculate the brightness the blue led should be, based on time (millis()). E.g. rgb(0,0,(millis()/40)%256); – Gerben Jun 17 at 13:39
  • I don't understand how to use millis there.. – tommy Jun 17 at 13:59
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("halt in step ");
        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) && condition) // or !condition, depending on what you prefer
    {
        rgb(0, 0, i);
        delay(40);

        Serial.print("halt in step ");
        Serial.print(step);
        Serial.println(halt);

        condition = `some condition`;
        i += step;
    }
}
| improve this answer | |
  • 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 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 at 14:27
  • The condition of the while in my case should be if there is obstacles in front of sensor? – tommy Jun 17 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 the while` loop when the condition is false. – Michel Keijzers Jun 17 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 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.