Reading and writing IR codes on same pin

Hello..

I'm currently trying to make a connection between pin 3 on my arduino and the outpin pin of the IR receiver in my speaker system's control-pod..

The result should be, that i am able to both read incoming IR-codes received by the speaker system, and send IR codes to actually control the system..

The IR-receiver in the system is Active-low..

Here is my code:

#include <avr/pgmspace.h>
#include "codes.h"
#include <IRremote.h>

const int IR = 3;

IRrecv irrecv(IR);

decode_results results;

int incomingByte = 0;

void setup() {
 pinMode(IR,INPUT);
 Serial.begin(9600);
 irrecv.enableIRIn();
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume();
  }
  if (Serial.available() > 0) {
      incomingByte = Serial.read();
      if(incomingByte = 49) {
         sendcommand(cmd_power); 
      }
  }
}
  
void sendcommand(prog_uint16_t *cmd) {
  pinMode(IR,OUTPUT);
  digitalWrite(IR,HIGH); 
  int t = 0;
  int addr = 0;
  for (int i=0;i<34;i++) {
      addr = (i<<1);
      digitalWrite(IR,LOW);
      t = pgm_read_word_near(cmd+addr);
      delayMicroseconds(t);
      digitalWrite(IR,HIGH);
      t = pgm_read_word_near(cmd+addr+1);
      delayMicroseconds(t);
  }
  delay(200); 
  pinMode(IR,INPUT);
  digitalWrite(IR,LOW); 
}

The problem is, that the send part only works if the

irrecv.enableIRIn();

isn't there..

The

if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
irrecv.resume();
}

part works perfectly..

And the send part works perfectly aswell.. just not together.

Here is a sample of codes.h:

prog_uint16_t cmd_power[] PROGMEM = {

        9100, 4450, 650, 500, 600, 550,
        550, 550, 600, 1650, 600, 550,
        600, 500, 600, 550, 600, 550,
        550, 1700, 550, 1700, 600, 1650,
        600, 500, 600, 1700, 550, 1700,
        550, 1700, 600, 1650, 600, 550,
        600, 500, 600, 550, 600, 500,
        600, 1700, 550, 550, 600, 550,
        550, 600, 550, 1700, 550, 1700,
        600, 1650, 600, 1650, 600, 550,
        550, 1700, 550, 1700, 600, 1650,
        600
};

And the IRremote.h is the library from: A Multi-Protocol Infrared Remote Library for the Arduino

Hope you can help!
Cheers

smilykoch:
Hello..

I'm currently trying to make a connection between pin 3 on my arduino and the outpin pin of the IR receiver in my speaker system's control-pod..

The result should be, that i am able to both read incoming IR-codes received by the speaker system, and send IR codes to actually control the system..

When you say "should", is this a hope you have or do you have a reason to expect this signal to be bidirectional? If the IR receiver is driving the line in push-pull mode you won't be able to override it without risking damage. If its open-drain or open-collector then things will be easy? Do you have information about this system we can look at?

Ill try to explain some more :slight_smile:

It's a hope i have.. but it not "really" bidirectional, as i dont want to be able to send and receive at the exact same time.. so i want my arduino to be an input device at default, but when i send e.g. "p"/"1" or something to my arduino over Serial, i want it to change the pinMode to OUTPUT, send the IR-command (the one in Codes.h) and the change pinMode back to INPUT..

Ihe IR-receiver in the speaker system is: http://www.digikey.dk/product-detail/en/GP1UE271RKVF/425-2515-ND/1302216

And the thing is.. Individually both sending and receiving on the Arduino works perfectly! but when the two parts of code is put together, they fail..

Cheers

Never mind.. i found the answer.. it turns out, that disabling global interrupts before sending and then enabling them again after does the job :slight_smile:

#include <avr/pgmspace.h>
#include "codes.h"
#include <IRremote.h>

const int IR = 3;

IRrecv irrecv(IR);

decode_results results;

int incomingByte = 0;

void setup() {
 Serial.begin(9600);
 irrecv.enableIRIn();
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume();
  }
  if (Serial.available() > 0) {
      incomingByte = Serial.read();
      if(incomingByte = 49) {
         sendcommand(cmd_power); 
      }
  }
}
  
void sendcommand(prog_uint16_t *cmd) {
  cli();                 <--------------- THIS PART
  pinMode(IR,OUTPUT);
  digitalWrite(IR,HIGH); 
  int t = 0;
  int addr = 0;
  for (int i=0;i<34;i++) {
      addr = (i<<1);
      digitalWrite(IR,LOW);
      t = pgm_read_word_near(cmd+addr);
      delayMicroseconds(t);
      digitalWrite(IR,HIGH);
      t = pgm_read_word_near(cmd+addr+1);
      delayMicroseconds(t);
  }
  delay(200); 
  pinMode(IR,INPUT); 
  sei();                 <---------------- THIS PART
}