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

5
  • Did you try to break out of the loop? Commented Jun 17, 2020 at 13:28
  • No, i don't know how to use break here.. i have to break in for? Commented Jun 17, 2020 at 13:31
  • if( [some-condition] ) return; Commented Jun 17, 2020 at 13:36
  • Alternatively, calculate the brightness the blue led should be, based on time (millis()). E.g. rgb(0,0,(millis()/40)%256); Commented Jun 17, 2020 at 13:39
  • I don't understand how to use millis there.. Commented Jun 17, 2020 at 13:59

1 Answer 1

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;
    }
}
8
  • 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. Commented Jun 17, 2020 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). Commented Jun 17, 2020 at 14:27
  • The condition of the while in my case should be if there is obstacles in front of sensor? Commented Jun 17, 2020 at 14:30
  • 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? Commented Jun 17, 2020 at 14:37
  • 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... Commented Jun 17, 2020 at 14:51

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.