(RESOLVED) Moving if (irrecv.decode(&results)) into function

I'm trying to clean up my code a bit, and move as much repeated code as I can into functions. I have code sprinkled around my loop that looked like this (from the IRremote library for using IR remotes):

if (irrecv.decode(&results))
  {
    translateIR();
    irrecv.resume();
  }

which called the following function:

void translateIR()
{
  switch(results.value)
  {
    case 0xFD00FF: Serial.println(F("Remote button 1 pressed.")); coldvalue = HIGH; break;
    case 0xFD807F: Serial.println(F("Remote button 2 pressed.")); hotvalue = HIGH; break;
    case 0xFD40BF: Serial.println(F("Remote button 3 pressed.")); tempvalue = HIGH; break;
    case 0xFD708F: Serial.println(F("Remote cancel code received.")); cancelcode = 1; break;
    case 0xFD8877: Serial.println(F("Increase temp button pressed.")); increaseTarget(); break;
    case 0xFD9867: Serial.println(F("Decrease temp button pressed.")); decreaseTarget(); break;
    case 0xFDA857: Serial.println(F("Update temp button pressed.")); updateTemp(); break;

    default: Serial.println("Unknown IR signal received"); break;
  }
  delay(500); // do not get immediatly repeated button press
}

I wanted to get rid of the first bit of code and just call translateIR() in the loop so I modified the code as follows:

void translateIR()
{
  if (irrecv.decode(&results))
  {
  switch(results.value)
   {
    case 0xFD00FF: Serial.println(F("Remote button 1 pressed.")); coldvalue = HIGH; break;
    case 0xFD807F: Serial.println(F("Remote button 2 pressed.")); hotvalue = HIGH; break;
    case 0xFD40BF: Serial.println(F("Remote button 3 pressed.")); tempvalue = HIGH; break;
    case 0xFD708F: Serial.println(F("Remote cancel code received.")); cancelcode = 1; break;
    case 0xFD8877: Serial.println(F("Increase temp button pressed.")); increaseTarget(); break;
    case 0xFD9867: Serial.println(F("Decrease temp button pressed.")); decreaseTarget(); break;
    case 0xFDA857: Serial.println(F("Update temp button pressed.")); updateTemp(); break;

    default: Serial.println("Unknown IR signal received"); break;
   }
  delay(500); // do not get quickly repeated button press
  }
  irrecv.resume();
}

so that the decode and resume are part of the function. However, this isn't working and none of the cases are being triggered. I'm guessing this is a simple problem to fix, but I can't seem to figure out what I'm doing wrong. I would greatly appreciate some help here.

are you calling your new translateIR() at every loop ? is the loop fast ? where is results declared?

the   irrecv.resume(); needs to be within the if (irrecv.decode(&results)) {... (are you missing a closing brace)

don't post snippets of code, it's too much guesswork otherwise to provide guidance

Thank you. Moving irrecv.resume(); above the previous bracket below delay(500); fixed this.

good :slight_smile:

if you notice in the initial code, it was only executed once you had dealt with the incoming IR code. if you reset it all the time it's not a great idea