Sending and receiving IR signals on the same Arduino

Hi all, I just found a solution to a question which has been asked before and has been bothering me for quite some time. When using the IRremote library on an Arduino, you can not send and receive in the same program it seems at first. The trick is to put a delay of 400 ms between receiving a signal and sending a new one. Also see this topic: IR Remote Send/Receive from the same Arduino - Troubleshooting - Arduino Forum

Here's an example:

#include <IRremote.h>
#include <IRremoteInt.h>

#define  SEND_DELAY          100
#define  RECEIVE_SEND_DELAY  400

enum ziggoRemote
{
  ziggoPower = 2060,
  ziggoPower2 = 12,

} ZiggoRemote;

enum wingLedStrip
{
  wingLedStripType = NEC,
  wingLedStripLength = 32,
  wingLedStripOn = 16203967,
} WingLedStrip;

int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
IRsend irsend;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
}

void loop()
{
  if (receiveSignal())
  {
    sendSignal();
  }
}

bool receiveSignal()
{
  if (irrecv.decode(&results))
  {
    switch (results.decode_type)
    {
      case SONY:
        Serial.println("SONY");
        break;
      case NEC:
        Serial.println("NEC");
        break;
      case RC5:
        Serial.println("RC5");
        break;
      case RC6:
        Serial.println("RC6");
        break;
      default:
        break;
    }
    Serial.println(results.value);
    Serial.println(results.bits);
    irrecv.resume(); // Receive the next value
    return true;
  }
  return false;
}

void sendSignal()
{
  switch (results.value)
  {
    case ziggoPower: case ziggoPower2:
      delay(RECEIVE_SEND_DELAY);
      irsend.sendNEC(wingLedStripOn, wingLedStripLength);
      break;
    default:
      break;
  }
  delay(SEND_DELAY);
  Serial.println("Signal send");
  irrecv.enableIRIn();
}

Thought I'd quickly share it with you for the guys out there struggling with this like I was.

1 Like

Why would you want to do that anyway?

JimboZA:
Why would you want to do that anyway?

  1. So you can use an unused button on your TV remote to operate some other device - saves having to keep multiple remotes handy.

  2. So you can send codes not normally available from your remote (e.g. discreet input selection code)

  3. So you can use one button command to send several different commands - e.g. TV off also turns off Amplifier, DVD and STB.

  4. So a handy spare remote can be used in place of a lost or broken remote - e.g. use a Sony TV remote to work an old - obsolete device with missing remote.

.... must be other handy uses :slight_smile: