Page 1 of 1

IR library

Posted: Sun May 15, 2016 5:07 pm
by mrburen121
Hello,

I found at Github your IRSendRev library . In the examples sending and receiving IR signals are separated. I tried to combine the codes so both my Arduino's can send and receive data (half-duplex). But I whatever I tried it doesn't work. With the code below the Arduino's only sending data and no one is receiving data.

What I'm doing wrong?

Code: Select all

#include <IRSendRev.h>

#define BIT_LEN         0
#define BIT_START_H     1
#define BIT_START_L     2
#define BIT_DATA_H      3
#define BIT_DATA_L      4
#define BIT_DATA_LEN    5
#define BIT_DATA        6

const int pinRecv = 2;              // ir receiver connect to D2
const int ir_freq = 38;                 // 38k
unsigned char dtaSend[20];
unsigned char dta[20];

void dtaInit()
{
  dtaSend[BIT_LEN]        = 11;     // all data that needs to be sent
  dtaSend[BIT_START_H]    = 179;    // the logic high duration of "Start"
  dtaSend[BIT_START_L]    = 90;     // the logic low duration of "Start"
  dtaSend[BIT_DATA_H]     = 11;     // the logic "long" duration in the communication
  dtaSend[BIT_DATA_L]     = 33;     // the logic "short" duration in the communication

  dtaSend[BIT_DATA_LEN]   = 6;      // Number of data which will sent. If the number is other, you should increase or reduce dtaSend[BIT_DATA+x].

  dtaSend[BIT_DATA + 0]     = 'T';    // data that will sent
  dtaSend[BIT_DATA + 1]     = 'e';
  dtaSend[BIT_DATA + 2]     = 's';
  dtaSend[BIT_DATA + 3]     = 't';
  dtaSend[BIT_DATA + 4]     = 'e';
  dtaSend[BIT_DATA + 5]     = '!';
}

void setup()
{
  Serial.begin(115200);
  IR.Init(pinRecv);
  Serial.println("init over");
  dtaInit();
  pinMode(4, INPUT);      // Button to send data
  pinMode(8, OUTPUT);     // LED for indicaton receiving data
  pinMode(9, OUTPUT);     // LED for indication sending data
}

void loop()
{

  int a = digitalRead(4);
  if (a == HIGH) {          // if button pressed send data
    void Send(unsigned char *dta_buf, unsigned char ifreq);
    Serial.println("sending data");
    
    digitalWrite(9, HIGH);    // Send data LED indication
    delay(500);
    digitalWrite(9, LOW);
  }

  else {
    if (IR.IsDta())                 // get IR data
    {
      IR.Recv(dta);               // receive data to dta
      Serial.println("+------------------------------------------------------+");
      Serial.print("LEN = ");
      Serial.println(dta[BIT_LEN]);
      Serial.print("START_H: ");
      Serial.print(dta[BIT_START_H]);
      Serial.print("\tSTART_L: ");
      Serial.println(dta[BIT_START_L]);

      Serial.print("DATA_H: ");
      Serial.print(dta[BIT_DATA_H]);
      Serial.print("\tDATA_L: ");
      Serial.println(dta[BIT_DATA_L]);

      Serial.print("\r\nDATA_LEN = ");
      Serial.println(dta[BIT_DATA_LEN]);

      Serial.print("DATA: ");
      for (int i = 0; i < dta[BIT_DATA_LEN]; i++)
      {
        Serial.print("0x");
        Serial.print(dta[i + BIT_DATA], HEX);
        Serial.print("\t");
      }
      Serial.println();

      Serial.print("DATA: ");
      for (int i = 0; i < dta[BIT_DATA_LEN]; i++)
      {
        Serial.print(dta[i + BIT_DATA], DEC);
        Serial.print("\t");
      }
      Serial.println();
      Serial.println("+------------------------------------------------------+\r\n\r\n");
      
      digitalWrite(8, HIGH);    // Receive data LED indication
      delay(500);
      digitalWrite(8, LOW);
    }
  }
}

Re: IR library

Posted: Wed May 18, 2016 2:02 pm
by kavi
Hello,

Please disable the receive interrupt while sending the data and enable the receive interrupt again.

Thanks and regards

Re: IR library

Posted: Fri May 20, 2016 6:15 pm
by mrburen121
Thank you for your reply. I added #include <IRSendRevInt.h> so I can use the code TIMER_ENABLE_INTR; and TIMER_DISABLE_INTR;
I tried to enable and disable the interrupt but it don't work. Where do I need to place the enable and disable?

Code: Select all

#include <IRSendRev.h>
#include <IRSendRevInt.h>

#define BIT_LEN         0
#define BIT_START_H     1
#define BIT_START_L     2
#define BIT_DATA_H      3
#define BIT_DATA_L      4
#define BIT_DATA_LEN    5
#define BIT_DATA        6

const int pinRecv = 2;              // ir receiver connect to D2

const int ir_freq = 38;                 // 38k

unsigned char dtaSend[20];

void dtaInit()
{
  dtaSend[BIT_LEN]        = 11;     // all data that needs to be sent
  dtaSend[BIT_START_H]    = 179;    // the logic high duration of "Start"
  dtaSend[BIT_START_L]    = 90;     // the logic low duration of "Start"
  dtaSend[BIT_DATA_H]     = 11;     // the logic "long" duration in the communication
  dtaSend[BIT_DATA_L]     = 33;     // the logic "short" duration in the communication

  dtaSend[BIT_DATA_LEN]   = 6;      // Number of data which will sent. If the number is other, you should increase or reduce dtaSend[BIT_DATA+x].

  dtaSend[BIT_DATA + 0]     = 'c';    // data that will sent
  dtaSend[BIT_DATA + 1]     = 31;
  dtaSend[BIT_DATA + 2]     = 'l';
  dtaSend[BIT_DATA + 3]     = 'l';
  dtaSend[BIT_DATA + 4]     = 'o';
  dtaSend[BIT_DATA + 5]     = '!';
}


void setup()
{
  Serial.begin(115200);
  IR.Init(pinRecv);
  Serial.println("init over");
  dtaInit();
  pinMode(4, INPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
}

unsigned char dta[20];

void loop()
{


  int b = digitalRead(4);
  if (b == HIGH) {
    TIMER_DISABLE_INTR;//stop interrupt
    //void Send(unsigned char *dta_buf, unsigned char ifreq);
    IR.Send(dtaSend, 38);
    Serial.println("sturende");
    digitalWrite(9, HIGH);
    delay(500);
    digitalWrite(9, LOW);

  }

  else {
    if (IR.IsDta()) // get IR data
      TIMER_ENABLE_INTR;//start interrupt
    {
      IR.Recv(dta);               // receive data to dta
      //IR.Recv(unsigned char *dta);
      if (dta[BIT_DATA + 0] == 99 && dta[BIT_DATA + 1] == 31) {
        Serial.println("+------------------------------------------------------+");
        Serial.print("LEN = ");
        Serial.println(dta[BIT_LEN]);
        Serial.print("START_H: ");
        Serial.print(dta[BIT_START_H]);
        Serial.print("\tSTART_L: ");
        Serial.println(dta[BIT_START_L]);

        Serial.print("DATA_H: ");
        Serial.print(dta[BIT_DATA_H]);
        Serial.print("\tDATA_L: ");
        Serial.println(dta[BIT_DATA_L]);

        Serial.print("\r\nDATA_LEN = ");
        Serial.println(dta[BIT_DATA_LEN]);

        Serial.print("DATA: ");
        for (int i = 0; i < dta[BIT_DATA_LEN]; i++)
        {
          Serial.print("0x");
          Serial.print(dta[i + BIT_DATA], HEX);
          Serial.print("\t");
        }
        Serial.println();

        Serial.print("DATA: ");
        for (int i = 0; i < dta[BIT_DATA_LEN]; i++)
        {

          Serial.print(dta[i + BIT_DATA], DEC);
          Serial.print("\t");

        }
        Serial.println();
        Serial.println("+------------------------------------------------------+\r\n\r\n");
        digitalWrite(8, HIGH);
        delay(500);
        digitalWrite(8, LOW);

      }
    }
  }
  TIMER_RESET;
}