IR Receiver Library

Hi all!

I have built an automatic laser (IR) gun that interacts with the Nerf LaserX guns using an Arduino Nano! I am trying to do it with a ESP32 (Fire-Beetle) but I am having library issues. Does anyone know of a good replacement library for the ESP32? It took forever for me to find suitable libraries just for the Nano, because I need to be able to send and receive raw values.

Code:

#include <IRLibSendBase.h>
#include <IRLibRecv.h> 
#include <IRLibDecodeBase.h> 
#include <IRLib_HashRaw.h>
#include <IRLibCombo.h> 
#include <Servo.h>

//void(* resetFunc) (void) = 0; // create a standard reset function

#define RECV_PIN 11
#define shotLED 8
#define redLED 6
#define yellowLED 5
#define blueLED 7
#define shootingLED 4

Servo servo;
Servo servo2;

IRrecv myReceiver(RECV_PIN);
IRsendRaw mySender;

IRdecode myDecoder;

static const long PURPLE = 0x67228B44;
static const long RED    = 0x78653B0E;
static const long BLUE   = 0x2FFEA610;
static const long LStar  = 0x181FAA50;

// 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
};

int timer;
int timer1;
int timer2;
int maxTimer = 2000;

int shotMax = 5;

int timeoutTime;
int shotAmount;

int myColor;

int shotA;

int pos;
int pos2;

boolean endScan;
boolean endScan2;

boolean startTimer;

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

  myReceiver.enableIRIn(); // Start the receiver

  servo.attach(9);
  servo2.attach(10);
  servo.write(40);
  servo2.write(70);

  pinMode(shotLED, OUTPUT);
  pinMode(redLED, OUTPUT);
  pinMode(yellowLED, OUTPUT);
  pinMode(blueLED, OUTPUT);
  pinMode(shootingLED, OUTPUT);
  digitalWrite(shotLED, LOW);
  digitalWrite(redLED, LOW);
  digitalWrite(yellowLED, LOW);
  digitalWrite(blueLED, LOW);
  digitalWrite(shootingLED, LOW);
  
  delay(1000);

  Try:
  while (!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 set!!!!");
      myColor = 1;
      digitalWrite(yellowLED, HIGH);
    }
    else if (myDecoder.value == BLUE) {
      Serial.println("BLUE set!!!!");
      myColor = 2;
      digitalWrite(blueLED, HIGH);
    }
    else if (myDecoder.value == RED) {
      Serial.println("RED set!!!!"); 
      myColor = 3;
      digitalWrite(redLED, HIGH);     
    }
    else {
      Serial.println("Try again...");
      myReceiver.enableIRIn();
      goto Try;
    }
    myReceiver.enableIRIn();
  }

  for (int p=0;p<=(myColor-1);p++){
    digitalWrite(shotLED, HIGH);
    delay(500);
    digitalWrite(shotLED, LOW);
    delay(500);
  }
}

void loop() {
  servoScan();

  // team leds
  if (myColor == 1) {
    digitalWrite(yellowLED, HIGH);
  }
  else if (myColor == 2) {
    digitalWrite(blueLED, HIGH);
  }
  else if (myColor == 2) {
    digitalWrite(redLED, HIGH);     
  }
  
  //servo timer
  timer++;
  if (startTimer == true){
    timer1++;
    if (timer1 == 2000){
      timer1 = 0;
      startTimer = false;
      maxTimer = 2000;
      servo.write(40);
      servo2.write(57);
      delay(500);
      servo2.write(70); //cancel out servo sag
    }
  }

  if (timer == maxTimer) {
    timer = 0;
    Serial.println("Shooting...");
    digitalWrite(shootingLED, HIGH);
    shoot();
    myReceiver.enableIRIn();
    delay(50);
    digitalWrite(shootingLED, LOW);
  }

  // boost mode
  if (myReceiver.getResults()){
    Serial.println(myReceiver.getResults());
    myDecoder.decode();
    if(myDecoder.protocolNum==UNKNOWN) {
      //Serial.print(F("IR recieved. Hash value is: 0x"));
      Serial.println(myDecoder.value,HEX);
      delay(20);
      if (myDecoder.value == RED && myColor == 3 || myDecoder.value == BLUE && myColor == 2){
        boostSet();
        timer = 0;
        maxTimer = 6;
        startTimer = true;
      }

      if (myDecoder.value == PURPLE && (myColor == 2 || myColor == 3)){ //reset
        digitalWrite(shotLED, HIGH);
        digitalWrite(redLED, HIGH);
        digitalWrite(yellowLED, HIGH);
        digitalWrite(blueLED, HIGH);
        digitalWrite(shootingLED, HIGH);
        delay(4000);
        digitalWrite(shotLED, LOW);
        digitalWrite(redLED, LOW);
        digitalWrite(yellowLED, LOW);
        digitalWrite(blueLED, LOW);
        digitalWrite(shootingLED, LOW);
        resetFunc();
      }

      //normal mode
      if (myDecoder.value == PURPLE && myColor == 1) {
        shotA++;
        if (shotA != 3){
          return;
        }
        shotA = 0;
        Serial.println("Purple hit!!!!");
        digitalWrite(shotLED, HIGH);
        delay(2000);
        digitalWrite(shotLED, LOW);
        shotAmount++;
        Serial.println(shotAmount);
        if (shotAmount == shotMax){
          shotAmount = 0;
          for (int i=0;i<=18;i++){
            digitalWrite(shotLED, HIGH);
            delay(350);
            digitalWrite(shotLED, LOW);
            delay(350);
          }
        }
        delay(100);
      }
      else if (myDecoder.value == BLUE && myColor == 3) {
        shotA++;
        if (shotA != 3){
          return;
        }
        shotA = 0;
        Serial.println("BLUE hit!!!!");
        digitalWrite(shotLED, HIGH);
        delay(2000);
        digitalWrite(shotLED, LOW);
        shotAmount++;
        Serial.println(shotAmount);
        if (shotAmount == shotMax){
          shotAmount = 0;
          for (int i=0;i<=18;i++){
            digitalWrite(shotLED, HIGH);
            delay(350);
            digitalWrite(shotLED, LOW);
            delay(350);
          }
        }
        delay(100);
      }
      else if (myDecoder.value == RED && myColor == 2) {
        shotA++;
        if (shotA != 3){
          return;
        }
        shotA = 0;
        Serial.println("RED hit!!!!"); 
        digitalWrite(shotLED, HIGH);
        delay(2000);
        digitalWrite(shotLED, LOW);
        shotAmount++;
        Serial.println(shotAmount);
        if (shotAmount == shotMax){
          shotAmount = 0;
          for (int i=0;i<=18;i++){
            digitalWrite(shotLED, HIGH);
            delay(350);
            digitalWrite(shotLED, LOW);
            delay(350);
          }
        }
        delay(100);
      }
    }
    myReceiver.enableIRIn();
  }
  delay(1);
}

void servoScan(){
  //scan servos
  if (startTimer == true){
    //servo1
    if (endScan == false){
      pos++;
    }
    else if (endScan == true){
      pos--;
    }
    if (pos == 65){
      endScan = true;
    }
    else if (pos == 15){
      endScan = false;
    }
    servo.write(pos);
    //servo2
    if (endScan2 == false){
      pos2++;
    }
    else if (endScan2 == true){
      pos2--;
    }
    if (pos2 == 110){
      endScan2 = true;
    }
    else if (pos2 == 40){
      endScan2 = false;
    }
    servo2.write(pos2);
    
    delay(4);
  }
}


void shoot() {
  if (myColor == 1){
    mySender.send(rawDataPurple, 36, 38);
  }
  else if (myColor == 3){
  mySender.send(rawDataRed, 36, 38);
  }
  else if (myColor == 2){
    mySender.send(rawDataBlue, 36, 38);//Pass the buffer,length, optionally frequency
  }
}

void boostSet(){
  Serial.println("Boost charge...");
  digitalWrite(yellowLED, HIGH);
  delay(500);
  digitalWrite(redLED, HIGH);
  delay(500);
  digitalWrite(blueLED, HIGH);
  delay(500);
  digitalWrite(shotLED, HIGH);
  delay(500);
  digitalWrite(shootingLED, HIGH);
  delay(500);
  digitalWrite(shotLED, LOW);
  digitalWrite(redLED, LOW);
  digitalWrite(yellowLED, LOW);
  digitalWrite(blueLED, LOW);
  digitalWrite(shootingLED, LOW);
}

Error:

Multiple libraries were found for "Servo.h"
 Used: /Users/user/Desktop/Arduino/libraries/Servo
/Users/user/Desktop/Arduino/libraries/IRLibRecv/IRLibRecv.cpp:16:12: fatal error: avr/interrupt.h: No such file or directory
 Not used: /Applications/Electronics/Arduino.app/Contents/Java/libraries/Servo
   #include <avr/interrupt.h>
            ^~~~~~~~~~~~~~~~~
compilation terminated.
exit status 1
Error compiling for board FireBeetle ESP32-E.

Any suggestions?
Thanks in advance!

Break the code down to find out where it fails..//…

Ok, I got everything working including sending, but can't figure out the receiver code. Before on the Arduino Nano, I used this, but you can't use that with the IRremoteESP8266 library.
I basically just need a way to hash the IR signal to a 32 bit code, and I don't care what protocols or values are being sent. I just need to distinguish between different team shots.

Any help or ideas would be greatly appreciated!

I found this, but it's private. How would I be able to use it?
https://crankyoldgit.github.io/IRremoteESP8266/doxygen/html/classIRrecv.html#a7c15fbfa7936ca474712a1953911fd06

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.