Strange problem with IR Reception

Hello,

I’m working on windows, Arduino V1.8.9 with two official Arduino Micro to transmit datas over IR back and forth between the two of them. I attached my schematic to the post.

I uploaded in each or them those quick test sketches :

Emission:

#include <IRremote.h>
IRsend irsend;


void setup()
{
  Serial.begin(9600);

}

void loop() {
  
    while (1) {
      irsend.sendNEC(0xa90, 16); // NEC TV power code
      delay(100);
    }
  
}

Reception:

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


int RECV_PIN = 3;
IRrecv irrecv(RECV_PIN);
decode_results results;

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

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
  }  
}

Everything work smoothly, the datas are transmited perfectly.

The problem is when I try to make a code with both reception and emission in it, reception doesn’t work anymore, the condition never trigger. here is my code (Only one of the board contain this sketch, the other one just transmit data continuously with the initial “emission” sketch):

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

int i = 0;
IRsend irsend;
int RECV_PIN = 3;
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long time_now = 0;
int period = 100; 

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

void sending()
{
  irsend.sendNEC(0xa90, 16); // NEC TV power code
  delay(100);
  //Serial.println("Sending");
}

void receiving()
{
  Serial.println("Receiving");
  irrecv.resume(); // Receive the next value
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
    delay(500);
    
  }
}

void loop() {

for (i = 0; i < 20; i++) 
{
sending();  

}

time_now = millis();

while (millis() < time_now + period)
{
  receiving();
   
}
Serial.println("end of loop");
    
}

What could be wrong ? I’m at a loss here. the code enter my receiving function but the “if” is never executed. I tried to put a " irrecv.resume()" before in case it needed a reset but nop it doesn’t work.

Here is some info about the library I use :IRremote Library, Send & Receive Infrared Remote Control

thank you in advance,

GotGot

You have a delay of 500 mS in function receiving(), but in the loop() you call receiving() during a period of 100mS (which is hardly enough time to receive even a single character). Also, the Serial.print debug message may itself interfere with the timings.

Start by commenting out any code in the loop() which is not for receiving, until it works again

Thanks for your answer,

I corrected the delay thing and commented various part of my code and the line that messes up everything is this one :

irsend.sendNEC(0xa90, 16); // NEC TV power code

When I comment this line, everything work fine, the datas are received. .

It is probably something to reset after a send but I have no idea what. the library is quite complicated.

OK. To save me hunting through all the elements of that huge library, can you tell me how you came to choose pin13 as the output pin for the send part ?

The library apparently uses timers for both the receive and transmit part. I could imagine a clash somewhere.

Pin 13 for the output was hard coded in the library, I didn't choose it, I just went through the library and checked the send pin that was defined for my type of board.

Pins are set in the file boarddefs.h

I think it is line 392 for my ATMEGA 32U4

Post a link to the example or instructions you are following.

my code partially come from this post:

OK. Just to confirm if there is a clash between the resources used to send and to receive the IR codes, try taking a working "receive" sketch and adding this to the end of setup():

 irsend.sendNEC(0xa90, 16); // NEC TV power code
 delay(100);

Then post the code that use.

yes when I add those two lines, my working reception code stops working.

OK. If you add these 3 lines to the end of setup() inyour working receive sketch, what happens?

irsend.sendNEC(0xa90, 16); // NEC TV power code
delay(100);
irrecv.enableIRIn ( ) ;  // initialize ?

it actually works,

the arduino is receiving the infos correctly, no problem.

I changed the code a bit according to what you told me and it worked a little better but it is still weird. It receives and prints the correct value (0xa90) once in a while, the rest of the time, the “if” condition isn’t triggered.

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

int i = 0;
IRsend irsend;
int RECV_PIN = 3;
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long time_now = 0;
int period = 100; 

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

void loop() {

for (i = 0; i < 20; i++) 
{
  
  irsend.sendNEC(0xa90, 16); // NEC TV power code
  delay(100);
  irrecv.enableIRIn ( ) ;  // initialize ? CHANGED HERE
  //Serial.println("Sending");
}

time_now = millis();

while (millis() < time_now + period)
{ 
  //irrecv.enableIRIn ( ) ;  // initialize ?
  //Serial.println("Receiving");
  delay(100);
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
    //delay(150);
    
  }
}
//Serial.println("end of loop");
    
}

OK. Don't do this every time in a loop. Just do it only when changing from sending to receiving:

irrecv.enableIRIn ( ) ; // initialize ? CHANGED HERE

Thanks I'll modify that.
Do you have any other clue about how to improve it ? because it is still not working at all, just doing less bad than before I guess.
It fails to receive the data 19 times out of 20.

in anycase, thanks a lot for your help !

Remove the delay() statements.

I finally fixed it! here it is:

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


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

int i = 0;
unsigned long time_now = 0;
int period = 1000; 

void setup() // setting serial and IR Send
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
 }

void loop() {

  for (i = 0; i < 20; i++) // Sending data 20 times
    {  
      irsend.sendNEC(0xa90, 16); // NEC TV power code
      irrecv.enableIRIn ( ) ;  // initialize ?
    }
  
  irrecv.enableIRIn ( ) ;  // reinitialising IR Receive to avoid conflics
  time_now = millis();
  
  while (millis() < time_now + period) // receiving during 1 second
    {  
      while (irrecv.decode(&results))
        {
          Serial.println(results.value, HEX);
          delay(50);
          irrecv.resume(); // Receive the next value
        }
      
      }   
  }

Thanks a lot for your help 6v6gt!

it was some delay messing up everythin and also my “millis()” loop which was too short to allow proper receiving.