IRremote Send and Receive simultaneously

Hey guys,

I'm currently working on a lasertag game with several weapons.

I would like to use one arduino nano in each weapon. It should be able to receive the IR-signals of the opponents as well as send IR-signals if a button is triggered.
So now there comes my problem:

I implemented an interrupt for the IR-receiver pin, so that an opponent's shot is always detected even when I'm shooting.
When the button is permanently pressed, the IR LED will shoot every 300 milliseconds (the send-function takes approximately 70ms and I implemented a delay of 230ms).
Unfortunately, the Nano won't detect any signal of the receiver in those 300ms.
However, if I disconnect the IR-LED everything seems to work perfectly.

Now I'm wondering, why the IR-LEDs connection has an effect on the functionality of my code.
Do you know any way I could solve this problem?

Here you can see the entire code I implemented:

#define IR_RECEIVE_PIN           2
#define IR_SEND_PIN              3
 
#define BUTTON_PIN              10
#define LED_PIN                 12
 
#include <IRremote.hpp>
#include <Arduino.h>
 
uint8_t sAddress = 0;
uint8_t sCommand = 0x59;
uint8_t sRepeats = 0;
 
volatile uint8_t hitData;
 
void setup() {
  IrSender.begin();
  IrReceiver.begin(IR_RECEIVE_PIN);
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(IR_RECEIVE_PIN), HIT, CHANGE);
}
 
void HIT() {
  if (IrReceiver.decode()) {
    hitData = IrReceiver.decodedIRData.command;
  }
  IrReceiver.resume();
}
 
void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {
    IrSender.sendNEC(sAddress, sCommand, sRepeats);
    delay(120);
  }
  
  if (hitData == sCommand) {   // indicates received signal 0x59
    hitData = 0x00;
    IrReceiver.resume();
    digitalWrite(LED_PIN, HIGH);
    delay(8);
    digitalWrite(LED_PIN, LOW);
  }
}

Could the emitted by the IR LED possibly be interfering with what is being received by the IR detector?

I'd rewrite the send fragment to not block the receiver processing from happening:

  if (digitalRead(BUTTON_PIN) == LOW) {
    static unsigned long last = 0;
    now = millis();
    if (now - last > 120 ) { 
       last = now;
       IrSender.sendNEC(sAddress, sCommand, sRepeats);
    }
  }

As is, when the button is down, you only get a single chance to read one incoming signal per outgoing signal.

1 Like

The sent IR signal, or reflected parts of it is received by your IR receiver in addition to any external signals.

Sounds like the 3-pin receiver is turning down it's internal gain (AGC) when it gets blinded by the IR LED. Did you use any optical shielding.

The code says 120ms.
Time to read up about using millis() for timing instead of that blocking delay().
Start with the BlinkWithoutDelay example that comes with the IDE.
Leo..

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