IR Receive / Send Sketch "Error Compiling for 'board'"

Hi all!

I am trying to make a sketch that can send and receive IR signals. I have a working sender sketch, and a working receiver sketch, but when I tried to combine them, I got this error:

Arduino: 1.8.19 (Mac OS X), Board: "Arduino Nano, ATmega328P (Old Bootloader)"











In file included from /Users/user/Desktop/Arduino/NERF_LG_Hash_Decoder/NERF_LG_Hash_Decoder.ino:16:0:
/Users/user/Desktop/Arduino/libraries/IRLibRecv/IRLibRecv.h:18:7: error: redefinition of 'class IRrecv'
 class IRrecv: public IRrecvBase {
       ^~~~~~
In file included from /Users/user/Desktop/Arduino/NERF_LG_Hash_Decoder/NERF_LG_Hash_Decoder.ino:15:0:
/Users/user/Desktop/Arduino/libraries/IRremote/IRremote.h:171:7: note: previous definition of 'class IRrecv'
 class IRrecv
       ^~~~~~
In file included from /Users/user/Desktop/Arduino/libraries/IRremote/IRremote.h:24:0,
                 from /Users/user/Desktop/Arduino/NERF_LG_Hash_Decoder/NERF_LG_Hash_Decoder.ino:15:
/Users/user/Desktop/Arduino/libraries/IRremote/IRremoteInt.h:107:16: error: expected identifier before numeric constant
 #define MARK   0
                ^
/Users/user/Desktop/Arduino/libraries/IRLib2/IRLibDecodeBase.h:37:17: note: in expansion of macro 'MARK'
   enum RCLevel {MARK, SPACE, ERROR};//used by decoders for RC5/RC6
                 ^~~~
/Users/user/Desktop/Arduino/libraries/IRremote/IRremoteInt.h:107:16: error: expected '}' before numeric constant
 #define MARK   0
                ^
/Users/user/Desktop/Arduino/libraries/IRLib2/IRLibDecodeBase.h:37:17: note: in expansion of macro 'MARK'
   enum RCLevel {MARK, SPACE, ERROR};//used by decoders for RC5/RC6
                 ^~~~
/Users/user/Desktop/Arduino/libraries/IRremote/IRremoteInt.h:107:16: error: expected unqualified-id before numeric constant
 #define MARK   0
                ^
/Users/user/Desktop/Arduino/libraries/IRLib2/IRLibDecodeBase.h:37:17: note: in expansion of macro 'MARK'
   enum RCLevel {MARK, SPACE, ERROR};//used by decoders for RC5/RC6
                 ^~~~
In file included from /Users/user/Desktop/Arduino/NERF_LG_Hash_Decoder/NERF_LG_Hash_Decoder.ino:17:0:
/Users/user/Desktop/Arduino/libraries/IRLib2/IRLibDecodeBase.h:38:3: error: 'RCLevel' does not name a type
   RCLevel getRClevel(uint8_t *used, const uint16_t t1);
   ^~~~~~~
/Users/user/Desktop/Arduino/libraries/IRLib2/IRLibDecodeBase.h:39:1: error: expected unqualified-id before 'protected'
 protected:
 ^~~~~~~~~
/Users/user/Desktop/Arduino/libraries/IRLib2/IRLibDecodeBase.h:43:1: error: expected declaration before '}' token
 };
 ^
Multiple libraries were found for "IRremote.h"
 Used: /Users/user/Desktop/Arduino/libraries/IRremote
 Not used: /Users/user/Desktop/Arduino/libraries/IRremote-2.2.3
 Not used: /Users/user/Desktop/Arduino/libraries/Arduino-IRremote-master
exit status 1
Error compiling for board Arduino Nano.

Any thoughts?

Sender code:

#include <IRremote.h>

IRsend irsend;

// PURPLE
unsigned int rawDataPurple[] = {
  2888, 5987, 2893,
  2058, 848, 2052, 843, 2069, 795, 2104, 791,  // 0, 0, 0, 0
  2109, 1816, 2109, 837, 2063, 842, 2057, 848, // 1, 0, 0, 0
  2052, 1873, 2055, 840, 2059, 846, 2054, 841, // 1, 0, 0, 0
  2071, 794, 2106, 789, 2111, 795, 2105, 841,  // 0, 0, 0, 0
  1000
};

// RED
unsigned int rawDataRed[] = {
  2899, 5977, 2897,
  2065, 842, 2058, 849, 2051, 845, 2055, 842,  // 0, 0, 0, 0
  2058, 1869, 2056, 851, 2061, 826, 2074, 792, // 1, 0, 0, 0
  2108, 789, 2111, 836, 2064, 843, 2057, 840,  // 0, 0, 0, 0
  2060, 846, 2054, 843, 2057, 850, 2050, 847,  // 0, 0, 0, 0
  1000
};

// BLUE
unsigned int rawDataBlue[] = {
  2896, 5979, 2902,
  2060, 836, 2064, 843, 2057, 848, 2052, 844,  // 0, 0, 0, 0
  2055, 1870, 2055, 851, 2052, 844, 2065, 820, // 1, 0, 0, 0
  2080, 795, 2105, 1821, 2104, 792, 2111, 836, // 0, 1, 0, 0
  2063, 843, 2057, 838, 2062, 845, 2055, 840,  // 0, 0, 0, 0
  1000
};

void setup(){}

void loop() {
  int khz = 38; // 38kHz carrier frequency for the NEC protocol
  //unsigned int irSignal[] = {9000, 4500, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 39416, 9000, 2210, 560}; //AnalysIR Batch Export (IRremote) - RAW
  digitalWrite(8, HIGH);
  delay(500);
  digitalWrite(8, LOW);
  irsend.sendRaw(rawDataBlue, sizeof(rawDataBlue) / sizeof(rawDataBlue[0]), khz); //Note the approach used to automatically calculate the size of the array.
  delay(5000); //In this example, the signal will be repeated every 5 seconds, approximately.
}

Combined (Sender + Receiver) Code:

#define IR_RECEIVER 2

#include <IRremote.h>
#include <IRLibRecv.h> 
#include <IRLibDecodeBase.h> 
#include <IRLib_HashRaw.h>
#include <IRLibCombo.h> 

#define LED 8

IRdecode myDecoder;

IRrecv myReceiver(IR_RECEIVER);
IRsend irsend;

// PURPLE
unsigned int rawDataPurple[] = {
  2888, 5987, 2893,
  2058, 848, 2052, 843, 2069, 795, 2104, 791,  // 0, 0, 0, 0
  2109, 1816, 2109, 837, 2063, 842, 2057, 848, // 1, 0, 0, 0
  2052, 1873, 2055, 840, 2059, 846, 2054, 841, // 1, 0, 0, 0
  2071, 794, 2106, 789, 2111, 795, 2105, 841,  // 0, 0, 0, 0
  1000
};

// RED
unsigned int rawDataRed[] = {
  2899, 5977, 2897,
  2065, 842, 2058, 849, 2051, 845, 2055, 842,  // 0, 0, 0, 0
  2058, 1869, 2056, 851, 2061, 826, 2074, 792, // 1, 0, 0, 0
  2108, 789, 2111, 836, 2064, 843, 2057, 840,  // 0, 0, 0, 0
  2060, 846, 2054, 843, 2057, 850, 2050, 847,  // 0, 0, 0, 0
  1000
};

// BLUE
unsigned int rawDataBlue[] = {
  2896, 5979, 2902,
  2060, 836, 2064, 843, 2057, 848, 2052, 844,  // 0, 0, 0, 0
  2055, 1870, 2055, 851, 2052, 844, 2065, 820, // 1, 0, 0, 0
  2080, 795, 2105, 1821, 2104, 792, 2111, 836, // 0, 1, 0, 0
  2063, 843, 2057, 838, 2062, 845, 2055, 840,  // 0, 0, 0, 0
  1000
};

/* IR hashes for Nerf LaserOps standard game */
static const long PURPLE = 0x67228B44;
static const long RED    = 0x78653B0E;
static const long BLUE   = 0x2FFEA610;

int khz = 38; // 38kHz carrier frequency for the NEC protocol

int timer;
int timer1;

void setup() {
  Serial.begin(115200);
  
  pinMode(LED, OUTPUT);
  
  myReceiver.enableIRIn(); // Start the receiver
  
  Serial.println(F("Ready to receive IR signals"));
}

void loop() {
  if(myReceiver.getResults()) {
    myDecoder.decode();
    if(myDecoder.protocolNum==UNKNOWN) {
      Serial.print(F("IR recieved. Hash value is: 0x"));
      Serial.println(myDecoder.value,HEX);
      if (myDecoder.value == PURPLE) {
        Serial.println("Purple hit!!!!");
      }
      else if (myDecoder.value == BLUE) {
        Serial.println("BLUE hit!!!!");
      }
      else if (myDecoder.value == RED) {
        Serial.println("RED hit!!!!");
        digitalWrite(LED, HIGH);
        delay(2000);
        digitalWrite(LED, LOW);
      }
    } 
    else {
      myDecoder.dumpResults(false);
    }
    
    myReceiver.enableIRIn(); 
  }
  timer++;
  if (timer == 5000){
    timer = 0;
    shoot();
  }
  delay(1);
}

void shoot(){
  digitalWrite(13, HIGH);
  delay(40);
  digitalWrite(13, LOW);
  irsend.sendRaw(rawDataBlue, sizeof(rawDataBlue) / sizeof(rawDataBlue[0]), khz); //Note the approach used to automatically calculate the size of the array.
}

Thanks in advance!

The IRremote library does both send and receive. you seem to have a second library called IRLibRecv and a third called IRLib2! Pick one library and base your sketch on the examples from that one library.

Yeah, that's what I have figured out. I'm not sure how to receive a raw value with irremote.h though. I'm working on that now.

IRremote "ReceiveAndSend" example.

File->Examples->IRremote->ReceiveAndSend

Thanks. I tried that sketch, but I need to read raw values. When I send the code I want to receive, the serial monitor reads "FFFFFFFF", instead of "0x78653B0E" that I know is sent. How do I read that?

An example for reading the raw remote data using the latest version of the IRremote library.

#include <IRremote.h>

const byte IR_RECEIVE_PIN = 7;

unsigned long checkIrInterval = 100; // millis
unsigned long keycommand = 0;
unsigned long keyAddress = 0;
unsigned long keycode = 0;

void setup()
{
   Serial.begin(115200);
   Serial.println("IR receive test");
   IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
}

void loop()
{
   checkIR();
}

void checkIR()
{
   static unsigned long timer = 0;
   unsigned long interval = checkIrInterval;
   if (millis() - timer >= interval)
   {
      timer = millis();
      if (IrReceiver.decode())
      {         
         keycode = IrReceiver.decodedIRData.decodedRawData;
         printIR();
         // ignore repeat codes
         if ((IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT))
         {
            IrReceiver.resume();
            return;
         }
         IrReceiver.resume();
      }
   }
}

void printIR()
{  
   Serial.print("   decoded raw data = ");
   Serial.print(keycode);
   Serial.print("   raw HEX = ");
   Serial.println(keycode, HEX);
   Serial.println();
}

What all shows up on Serial Monitor?

I just tried your code with the newest library, and the serial monitor prints "decoded raw data = 0 raw HEX = 0".

Serial monitor prints results.value as "FFFFFFFF"