Trouble with Data in NRF24L01

Hello, as of now I am trying to create a remote controlled car as an independent project. Basically, I am trying to send data between a joystick to a motor driver while communicating between two NRF24L01's. Here is the code for both the receiver and transmitter below:

/*ARDUINO JOYSTICK CONTROLLED CAR (RECEIVER)  
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define enA 2  
#define in1 3
#define in2 4
#define enB 7   
#define in3 5
#define in4 6
RF24 radio(8, 9); // CE, CSN
const byte address[6] = "12345";
char receivedData[32] = "";
int  xAxis, yAxis;
int motorSpeedA = 0;
int motorSpeedB = 0;
void setup() {
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  Serial.begin(38400);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}
void loop() {
  if (radio.available()) {   // If the NRF240L01 module received data
    radio.read(&receivedData, sizeof(receivedData)); // Read the data and put it into character array
    xAxis = atoi(&receivedData[0]); // Convert the data from the character array (received X value) into integer
    Serial.print("\nX: ");
    Serial.print(xAxis);
    delay(10);
    
    radio.read(&receivedData, sizeof(receivedData));
    yAxis = atoi(&receivedData[0]);
    Serial.print("\nY: ");
    Serial.print(yAxis);
    delay(10);
  }
  
  // Y-axis used for forward and backward control
  if (yAxis < 470) {
    // Set Motor A backward
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    // Set Motor B backward
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
    motorSpeedA = map(yAxis, 470, 0, 0, 255);
    motorSpeedB = map(yAxis, 470, 0, 0, 255);
  }
  else if (yAxis > 550) {
    // Set Motor A forward
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    // Set Motor B forward
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
    motorSpeedA = map(yAxis, 550, 1023, 0, 255);
    motorSpeedB = map(yAxis, 550, 1023, 0, 255);
  }
  // If joystick stays in middle the motors are not moving
  else {
    motorSpeedA = 0;
    motorSpeedB = 0;
  }
  // X-axis used for left and right control
  if (xAxis < 470) {
    // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
    int xMapped = map(xAxis, 470, 0, 0, 255);
    // Move to left - decrease left motor speed, increase right motor speed
    motorSpeedA = motorSpeedA + xMapped;
    motorSpeedB = motorSpeedB - xMapped;
    // Confine the range from 0 to 255
    if (motorSpeedA < 0) {
      motorSpeedA = 0;
    }
    if (motorSpeedB > 255) {
      motorSpeedB = 255;
    }
  }
  if (xAxis > 550) {
    // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
    int xMapped = map(xAxis, 550, 1023, 0, 255);
    // Move right - decrease right motor speed, increase left motor speed
    motorSpeedA = motorSpeedA - xMapped;
    motorSpeedB = motorSpeedB + xMapped;
    // Confine the range from 0 to 255
    if (motorSpeedA > 255) {
      motorSpeedA = 255;
    }
    if (motorSpeedB < 0) {
      motorSpeedB = 0;
    }
  }
  // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
  if (motorSpeedA < 70) {
    motorSpeedA = 0;
  }
  if (motorSpeedB < 70) {
    motorSpeedB = 0;
  }
  analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}

/*Arduino JOYSTICK CONTROLLED CAR (TRANSMITTER)
*/

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(8,9); // CE, CSN
const byte address[6] = "12345";
char xyData[32] = "";
String xAxis, yAxis;
void setup() {
  Serial.begin(38400);
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}
void loop() {
  
  xAxis = analogRead(A1); // Read Joysticks X-axis
  yAxis = analogRead(A0); // Read Joysticks Y-axis
  Serial.println("X: " + xAxis);
  Serial.println("Y: " + yAxis);
  // X value
  xAxis.toCharArray(xyData, 5); // Put the String (X Value) into a character array
  radio.write(&xyData, sizeof(xyData)); // Send the array data (X value) to the other NRF24L01 modile
  // Y value
  yAxis.toCharArray(xyData, 5);
  radio.write(&xyData, sizeof(xyData));
  delay(10);
}

For the x and y values in the transmitter, they appear to be consistent at around 516 for the x-axis and 510 on the y-axis. However, the receiver sometimes has inaccurate values when I test it. Here is an example of what the receiver's x and y values look like:

16:51:36.277 -> X: 510
16:51:36.320 -> Y: 0
16:51:36.320 -> X: 516
16:51:36.320 -> Y: 0
16:51:36.352 -> X: 516
16:51:36.352 -> Y: 510
16:51:36.398 -> X: 516
16:51:36.398 -> Y: 0
16:51:36.398 -> X: 510
16:51:36.398 -> Y: 516
16:51:36.474 -> X: 516
16:51:36.474 -> Y: 0
16:51:36.507 -> X: 516
16:51:36.507 -> Y: 510
16:51:36.541 -> X: 516
16:51:36.541 -> Y: 510
16:51:36.541 -> X: 516
16:51:36.584 -> Y: 510
16:51:36.632 -> X: 516
16:51:36.632 -> Y: 0
16:51:36.664 -> X: 516
16:51:36.664 -> Y: 510

I think the issue has to do with how the data is being organized when it is brought over, especially considering the x values will sometimes have the expected value for the y-axis and that the y-axis will sometimes be 0. Please let me know if you have any idea and if there is anything you need in order to help then please do ask. Thank you again for your help!

You check for availability of a packet, and then go and read two packets.

Create a structure with binary x and y values and send it in a single packet.

I'm kinda new to Arduino coding, especially when it comes down to what you're describing. How exactly would I be able to do this or at least could you point me in the right direction on how I would create this.

Also, this code was taken from a video tutorial I was following so I didn't make it myself.

Untested, but probably close.
I moved the hardware manipulations inside the if (radio.available()) clause also.

#include <RF24.h>
#define enA 2  
#define in1 3
#define in2 4
#define enB 7   
#define in3 5
#define in4 6
RF24 radio(8, 9); // CE, CSN
const byte address[6] = "12345";

struct Packet {
int  xAxis;
int  yAxis;
} rxed;

int motorSpeedA = 0;
int motorSpeedB = 0;
void setup() {
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  Serial.begin(38400);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}
void loop() {
  if (radio.available()) {
    radio.read(&rxed, sizeof(rxed));
    Serial.print("\nX: ");
    Serial.print(rxed.xAxis);
    Serial.print("\nY: ");
    Serial.print(rxed.yAxis);
  
  // Y-axis used for forward and backward control
  if (rxed.yAxis < 470) {
    // Set Motor A backward
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    // Set Motor B backward
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
    motorSpeedA = map(rxed.yAxis, 470, 0, 0, 255);
    motorSpeedB = map(rxed.yAxis, 470, 0, 0, 255);
  }
  else if (rxed.yAxis > 550) {
    // Set Motor A forward
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    // Set Motor B forward
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
    motorSpeedA = map(rxed.yAxis, 550, 1023, 0, 255);
    motorSpeedB = map(rxed.yAxis, 550, 1023, 0, 255);
  }
  // If joystick stays in middle the motors are not moving
  else {
    motorSpeedA = 0;
    motorSpeedB = 0;
  }
  // X-axis used for left and right control
  if (rxed.xAxis < 470) {
    // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
    int xMapped = map(rxed.xAxis, 470, 0, 0, 255);
    // Move to left - decrease left motor speed, increase right motor speed
    motorSpeedA = motorSpeedA + xMapped;
    motorSpeedB = motorSpeedB - xMapped;
    // Confine the range from 0 to 255
    if (motorSpeedA < 0) {
      motorSpeedA = 0;
    }
    if (motorSpeedB > 255) {
      motorSpeedB = 255;
    }
  }
  if (rxed.xAxis > 550) {
    // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
    int xMapped = map(rxed.xAxis, 550, 1023, 0, 255);
    // Move right - decrease right motor speed, increase left motor speed
    motorSpeedA = motorSpeedA - xMapped;
    motorSpeedB = motorSpeedB + xMapped;
    // Confine the range from 0 to 255
    if (motorSpeedA > 255) {
      motorSpeedA = 255;
    }
    if (motorSpeedB < 0) {
      motorSpeedB = 0;
    }
  }
  // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
  if (motorSpeedA < 70) {
    motorSpeedA = 0;
  }
  if (motorSpeedB < 70) {
    motorSpeedB = 0;
  }
  analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}
}

I think it would be a good idea to switch off the motors,
if you have not received a valid packet for some time, don't you think so?

It seems to be slightly better, however, I noticed the range that is printing into my serial monitor is much higher (from about 0 to 13000-ish) and there are some instances where my values will still have jumps.

Here is what I'm trying to explain:

18:32:37.023 -> X: 12597
18:32:37.023 -> Y: 54
18:32:37.023 -> X: 48
18:32:37.023 -> Y: 54
18:32:37.055 -> X: 12597
18:32:37.055 -> Y: 54
18:32:37.145 -> X: 48
18:32:37.145 -> Y: 54
18:32:37.179 -> X: 12597
18:32:37.179 -> Y: 54
18:32:37.179 -> X: 48
18:32:37.179 -> Y: 54
18:32:37.179 -> X: 12597
18:32:37.179 -> Y: 54
18:32:37.179 -> X: 48
18:32:37.179 -> Y: 54
18:32:37.225 -> X: 12597
18:32:37.225 -> Y: 54
18:32:37.225 -> X: 48
18:32:37.225 -> Y: 54
18:32:37.256 -> X: 12597
18:32:37.256 -> Y: 54

These are the values shown when I move the joystick upwards. It seems that the x-value consistently switches between 48 and 12597. The same does happen when I move the joystick downwards, but the y-axis is affected. I'm sorry that I can't figure this stuff out I'm really trying to figure out why and I greatly appreciate you helping out.

Post both new sketches.

The new sketch for the receiver is as you posted and I didn't change anything for the transmitter. Am I supposed to change something in the transmitter sketch?

Nevermind, I realized I had to change both scripts and I was able to get it to work. Thank you again for all of your help!

Here were both sketches that helped it work:

/*Arduino JOYSTICK CONTROLLED CAR (TRANSMITTER)
*/

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(8,9); // CE, CSN
const byte address[6] = "12345";

struct Packet {
  int xAxis;
  int yAxis;
} rxed;

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}
void loop() {
  
  rxed.xAxis = analogRead(A1); // Read Joysticks X-axis
  rxed.yAxis = analogRead(A0); // Read Joysticks Y-axis
  Serial.println(rxed.xAxis);
  Serial.println(rxed.yAxis);

  radio.write(&rxed, sizeof(rxed));
  delay(10);
}
#include <RF24.h>
#define enA 2  
#define in1 3
#define in2 4
#define enB 7   
#define in3 5
#define in4 6
RF24 radio(8, 9); // CE, CSN
const byte address[6] = "12345";

struct Packet {
int  xAxis;
int  yAxis;
} rxed;

int motorSpeedA = 0;
int motorSpeedB = 0;
void setup() {
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  Serial.begin(38400);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}
void loop() {
  if (radio.available()) {
    radio.read(&rxed, sizeof(rxed));
    Serial.print("\nX: ");
    Serial.print(rxed.xAxis);
    Serial.print("\nY: ");
    Serial.print(rxed.yAxis);
  
  // Y-axis used for forward and backward control
  if (rxed.yAxis < 470) {
    // Set Motor A backward
    digitalWrite(in1, HIGH);
    digitalWrite(in2, LOW);
    // Set Motor B backward
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);
    // Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
    motorSpeedA = map(rxed.yAxis, 470, 0, 0, 255);
    motorSpeedB = map(rxed.yAxis, 470, 0, 0, 255);
  }
  else if (rxed.yAxis > 550) {
    // Set Motor A forward
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    // Set Motor B forward
    digitalWrite(in3, LOW);
    digitalWrite(in4, HIGH);
    // Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
    motorSpeedA = map(rxed.yAxis, 550, 1023, 0, 255);
    motorSpeedB = map(rxed.yAxis, 550, 1023, 0, 255);
  }
  // If joystick stays in middle the motors are not moving
  else {
    motorSpeedA = 0;
    motorSpeedB = 0;
  }
  // X-axis used for left and right control
  if (rxed.xAxis < 470) {
    // Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
    int xMapped = map(rxed.xAxis, 470, 0, 0, 255);
    // Move to left - decrease left motor speed, increase right motor speed
    motorSpeedA = motorSpeedA + xMapped;
    motorSpeedB = motorSpeedB - xMapped;
    // Confine the range from 0 to 255
    if (motorSpeedA < 0) {
      motorSpeedA = 0;
    }
    if (motorSpeedB > 255) {
      motorSpeedB = 255;
    }
  }
  if (rxed.xAxis > 550) {
    // Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
    int xMapped = map(rxed.xAxis, 550, 1023, 0, 255);
    // Move right - decrease right motor speed, increase left motor speed
    motorSpeedA = motorSpeedA - xMapped;
    motorSpeedB = motorSpeedB + xMapped;
    // Confine the range from 0 to 255
    if (motorSpeedA > 255) {
      motorSpeedA = 255;
    }
    if (motorSpeedB < 0) {
      motorSpeedB = 0;
    }
  }
  // Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
  if (motorSpeedA < 70) {
    motorSpeedA = 0;
  }
  if (motorSpeedB < 70) {
    motorSpeedB = 0;
  }
  analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
  analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
}
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.