NRF24L01 lagging problem

hi I'm working on a project witch I control a servo via one mpu6050 one arduino nano, one arduino uno r3 and two nrf24l01..the project rarelly works for a little and then it lags why thiw happens?
here are my codes

//transmitter
#include <RF24.h>
#include <SPI.h>
#define CE_PIN 9
#define CSN_PIN 10
#include "MPU6050.h"
#include "I2Cdev.h"
#include "Wire.h"
MPU6050 mpu;
int16_t ax;
int valx;
int prevValx;
int ay, az, gx, gy, gz;

const uint64_t pipe = 0xE8E8F0F0E1LL;

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

int mpu1[1];

void setup() {
  Wire.begin();
  Serial.begin(9600);
  radio.begin(); radio.openWritingPipe(pipe);
  mpu.initialize();
}

void loop() {
  mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  valx = map(ax, -1800, 1800, 0, 180);
  mpu1[0] = valx; // x_data from mpu
  //y_data from mpu
  radio.write(mpu1, sizeof(mpu1) );
}
//receiver
#include <SPI.h>
#include <RF24.h>
#include "Servo.h"
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN   9
#define CSN_PIN 10

/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
int mpu1[1];  // 1 element array holding Joystick readings
Servo servox;
void setup()   /****** SETUP: RUNS ONCE ******/
{
Serial.begin(9600);
delay(1000);
Serial.println("Nrf24L01 Receiver Starting");
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();;
servox.attach(2);
}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
if ( radio.available() )
{
   // Read the data payload until we've received everything
   bool done = false;
   while (!done)
   {
     // Fetch the data payload
     done = radio.read( mpu1, sizeof(mpu1) );
     delay(100);
     
     Serial.print(mpu1[0]);
     servox.write(mpu1[0]);
   }
}
else
{   
     Serial.println("No radio available");
}

}
     done = radio.read( mpu1, sizeof(mpu1) );
     delay(100);
     
     Serial.print(mpu1[0]);
     servox.write(mpu1[0]);

The delay(100) means you can only receive 10 messages per second. Your transmitter code looks like it will run faster than that.

Thanks for your reply.
So what I should change in code?

You could try the following:

  • Remove the delay from the receive program so that it can run as fast as possible (limited just by the time for the call to the radio, print and servo functions.

  • Look at the BlinkWithoutDelay example program in the Arduino IDE.

  • Add the BlinkWithoutDelay approach inside loop in your send program so that the following code is executed, say, every 50 or 100 milliseconds.

  mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  valx = map(ax, -1800, 1800, 0, 180);
  mpu1[0] = valx; // x_data from mpu
  //y_data from mpu
  radio.write(mpu1, sizeof(mpu1) );

against the same problem

on receiver serial monitor it says me No radio available
why?

Please post the new versions of both programs.

//transmitter
#include <RF24.h>
#include <SPI.h>
#define CE_PIN 9
#define CSN_PIN 10
#include "MPU6050.h"
#include "I2Cdev.h"
#include "Wire.h"
MPU6050 mpu;
int16_t ax;
int valx;
int prevValx;
int ay, az, gx, gy, gz;

const uint64_t pipe = 0xE8E8F0F0E1LL;

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

int mpu1[1];

void setup() {
  Wire.begin();
  Serial.begin(9600);
  radio.begin(); radio.openWritingPipe(pipe);
  mpu.initialize();
}

void loop() {
 
  mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  valx = map(ax, -1800, 1800, 0, 180);
  mpu1[0] = valx; // x_data from mpu
  
  radio.write(mpu1, sizeof(mpu1) );
}

//receiver

#include <SPI.h>
#include <RF24.h>
#include "Servo.h"
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN   9
#define CSN_PIN 10

/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
int mpu1[1];  // 1 element array holding Joystick readings
Servo servox;
void setup()   /****** SETUP: RUNS ONCE ******/
{
Serial.begin(57600);
delay(1000);
Serial.println("Nrf24L01 Receiver Starting");
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();;
servox.attach(2);
}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
     
  
if ( radio.available() )
{
   // Read the data payload until we've received everything
   bool done = false;
   while (!done)
   {
     // Fetch the data payload
     done = radio.read( mpu1, sizeof(mpu1) );
     
     
     Serial.print(mpu1[0]);
     servox.write(mpu1[0]);
   }
}
else
{   
     Serial.println("No radio available");
}

}

You could try the change I suggested in reply #3.

  • Look at the BlinkWithoutDelay example program in the Arduino IDE.

  • Add the BlinkWithoutDelay approach inside loop in your send program so that the following code is executed, say, every 50 or 100 milliseconds.

I don't know how could you make the necessary changes in code and then post it please?

..try out first..:
In transmitter loop - add: delay(100);

I use the same connection of nrf and on arduino uno and on arduino nano maybe this is the problem?

please help me

No delays on the reciever side.
avoid serial output on receiver side, as it is slow

Add delay on sender side to allow receiver to act on incoming messages. (no overrun)