Problem with the first recieved packet not displayed (Arduino with MRF24j40MA)

Hello guys

I'm trying to make two arduino card communicate, one is a sender and the other only receive packet from the first one and then display them.

My problem is that the first recieved packet is not displayed unlike the other packets(displayed normally)

here's the script for the reciever :

#include <SPI.h>
#include <mrf24j.h>
#include <dht.h>

dht DHT;

#define DHT22_PIN 7

const int pin_reset = 6;
const int pin_cs = 52; // default CS pin on ATmega8/168/328
const int pin_interrupt = 5; // default interrupt pin on ATmega8/168/328

Mrf24j mrf(pin_reset, pin_cs, pin_interrupt);

long last_time;
long tx_interval = 1000;

void setup() {
 
  SPI.setBitOrder(MSBFIRST) ;
    SPI.setDataMode(SPI_MODE0);
    SPI.begin();
  Serial.begin(115200);
 
  mrf.reset();
  mrf.init();
 
  mrf.set_pan(0x1234);
  // This is _our_ address
  mrf.address16_write(0x6002);

  // uncomment if you want to receive any packet on this channel
//  mrf.set_promiscuous(true);
 
   //uncomment if you want to enable PA/LNA external control
  //mrf.set_palna(true);
 
  // uncomment if you want to buffer all PHY Payload
  mrf.set_bufferPHY(true);

  attachInterrupt(pin_interrupt, interrupt_routine, CHANGE); // interrupt 0 equivalent to pin 2(INT0) on ATmega8/168/328
  last_time = millis();
  interrupts();
}

void interrupt_routine() {
    mrf.interrupt_handler(); // mrf24 object interrupt routine
}

void loop() {
    mrf.check_flags(&handle_rx, &handle_tx);
    unsigned long current_time = millis();

}

void handle_rx() {

    Serial.print("received a packet ");Serial.print(mrf.get_rxinfo()->frame_length, DEC);Serial.println(" bytes long");
   
    if(mrf.get_bufferPHY()){
      Serial.println("Packet data (PHY Payload):");
      for (int i = 0; i < mrf.get_rxinfo()->frame_length; i++) {
          Serial.print(mrf.get_rxbuf()[i]);
      }
    }
   

    Serial.println("\r\nASCII data (relevant data):");
    for (int i = 0; i < mrf.rx_datalength(); i++) {
        Serial.write(mrf.get_rxinfo()->rx_data[i]);
    }
   
    Serial.print("\r\nLQI/RSSI=");
    Serial.print(mrf.get_rxinfo()->lqi, DEC);
    Serial.print("/");
    Serial.println(mrf.get_rxinfo()->rssi, DEC);
}

void handle_tx() {

    if (mrf.get_txinfo()->tx_ok) {
        Serial.println("TX went ok, got ack \n");
    } else {
        Serial.print("TX failed after \n ");Serial.print(mrf.get_txinfo()->retries);Serial.println(" retries\n");
    }
}

I put in the attachement the output of the serial monitor

Thanks for any help or guidance

const int pin_interrupt = 5; // default interrupt pin on ATmega8/168/328

Pin 5 is not an external interrupt pin on the 328-based Arduinos.

  attachInterrupt(pin_interrupt, interrupt_routine, CHANGE); // interrupt 0 equivalent to pin 2(INT0) on ATmega8/168/328

So, why are you attaching an interrupt handler for interrupt 5? Even the Mega doesn't have that many external interrupts.

What causes handle_rx() and handle_tx() to be called? If those are called in response to interrupts, you can NOT be using Serial.print() in those functions.

Thank you for the replay :slight_smile:

Iam using Arduino Due, and i found that code as an example sketch to start with in the mrf24j40 library.

I don't know what i have to do but should i change the pin_interrupt into 2 I mean this way :

const int pin_interrupt = 2;

For the handle_rx() and handle_tx() they are called in loop each time a reception a transmission is detected. What check this is the following line :

mrf.check_flags(&handle_rx, &handle_tx);

Tnk you :slight_smile: