IR Remote Version 4. Issues

Good Afternoon,

I had some code to control a device with an IR remote that used to work very well, but not anymore. After a frustrating few hours only receiving 0xFFFFFF codes back, I finally realized that by updating my IDE and libraries, my code would no longer function.

I have tried rolling the IRremote.h library back to older versions but still no luck. I used a guide on IRremote.hpp 4. and can now receive proper codes from the remote and display them in the serial monitor, But I cannot for the life of figure out how to implement these changes in the old code. Any guidance would be much appreciated.

#include <IRremote.h>

#define RECEIVER_PIN 43         // define the IR receiver pin
IRrecv receiver(RECEIVER_PIN);  // create a receiver object of the IRrecv class
decode_results results;         // create a results object of the decode_results class
unsigned long key_value = 0;    // variable to store the key value

void setup() {
  ledOnboardBlink();
  Serial.begin(9600);

  receiver.enableIRIn();
  receiver.blink13(true);
}
void loop (){
if (receiver.decode()) {
    if (results.value == 0xFFFFFFFF) {
      results.value = key_value;
    }
    switch (results.value) {
      case 0xFFA25D:
        Serial.print("Option One Selected - IR Remote");
        Serial.println(key_value);
        if (digitalRead(IRone) == LOW) {
          dispenseOne();
        } else if (digitalRead(IRone) == HIGH) {
          ledOnboardBlink();
        }

        break;

      case 0xFF629D:
        Serial.print("Option Two Selected - IR Remote");
        if (digitalRead(IRone) == LOW) {
          dispenseTwo();
        } else if (digitalRead(IRone) == HIGH) {
          ledOnboardBlink();
        }
        break;
        Serial.print("Option Three Selected - IR Remote");
        if (digitalRead(IRone) == LOW) {
          dispenseThree();
        } else if (digitalRead(IRone) == HIGH) {
          ledOnboardBlink();
        }
        break;

      case 0xFF22DD:
        Serial.print("Option Four Selected - IR Remote");
        if (digitalRead(IRone) == LOW) {
          dispenseFour();
        } else if (digitalRead(IRone) == HIGH) {
          ledOnboardBlink();
        }
        break;
    }
    key_value = results.value;
    receiver.resume();
  }
}

Study this example sketch for library version 4.0 or later.



//IR remote ON/OFF example

//********************************************^************************************************
// Version    YY/MM/DD    Description
// 1.00       21/09/27    Running sketch
//
//

//Install IRremote Version 4 or later

#include <IRremote.h>   // Ver 4 or later <-------<<<<<<<

#define DECODE_NEC

unsigned long irMillis;


//                                   s e t u p ( )
//******************************************************************************************
void setup()
{
  Serial.begin(9600);

  pinMode(13, OUTPUT);  //LED feedback
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9,  OUTPUT);

  //**************************************
  const byte IR_RECEIVE_PIN = 2;

  IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK, USE_DEFAULT_FEEDBACK_LED_PIN);

  //IR_RECEIVE_PIN (UNO) default = 2
  //IrReceiver.begin(IR_RECEIVE_PIN); //receive on pin 2
  //IrReceiver.begin(4);              //receive on pin 4

  //disableLEDFeedback();
  //enableLEDFeedback ();

  //use pin 12, defaults to 13, true/false
  //setLEDFeedback(12,true);

} //END of   setup()


//                                    l o o p ( )
//******************************************************************************************
void loop()
{
  //*******************************************************
  if (IrReceiver.decode())
  {
    //Some code lines to try
    //
    //IrReceiver.printIRResultShort(&Serial);
    //IrReceiver.printIRResultRawFormatted(&Serial, true);
    //IrReceiver.printIRResultMinimal(&Serial);
    //Serial.println(IrReceiver.decodedIRData.command);
    //Serial.println(IrReceiver.decodedIRData.address);
    //Serial.println(IrReceiver.decodedIRData.protocol);    
    //Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX);

    IrReceiver.resume(); // Enable receiving of the next value
  }

  //*******************************************************
  //have we received a new IR code form the remote ?
  if (IrReceiver.decodedIRData.command != 0)
  {
    checkIRcode();
  }

} //END of   loop()


//                                c h e c k I R c o d e ( )
//******************************************************************************************
void checkIRcode()
{
  //RAW code
  Serial.print(IrReceiver.decodedIRData.decodedRawData, HEX);
 
  Serial.print("  ");   
  //command
  Serial.println(IrReceiver.decodedIRData.command);

  switch (IrReceiver.decodedIRData.command)
  {
    //*********************  key #1
    case 22:
      {
        digitalWrite(12, !digitalRead(12));
      }
      break;

    //*********************  key #2
    case 25:
      {
        digitalWrite(11, !digitalRead(11));
      }
      break;

    //*********************  key #3
    case 13:
      {
        digitalWrite(10, !digitalRead(10));
      }
      break;
      
    //*********************  key #4
    case 12:
      {
        digitalWrite(9, !digitalRead(9));
      }
      break;

  }  //END of   switch...case


  //needed to block repeat codes
  IrReceiver.decodedIRData.command = 0;

} //END of   checkIRcode()


You are working on the current key (results.value) but displaying the previous key (key_value).

Try changing:

to:

    if (results.value != 0xFFFFFFFF) {
      key_value = results.value;
    }

You're the man, found my solution in here!

For anyone who may read this after the fact, I didnt bother to find exactly why the outdated code didnt work, I have just replaced it with this above code as an example