loop stopping at if statement

I’m attempting to make an LED strip which is controlled from an IR remote. The problem is, the IR sensor is stopping the loop from cycling. But, in order for the rainbow effect to execute properly I need the loop to continuously cycle. Any recommendations?

IRtest2.ino (623 Bytes)

The first recommendation is to post code as a code-section

I’m very very sure that the loop continues to looping
it might be that some if-conditions stay false or true which appears as the loop had stopped.

recommendation two have you looked inside a demo-code and inside the documenation of the lib?

The thrid recommendation is to
change baudrate to 115200 and add serial debug-output to make visible what is going on

in your case the loop is running very fast so sending serial output in every loop does not make sense
it could even slow down the whole computer to react slow.

so there must be added functionality that let’s run your loop still fast but gives serial output only from time to time.
This can be done with a technique called non-blocking delay

I also added a variable DecodeCount that counts up everytime your if-condition

if (irrecv.decode(&results)) {

evaluates true to see if it evaluates continiously to true

the code compiles but I don’t have ir-hardware handy. This means it is not tested with real hardware

#include <IRremote.h>
const byte IRpin   = 3;  // pin for the IR sensor
const byte LED_Pin = 5;

IRrecv irrecv(IRpin);
decode_results results;


void setup() {
  Serial.begin(115200);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(LED_Pin, OUTPUT);
}


// non-blocking timing in a regular manner. Function gets true after every TimePeriod
boolean TimePeriodIsOver (unsigned long &expireTime, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - expireTime >= TimePeriod )
  {
    expireTime = currentMillis; // set new expireTime
    return true;                // more time than TimePeriod) has elapsed since last time if-condition was true
  } 
  else return false;            // not expired
}

unsigned long MyTimer;

char resumeMsg[32];  // array of char has to be long enough to "take-in" all characters
char HighMsg[32];
char LowMsg[32];
int  DecodeCount = 0;

void loop() {
  if (irrecv.decode(&results)) {
    strcpy(resumeMsg, "resumeMsg TRUE  ");
    DecodeCount++;
    
    irrecv.resume();   // Receive the next value
  }
  else {
    strcpy(resumeMsg, "resumeMsg false  ");
  }

  
  if (results.value == 16769565) {// change according to your IR remote button number  
    strcpy(HighMsg, "  HighMsg TRUE  ");
    digitalWrite(LED_Pin, HIGH);
    delay(100);
  }
  else {
    strcpy(HighMsg, "  HighMsg false  ");    
  }

  
  if (results.value == 16761405) { // change according  to your IR remote button number  
    strcpy(LowMsg, "LowMsg TRUE  ");
    digitalWrite(LED_Pin, LOW);
  }
  else {
    strcpy(LowMsg, "LowMsg false  ");    
  }

  // only once every 500 milliseconds print serial debug-info
  if (TimePeriodIsOver(MyTimer, 500) ) {
    Serial.print(resumeMsg);
    Serial.print(" DecodeCount:");
    Serial.print(DecodeCount);

    Serial.print(HighMsg);
    Serial.print(LowMsg);
  }
}

best regards Stefan

jc0187: The problem is, the IR sensor is stopping the loop from cycling.

are you suggesting that irrecv.decode() is not returning unless there is an IR code received?

how are you determining that loop() is is not running rather than you are not receiving one of the expected codes? you could use serial.println() to show the code being received

gcjr: are you suggesting that irrecv.decode() is not returning unless there is an IR code received?

how are you determining that loop() is is not running rather than you are not receiving one of the expected codes? you could use serial.println() to show the code being received

that is what I have posted as a already pretty advanced version that gives (most) of the information ever possible/useful best regards Stefan