How to fix error sending and receiving data in Arduino Uno + nRF24L01?

i have a sourcecode for my arduino uno + nrf24l01 project. However it does not work and there is often an error where data sometimes fails to be sent and sometimes the data is not received by the receiver. How i can fix it?

here I provide source code for the sender and receiver
The Sender

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"

RF24 radio (9,10);
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };

int randNumber;

void setup() {
Serial.begin(57600);
printf_begin();
randomSeed(analogRead(0));
randNumber = random(9999);
radio.begin();
radio.setPALevel(RF24_PA_MAX);
radio.startListening();
radio.printDetails();
}

void loop() {
  radio.openWritingPipe(pipes[0]);
 int idg=12345;
 int kg=1212018;
 int mac=5678;
 int nr=randNumber;
 int m1=idg^kg;
printf("nr= %d \n",nr);
printf("result m1= %d \n\n", m1);

 radio.stopListening();
 bool a=radio.write(&nr, sizeof(int));
  if(a)
{
 printf("successfully sent nr %d \n",nr);

}
 else
 printf("failed to send nr\n");

 bool b=radio.write(&m1, sizeof(int));
 if(b)
{
 printf("successfully sent m1 %d \n",m1);

}
 else
 printf("failed to send m1\n");
  
bool c=radio.write(&mac, sizeof(int));
 if(c)
{
 printf("successfully sent mac %d \n\n",mac);

}
 else
 printf("failed to send mac\n");

if(radio.available()){
radio.startListening();
int nt;
int m5;
int mac;

radio.read(&nt, sizeof(int));
radio.read(&m5, sizeof(int));
radio.read(&mac, sizeof(int));
printf("get nt= %d \n",nt);
printf("get m5= %d \n",m5);
printf("get mac= %d \n\n",mac);
}

else{
 printf("empty \n");
}

printf("----------------------\n\n");

}

The receiver

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"

RF24 radio (9,10);
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
int randNumber;

void setup() {
 Serial.begin(57600);
printf_begin();
radio.begin();
radio.setPALevel(RF24_PA_MAX);
radio.startListening();
radio.printDetails();
}

void loop() {
   if(radio.available())
  {
radio.openReadingPipe(1,pipes[0]);
int idg=12345;
int idt=23456;
int kg=1212018;
int kt=1112018;
int nr;
int m1;
int mac;

radio.read(&nr, sizeof(int));
radio.read(&m1, sizeof(int));
radio.read(&mac, sizeof(int));
printf("get nr= %d \n",nr);
printf("get m1= %d \n",m1);
printf("get mac= %d \n\n",mac);
int idg1=m1^kg;
printf("idg1= %d \n\n", idg1);

if(idg==idg1){
  printf("idg dan idg1 sama\n");
randomSeed(analogRead(0));
randNumber = random(9999);
  int nt=randNumber;
  printf("nt= %d \n",nt);
  int m2=idg^nt^nr;
  printf("m2= %d \n",m2);
  int m3=idt^nt;
  printf("m3= %d \n",m3);
  int m4=m3^kt;
  printf("m4= %d \n",m4);
  int m5=kg^m2;
  printf("m5= %d \n",m5);
  int m51=kg^m4;
  printf("m51= %d \n",m51);

radio.stopListening();
  bool a=radio.write(&nt, sizeof(int));
  if(a)
{
 printf("successfully sent nt %d \n",nt);

}
 else
 printf("failed to send nt\n");

 bool b=radio.write(&m5, sizeof(int));
  if(b)
{
 printf("successfully sent m5 %d \n",m5);

}
 else
 printf("failed to send m5\n");

bool c=radio.write(&m51, sizeof(int));
  if(c)
{
 printf("successfully sent m51 %d \n",m51);

}
 else
 printf("failed to send m51\n");
 
 bool d=radio.write(&mac, sizeof(int));
  if(d)
{
 printf("successfully sent mac %d \n",mac);

}
 else
 printf("failed to send mac\n");
}
else
printf("idg and idg1 different and finish\n");
}
else
printf("empty \n");

printf("-------------------\n\n");

}

printf.h (657 Bytes)

 int kg=1212018;

What is the largest number that a signed int can hold ?

The advice in Reply #1 may fix the problem.

If not this Simple nRF24L01+ Tutorial may help.

...R

After i changed int to long int, there are still error? Do you have another solutions?

hidzak:
Do you have another solutions?

See Reply #2

...R