breaking a while loop with irrecv.decode

I am trying to use IRremote control LED mode

this is a test code

I hope when it receives the first signal irrecv.decode(&results) . it can run in the while(1) loop ,and when it get another signal. it can break the while(1) loop to go the another while(1) loop.

(because I bulid 5 different light mode ,and i want when i give it a signal , it can run into one of the mode and continue execute it ,before reading the other signal.)

(For example I bulid a breathe light mode and a Flow light mode. i hope when i give it a signal , It can run the breathing light mode and continue breathing light mode ,and when i give it another signal it can switch to Flow light mode and continue the Flow light mode)

#include <IRremote.h> //引入函數庫
IRrecv irrecv(2); //將紅外線腳位設定為2
decode_results results; //接收到的紅外線訊號值

void setup() {
Serial.begin(9600);
irrecv.enableIRIn(); // 啟用紅外線接收
Serial.println(“Enabled IRin”); //印出啟動紅外線完成
}

void loop() {
if (irrecv.decode(&results)){ //如果接收到紅外線
long value = results.value;
if ( value == 924466310){
while (1){
Serial.println(“1”);
irrecv.resume();
if( value != 924466310){
break;
}
}

}

if ( value == 1319256238){
while (1){
Serial.println(“2”);
irrecv.resume();
if( value != 1319256238){
break;
}
}
}
}
}

godzilla_IR_test.ino (965 Bytes)

Please follow the advice on posting a programming question given in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here

You can't break a while (1) loop, that's kind of the point. You need to replace the (1) with a condition that becomes false when you want to break it.

Better still would be to not use a while loop at all but let loop do the looping.

There are various tutorial on here such as how to do several things at once and using millis for timing that explain the principles.

Thanks for your reply

this is the full code. i can use this code control the led mode, but it just do once when i click the button once, but i want when i click IR remote control once, it can continue loop the led mode

godzilla_contral_test.ino (11.7 KB)

boss45683968:
Thanks for your reply

this is the full code. i can use this code control the led mode, but it just do once when i click the button once, but i want when i click IR remote control once, it can continue loop the led mode

Please can you read the forum guidelines Bob linked to, especially the bit about posting code.

There are tutorials about what you are trying to do, it would help if you read them, learned from them then asked questions when you got stuck. Have a look through Useful links and do the tutorials.

You don't need the while, the loop does that for you.

void loop() {
      while (irrecv.decode(&results)) 
      { //如果接收到紅外線

You can use

void loop() {
      irrecv.decode(&results)
      //如果接收到紅外線

If it is not staying on the one mode when no button is pressed you need to put some debug println statements in to find out what results.value is. My guess (and purely a guess) is that if no button is pressed it returns a default value and not the last value you want.

Please try and put your code in code sections as above and like others have asked.

You will NEVER break out of this loop because you entered the loop with value = 924466310 but you never change value within the loop!

              while (1){
              Serial.println("1");
              irrecv.resume();
              if( value != 924466310){
                  break;
                 }
              }

PerryBebbington:
You can't break a while (1) loop, that's kind of the point.

https://www.tutorialspoint.com/cplusplus/cpp_break_statement.htm

gfvalvo:
C++ break statement - Tutorialspoint

I know.
I did consider whether I should state that saying it's not possible is not strictly true. I decided not to because I consider it bad practice because there is always (usually?) a better way to write code without the while loop and I wanted to keep things simple. I wondered how long it would be before someone pointed out that break can be used.

consider it bad practice because there is always (usually?) a better way to write code without the while loop

agreed, something like this would be better already.

 long value = results.value;
           while ( value == 924466310){
              Serial.println("1");
              irrecv.resume();
              value = results.value;
              }

Deva_Rishi:
agreed, something like this would be better already.

 long value = results.value;

while ( value == 924466310){
              Serial.println("1");
              irrecv.resume();
              value = results.value;
              }

That still won't work unless you perform a irrecv.decode(&results) again.

i change code to this but it still can’t break out

#include <IRremote.h> //引入函數庫
IRrecv irrecv(2); //將紅外線腳位設定為2
decode_results results; //接收到的紅外線訊號值

void setup() {
Serial.begin(9600);
irrecv.enableIRIn();  // 啟用紅外線接收
Serial.println("Enabled IRin"); //印出啟動紅外線完成
}

void loop() {
      if (irrecv.decode(&results)){ //如果接收到紅外線
       long value = results.value;
           while ( value == 924466310){
              Serial.println("1");
              delay (500);
              irrecv.resume();
              if (irrecv.decode(&results)){
              value = (results.value);
              }
              Serial.println(value);
              
              if (value != 924466310){
                break;
              }
           }
          
            while ( value == 1319256238){
                 Serial.println("2");
                 delay (500);
                 irrecv.resume();
                 if (irrecv.decode(&results)){
                 value = results.value;
                 }
                 Serial.println(value);
                if (value != 1319256238){
                break;
                 }
                }
             }
}

and i tryed to test this code it can run once [while ( value == 924466310)] then break out , to run [while ( value == 1319256238)] loop

void loop() {
      if (irrecv.decode(&results)){ //如果接收到紅外線
       long value = results.value;
           while ( value == 924466310){
              Serial.println("1");
              delay (500);
              irrecv.resume();
              value = (results.value - (924466310-1319256238));
              Serial.println(value);
              if (value != 924466310){
                break;
              }
           }
           
          
            while ( value == 1319256238){
                 Serial.println("2");
                 delay (500);
                 irrecv.resume();
                 if (irrecv.decode(&results)){
                 value = results.value;
                 }
                 Serial.println(value);
                if (value != 1319256238){
                break;
                 }
                }
             }

}

so i think does [if (irrecv.decode(&results))] can’t run in the while loop?

Why not structure it to use the loop only rather than all the differnt while loops. Something more along the lines of the section below. For each value there would be an else if block and then it would loop round and if value has changed enter a different section again to process.

void loop() {
      if (irrecv.decode(&results)){ //如果接收到紅外線
       long value = results.value;
       Serial.println(value);
}
           if ( value == 924466310){
              Serial.println("1");
              delay (500);
              irrecv.resume();
              }

because use if-else if , when i click once it just can run once . but i need to ,when i click once ,it can go loop

This is the if-else if code I used

#include <IRremote.h> //引入函數庫
IRrecv irrecv(2); //將紅外線腳位設定為2
decode_results results; //接收到的紅外線訊號值

void setup() {
Serial.begin(9600);
irrecv.enableIRIn();  // 啟用紅外線接收
Serial.println("Enabled IRin"); //印出啟動紅外線完成
}


void loop() {
      if (irrecv.decode(&results)){ //如果接收到紅外線
       long value = results.value;
       Serial.println(value);
     
           if ( value == 924466310){
              Serial.println("1");
              delay (500);
              irrecv.resume();
              }

          else if ( value == 1319256238){
               Serial.println("2");
              delay (500);
              irrecv.resume();
              }
          }
}

What was the output on the serial ? There is a button to allow you to copy it in the IDE so you can paste it here.

In my example if the decode fails the path still goes through the if .. else if but in your code if the decode fails it will run only once.

That still won't work unless you perform a irrecv.decode(&results) again.

Yeah probably, if you say so, but really that wasn't the point. Your response was expected and appreciated.//接收到的紅外線訊號值It's these things that really clarify things for me.