IR Remote Hex Codes Change between codes

Hello,
I am working towards using an IR remote to control a couple of DC motors and a couple of Servo Motors. However, I’ve been having trouble with Hex codes which I receive from the IR remote I am using.

When I use this code, I get the expected Hex codes.

#include <IRremote.h>

const int RECV_PIN = A5;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup(){
  Serial.begin(9600);
  irrecv.enableIRIn();
  irrecv.blink13(true);
}

void loop(){
  if (irrecv.decode(&results)){
        Serial.println(results.value, HEX);
        irrecv.resume();
  }
}

However when I use this code, I get a bunch of random codes.

#include <Servo.h>
#include <AFMotor.h>
#include <IRremote.h>

AF_DCMotor motor(1); // DC motor on M1
AF_DCMotor pump(3); // Pump at M3
Servo pitch;  // Pitch of Nozzle at Pin 9
Servo rotate; // Rotating plate for nozzle at Pin 10

const int RECV_PIN = A5;
IRrecv irrecv(RECV_PIN);
decode_results results;

bool Press = false;
int p_pos = 0;    // variable to store the pitch position
int r_pos = 0;    // variable to store the rotation position

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn();
  irrecv.blink13(true);

  pitch.attach(10);    // attaches the servo on pin 9 to the servo object
  rotate.attach(9);  // attaches the servo on pin 10 to the servo object
  pitch.write(p_pos);
  rotate.write(r_pos);


}

void loop() 
{
  if (irrecv.decode(&results))
  {
    Serial.println(results.value, HEX);
    irrecv.resume();



    if (results.value == 0xFFA25D)
    {
      motor.run(FORWARD);
      motor.setSpeed(255);
      delay(1000);
      motor.run(RELEASE);
      delay(1000);
    }

    if (results.value == 0xFFE21D)
    { 
      for (p_pos = 0; p_pos <= 45;) 
      {
        p_pos += 5;
        pitch.write(p_pos);
       delay(500);
      }
      for (p_pos = 45; p_pos >= 0;)
      {
        p_pos -= 5;
        pitch.write(p_pos);
        delay(500);
      }
  
    }

    if (Press == true)
    {
      for (r_pos = 0; r_pos <= 180;) 
      {
        r_pos += 5;
        rotate.write(r_pos);
        delay(500);
      }
      for (r_pos = 180; r_pos >= 0;)
      {
        r_pos -= 5;
        rotate.write(r_pos);
        delay(500);
      }

    }
  }

}

I’m using IRremote library 2.8.0 and I have tried looking up the issue and I have tried isolating the problem code to no avail. I assume its not the sensor considering that it works perfectly with the first code.

Hopefully someone can locate the problem.

Hello
Try to move this code to the end of your sketch

I tried moving the code to after all the if functions for the motors. I made sure that it was still within the first if function. Still the random codes.
I also tried blocking out all the other if functions for controlling the motors. I expected it to work like the first code since it was the same if function, but it gave me random codes too.

It is likely to be a timer resource conflict. Which Arduino board are you using ?

I’m using an Arduino Uno with a L293D Motor Shield from my local electronics store (Jaycar).

The IRremote library appears to use timer 2 by default on a Uno board. This can be changed in private/IRTimer.cpp.h

The Servo.h library appears to use timer 1 but I don’t know the library well. I’ve not looked at
AFMotor.h .

Start by trying to see which of the two motor libraries is causing the conflict by commenting out all the code belonging to a specific library including its # include statement.

Thanks a lot for your help.

When I comment out the Servo library statements, the codes I received were not the Hex codes I received previously, however, I do receive only 2 outputs for each button; an 8 char long string and a 6 char long string.

Commenting out the AFMotor library, the codes are random. So it appears the issue is with the servo library.

What can I do so that I can use both libraries together?

Unfortunately, I have not been able to keep fully up to date with all the changes that have happened (are happening?) with the IRremote library.

However, do you have the following file in your Arduino libraries folder under the directory IRremote/src

private/IRTimer.cpp.h

If so, is there any evidence that the default timer for the Uno (ATmega328p) has been changed? Here, for example

//------------------------------------------------------------------------------
// Define which timer to use
//
// Uncomment the timer you wish to use on your board.
// Here you have the option to switch IRremote to use a different timer and send pin.

/*********************
 * ARDUINO Boards
 *********************/
// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, Nano, etc
// ATmega48, ATmega88, ATmega168, ATmega328
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328PB__) || defined(__AVR_ATmega168__)
#  if !defined(IR_USE_TIMER1) && !defined(IR_USE_TIMER2)
//#define IR_USE_TIMER1   // send pin = pin 9
#define IR_USE_TIMER2     // send pin = pin 3
#  endif

The file you mentioned no longer exists, however, I was able to find this block of code in the file “IREremoteBoardDefs.h”.
Note: The bolded text has double underscores on either side ( Text )

//------------------------------------------------------------------------------
// Define which timer to use
//
// Uncomment the timer you wish to use on your board.
// If you are using another library which uses timer2, you have options to
// switch IRremote to use a different timer.
//

#ifndef ARDUINO
// Assume that we compile a test version, to be executed on the host,
// not on a board.

// Do not define any timer.

/*********************

  • ARDUINO Boards
    *********************/
    // Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, Nano, etc
    // ATmega48, ATmega88, ATmega168, ATmega328
    #elif defined(AVR_ATmega328P) || defined(AVR_ATmega328PB) || defined(AVR_ATmega168) // old default clause
    # if !defined(IR_USE_TIMER1) && !defined(IR_USE_TIMER2)
    //#define IR_USE_TIMER1 // tx = pin 9
    #define IR_USE_TIMER2 // tx = pin 3
    # endif

Perhaps I can just install an older version of the IRremote library which you are familiar with?

I’ve just checked. Version 3.2.0 of the IRremote does have the /priv/IRTimer.cpp.h so it may well be that your version is out of date. The file, incidentally, was updated on Github 6 hours ago. Arduino-IRremote/src/private at master · Arduino-IRremote/Arduino-IRremote · GitHub so this may all not be fully stable.

Maybe try ensuring that you have the latest version and then check that your first (simplified) sketch still works.

Which remote control type are you using ? I did once write a lightweight library for NEC remote controls at a time when the standard library had poor support for some ATTiny devices. It does not use timers. It uses external interrupts instead : Lightweight Arduino IR library for NEC remote control devices

If you get stuck, I can help you with that. However, for getting general help on the forum, it may be better to attempt to use the latest IRremote version.

The 3.2.0 version of the library does not work with my original code.

I did write this code to print.

#include <Arduino.h>
#include <IRremote.h>

void setup() {
    Serial.begin(115200);
   
    IrReceiver.begin(A5, ENABLE_LED_FEEDBACK, USE_DEFAULT_FEEDBACK_LED_PIN);

}

void loop() {
    if (IrReceiver.decode()) {

        // Print a short summary of received data
        IrReceiver.printIRResultShort(&Serial);
       
        Serial.println(IrReceiver.decodedIRData.command);

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

    }
}

Running it, I get values like this Ue,(⸮⸮
but they are still different every time.

I based my code off the new example code in the library.

/*
 * SimpleReceiver.cpp
 *
 * Demonstrates receiving NEC IR codes with IRrecv
 *
 *  Copyright (C) 2020-2021  Armin Joachimsmeyer
 *  armin.joachimsmeyer@gmail.com
 *
 *  This file is part of Arduino-IRremote https://github.com/Arduino-IRremote/Arduino-IRremote.
 *
 *  MIT License
 */

/*
 * Specify which protocol(s) should be used for decoding.
 * If no protocol is defined, all protocols are active.
 */
//#define DECODE_DENON        // Includes Sharp
//#define DECODE_JVC
//#define DECODE_KASEIKYO
//#define DECODE_PANASONIC    // the same as DECODE_KASEIKYO
//#define DECODE_LG
#define DECODE_NEC          // Includes Apple and Onkyo
//#define DECODE_SAMSUNG
//#define DECODE_SONY
//#define DECODE_RC5
//#define DECODE_RC6

//#define DECODE_BOSEWAVE
//#define DECODE_LEGO_PF
//#define DECODE_MAGIQUEST
//#define DECODE_WHYNTER

//#define DECODE_HASH         // special decoder for all protocols

#include <Arduino.h>

/*
 * Define macros for input and output pin etc.
 */
#include "PinDefinitionsAndMore.h"

#include <IRremote.h>

void setup() {
    Serial.begin(115200);
    // Just to know which program is running on my Arduino
    Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_IRREMOTE));

    /*
     * Start the receiver, enable feedback LED and take LED feedback pin from the internal boards definition
     */
    IrReceiver.begin(A5, ENABLE_LED_FEEDBACK, USE_DEFAULT_FEEDBACK_LED_PIN);

    Serial.print(F("Ready to receive IR signals at pin "));
    Serial.println(A5);
}

void loop() {
    /*
     * Check if received data is available and if yes, try to decode it.
     * Decoded result is in the IrReceiver.decodedIRData structure.
     *
     * E.g. command is in IrReceiver.decodedIRData.command
     * address is in command is in IrReceiver.decodedIRData.address
     * and up to 32 bit raw data in IrReceiver.decodedIRData.decodedRawData
     */
    if (IrReceiver.decode()) {

        // Print a short summary of received data
        IrReceiver.printIRResultShort(&Serial);
        if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
            // We have an unknown protocol here, print more info
            IrReceiver.printIRResultRawFormatted(&Serial, true);
        }
        Serial.println();

        /*
         * !!!Important!!! Enable receiving of the next value,
         * since receiving has stopped after the end of the current received data packet.
         */
        IrReceiver.resume(); // Enable receiving of the next value

        /*
         * Finally, check the received data and perform actions according to the received command
         */
        if (IrReceiver.decodedIRData.command == 0x10) {
            // do something
        } else if (IrReceiver.decodedIRData.command == 0x11) {
            // do something else
        }
    }
}

Also I am using this remote


which I purchased from my locale Jaycar (Austrailan Electronics store).

Just to be clear. You got consistent results with an old version of the IR remote library, the first code sample you posted, the IR remote device from Jaycar in the picture and you are using the same IR receiver in all tests. Is that correct?
Do you know what protocol that IR remote control uses ? That sample code which you have obtained from the new library specifies a protocol, which may give better results.
What IR receiver are you using?

Yes I got consistent results with the same IR remote and Receiver in all the tests. The remote uses the NEC protocol as specified on the company’s website who I bought it from.

I’m not sure what IR receiver I am using as it was sourced from my University but it was working as expected in my old code.

if You have no success with the standard example which came with the new library, the you can try the link I supplied in post #10

No success. I tried with your sample sketch at it appears to be working perfectly when I include the libraries for the dc motors and servo motors.

Just one thing, do you know why I am getting a bunch of weird symbols as my output? They are consistent with the buttons however.

Also, I would like to use one of my Analog pins with the code since I am using a Motor shield a long with my Arduino which uses all the digital pins except 2.
If possible, what edits would I have to do to the code?

Pin 2 works with the sketch I supplied.
The analog pins can also be used as digital pins, however, my sketch uses external interrupts so cannot, without modifications, be used with an analog pin. The modification would mean using pin change interrupts instead. It is slightly more complex.
If you print the hex codes received directly, you could get strange results since many codes will not map to printable characters.

Ah okay I see. I was planning to use the one digital pin to control an external PCB, but I’ve heard you can use Analog pins like Digital pins so hopefully that will work.

With the hex codes, is there a decoding function which I can integrate into your code or are the strange characters good enough for my if functions to control my motors?

You’ll definitely need to do some decoding. Normal is to use a switch case statement. For example:

switch ( irCode ) {

  case 0x1A37: { // issue motor command Forwards }
  case 0x1B45: { // issue motor command Stop }
  // Etc.
}

Where 0x1A37 is the hex code derived from the button to be used for Forwards.

In first code you used Serial speed 9600, in the other code it uses 115200, maybe that’s why you see strange characters ?

Sweet, you got the source of the strange characters. I think that’s the end of my issues. Thanks guys for all the help.

I’ll reply to this forum if I have any other issues.