Error in while function + IR receiver

Hello,

For a LARP, I have to create a necklace like Battle Royale, With a IR remote, we can control A RGB LED like as Blue, Green, Red or blinking

in my code, no problem with the IR aquisition an dispatch remote, but on the blinking part I have a problem with my while.

#include <IRremote.h>
 
int RECV_PIN = 8;
int R_PIN = 9;
int G_PIN = 5;
int B_PIN = 11;
boolean running = false;

#define ON                0xFFE01F
#define OFF               0xFF609F
#define BRIGHTNESS_UP     0xFFA05F
#define BRIGHTNESS_DOWN   0xFF20DF
#define FLASH             0xFFF00F
#define STROBE            0xFFE817
#define FADE              0xFFD827
#define SMOOTH            0xFFC837
 
#define RED               0xFF906F
#define GREEN             0xFF10EF
#define BLUE              0xFF50AF
#define WHITE             0xFFD02F
 
#define ORANGE            0xFFB04F
#define YELLOW_DARK       0xFFA857
#define YELLOW_MEDIUM     0xFF9867
#define YELLOW_LIGHT      0xFF8877
 
#define GREEN_LIGHT       0XFF30CF
#define GREEN_BLUE1       0XFF28D7
#define GREEN_BLUE2       0XFF18E7
#define GREEN_BLUE3       0XFF08F7
 
#define BLUE_RED          0XFF708F
#define PURPLE_DARK       0XFF6897
#define PURPLE_LIGHT      0XFF58A7
#define PINK              0XFF48B7

unsigned long rgb = 0;

byte r,g,b;
 
IRrecv irrecv(RECV_PIN);
 
decode_results results;
int currentMillis = 0; 
 
void setup()
{
  irrecv.enableIRIn(); // IR initialisation
  Serial.begin(9600);
  pinMode(R_PIN, OUTPUT);  
  pinMode(G_PIN, OUTPUT);  
  pinMode(B_PIN, OUTPUT);  
}
 
 
void variar (byte* color, char valor) {
    if (valor > 0) {
        if ( *color + valor <= 255) {
            *color += valor;
        } else {
            *color = 255;
        }
    } else {
        if (*color + valor >= 0) {
            *color += valor;
        } else {
            *color = 0;
        }
  }
}

void attendre(int interval) {
    currentMillis = millis();
    Serial.println(interval);
    Serial.println("entrée attendre");
    Serial.println(currentMillis);
    while (millis() - currentMillis < interval) {
      Serial.println(millis() - currentMillis < interval);
      Serial.println("entrée while");
      if (irrecv.decode(&results)) {
            if (results.value != 0xFFFFFFFF) {
            return;
          }
          irrecv.resume(); // Receive the next value
      }
      
    }//end while
}

void STEP1(){
	//initialise boolean
	running = true;
	// enter in a while routine
	while(running){
		//set the first step leds
		RGB(0x00FF00FF);
		//lights the LEDs
		analogWrite(R_PIN,r);
		analogWrite(G_PIN,g);
		analogWrite(B_PIN,b);
		//wait 100ms
		delay (100);
		//set the second step leds
		RGB(0x00FFFFFF);
		//switch of the leds
		analogWrite(R_PIN,r);
		analogWrite(G_PIN,g);
		analogWrite(B_PIN,b);
		//wait 100ms
		delay(100);
		// receive the next value
                Serial.println("test");
		irrecv.resume();
                Serial.println(results.value);
		// if we receive value
		if (irrecv.decode(&results)){ 
                    Serial.println("there is something");
                    Serial.println(results.value);
			//if value is defferent of the FLASH Code
			if ( results.value != 0xFFF00F) {
				running = false;
			}
		}
                else {
                    Serial.println("test2");
                }
	}		
	return;
}

void RGB(unsigned long valor) {
   r = valor >> 16;
   g = (valor >> 8) & 0xFF;
   b = valor & 0xFF;
}
 
void loop() {
  if (irrecv.decode(&results)) {
    if ( results.value != 0xFFFFFFFF) {
      switch (results.value) {
           case OFF :
               RGB(0x00FFFFFF); break;
           case ON :
               RGB(0x00000000); break;
           case RED           : 
               Serial.println("ROUGE");
               RGB(0x0000FFFF); break; 
           case GREEN         : RGB(0x00FF00FF); break; 
           case BLUE          : RGB(0x00FFFF00); break; 
           case WHITE         : RGB(0x00000000); break;
           case ORANGE        : RGB(0x0000AAFF); break; 
           case YELLOW_DARK   : RGB(0x00FFAA00); break;
           case YELLOW_MEDIUM : RGB(0x00FFD400); break;
           case YELLOW_LIGHT  : RGB(0x00FFFF00); break;
           case GREEN_LIGHT   : RGB(0x0000FFAA); break;
           case GREEN_BLUE1   : RGB(0x0000FFFF); break;
           case GREEN_BLUE2   : RGB(0x0000AAFF); break;
           case GREEN_BLUE3   : RGB(0x00FF7F00); break;
           case BLUE_RED      : RGB(0x00000080); break;
           case PURPLE_DARK   : RGB(0x003F0080); break;
           case PURPLE_LIGHT  : RGB(0x007A00BF); break; 
           case PINK          : RGB(0x00FF00FF); break;
           case FLASH         :
                STEP1();
           break;
      }
      Serial.println(results.value, HEX);
      Serial.println(r,DEC);
      Serial.println(g, DEC);
      Serial.println(b, DEC);
      analogWrite(R_PIN,r);
      analogWrite(G_PIN,g);
      analogWrite(B_PIN,b);
    }
    irrecv.resume(); // Receive the next value
  }
}

If you see on Step1, we have this part

		if (irrecv.decode(&results)){ 
                    Serial.println("there is something");
                    Serial.println(results.value);
			//if value is defferent of the FLASH Code
			if ( results.value != 0xFFF00F) {
				running = false;
			}
		}
                else {
                    Serial.println("test2");
                }

but when I run the code on my arduino (Uno V3) it's always prit : test2

I try many things but now I'm Lost
Could you help me please :stuck_out_tongue:

int currentMillis = 0; 
...
void attendre(int interval) {
    currentMillis = millis();
...
    while (millis() - currentMillis < interval) {

millis( ) returns unsigned long. I suggest you fix interval up to match that for a start. Also currentMillis.

Thanks,

for the moment, I am not using this function.
My issue comes from the STEP1 function, I tried many things today, but no one working :cold_sweat:

		irrecv.resume();
                Serial.println(results.value);
		// if we receive value
		if (irrecv.decode(&results)){

"resume" seems to reset ready for a new code. I suggest you only call that once (outside the loop). Otherwise there wasn't enough time for it to receive the next code (in "decode"). So more like this (untested):

void STEP1(){
  //initialise boolean
  running = true;
  irrecv.resume();    // <-------------- resume once
  // enter in a while routine
  while(running){
    //set the first step leds
    RGB(0x00FF00FF);
    //lights the LEDs
    analogWrite(R_PIN,r);
    analogWrite(G_PIN,g);
    analogWrite(B_PIN,b);
    //wait 100ms
    delay (100);
    //set the second step leds
    RGB(0x00FFFFFF);
    //switch of the leds
    analogWrite(R_PIN,r);
    analogWrite(G_PIN,g);
    analogWrite(B_PIN,b);
    //wait 100ms
    delay(100);
    // receive the next value
    Serial.println("test");
    Serial.println(results.value);
    // if we receive value
    if (irrecv.decode(&results)){ 
      Serial.println("there is something");
      Serial.println(results.value);
      //if value is defferent of the FLASH Code
      if ( results.value != 0xFFF00F) {
        running = false;
      }
      irrecv.resume();  // <--- resume after doing a decode
    }
    else {
      Serial.println("test2");
    }
  }		
  return;
}

It's Working !!!

Many thanks !!! :smiley: