IR Communication

Hi all! I am currently working on a system for RC airplanes to have air to air combat using IR. I have made this demo code to just work out the kinks before I actually get it to project ready and I keep having one problem that I can't understand. I am trying to make it so that the Arduino sends an IR signal and then picks it up again on the same board since I don't have to yet. I can't get the IR signal to read though they both are wired up correctly. I have hooked it up to my TV signals and it works both giving and taking them on separate pieces of code, but doesn't work with one. Any help is appreciated! Thanks!

#include <IRremote.h>

const int RECV_PIN = 7;
IRrecv irrecv(RECV_PIN);
decode_results results;
IRsend irsend;
int button = 13;
char buttonState = LOW;

void setup(){
  Serial.begin(9600);
  irrecv.enableIRIn();
  pinMode(7, INPUT);
  irrecv.blink13(true);
  pinMode(button, INPUT);
}

void loop(){
    buttonState = digitalRead(button);
    if (buttonState == LOW) {
      irsend.sendNEC(0x2FD58A7, 32);
      Serial.println("Fired");
      delay(500);
  }
  delay(500);
    if (irrecv.decode(&results)){
      switch(results.value){
        case 0x2FD58A7: 
        Serial.println("Hit");
        delay(500);
      }
      irrecv.resume(); 
    }
  }

You can not RX and TX IR at the same time using the usual IR library, because each one takes too much processor time. So a loopback won't work.

What should I do then?

IR will NOT work in sunlight, and will NOT work over a distance of more than about 10 feet....

Regards,
Ray L.

Cessna172:
What should I do then?

Alternate between RX and TX.

Cessna172:
What should I do then?

Use a second Arduino.

...R

I would like to make it a 2 way dogfight so you can both be shot and shoot. I have some amplification methods I am going to try to make it have a longer range and work better in daylight. Also, how would I alternate between TX and RX? Thanks!

Cessna172:
I have some amplification methods I am going to try to make it have a longer range and work better in daylight.

Think of the sun as one of the 16" guns on the USS Missouri, firing 16" high-explosive shells, weighing 2,000 pounds each, lobbing them up to 40 miles to their target. Think of your little IR transmitter as a straw, firing spitballs, maybe 8-10 feet....

Regards,
Ray L.

I'll figure it out, but do you have any advice on how to alternate between TX and RX?

Cessna172:
I'll figure it out, but do you have any advice on how to alternate between TX and RX?

When you are not transmitting, you are looking for receive data. If you're doing bi-directional communication, You will need to figure out a protocol to deal with "collisions", where two devices try to transmit at the same time. Or, you define a single "master" device that is the only one that can initiate communications. I have no clue what is appropriate for your application. You will SURELY also need a protocol with error handling, retries and all that good stuff, because IR is incredibly error-prone under the best of circumstances.

Regards,
Ray L.

Would you mind sending me an example of this code? I am a relative beginner. Thanks!

I don't have any such code. But I'd bet if you searched, you'd find SOMEONE has already done it. If, that is, it is possible to do it at all, which I don't think it is, for the reasons I've already pointed out. If I were you, I would forget about your application, and just take your setup outside, and see if you can send a single code between two Ardunos even 10 feet apart in sunlight. I'd be willing to bet you cannot. Increase the distance, and it will get EXPONENTIALLY worse.

Regards,
Ray L.

I'm going to be using an amplification circuit and a few lenses to help increase the range.

You'll still be trying to pick out a tiny, weak signal from a massive amount of ambient noise. Rather like NASA receiving the signals from spacecraft at the edge of the galaxy. But they have 70m dish antennas, and TONS of signal processing, to help them. And, they know EXACTLY where to look at all times.

Good luck....

Regards,
Ray L.

Thanks!