#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.
breakout of the loop?if( [some-condition] ) return;millis()). E.g.rgb(0,0,(millis()/40)%256);