Transmission finally works now, but the rc car keeps glitching(ps4 nrf24)

I'm using a ps4 controller, 2 Arduinos connected to 2 nrf24, & an rc car. The analog transmission works, but the rc car keeps lagging when receiving the inputs, any way to resolve the issue?

Video of the issue:

note:
the tx pins (CE, CSN) are (7,8) to accomodate the usb host shield
I have connected an external power supply to the rc arduino
the rc car has a brushed motor, i dont know if that plays much of a factor though as i dont know much about rc cars.

Ps4 transmitter code:

#include <PS4USB.h>

#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <SPI.h>

int Xaxis;
int Yaxis;

int dataX;
int dataY;

int data[2];

USB Usb;
PS4USB PS4(&Usb);

const uint64_t pipe = 0xE8E8F0F0E1LL; //the address of the modem, that will receive data from Arduino


RF24 radio(7, 8);

void setup() {
	Serial.begin(115200);
#if !defined(__MIPSEL__)
  while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
  if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while (1); // Halt
  }
  Serial.print(F("\r\nPS4 USB Library Started"));

  radio.begin();

	// Use PALevel low for testing purposes only (default: high)
	// PALevel now adjustable via options menu, default: high
	radio.setPALevel(RF24_PA_LOW);

	// Open a writing and reading pipe on each radio, with opposite addresses
	radio.openWritingPipe(pipe);
	//radio.enableDynamicAck();
	//radio.openReadingPipe(1, readingPipe);

}



void loop() {

  Usb.Task();

  if (PS4.connected()) {

  Xaxis=PS4.getAnalogHat(LeftHatX);
  Yaxis=PS4.getAnalogHat(LeftHatY);

  dataX = map(Xaxis, 0, 255, 0, 180);
  dataY = map(Yaxis, 0, 255, 180, 0);

  data[0] = dataX;
  data[1] = dataY;

  Serial.print(F("\r\nLeftHatX: "));
  Serial.print("Data X:"); Serial.print(dataX);
  Serial.print(F("\tLeftHatY: "));
  Serial.print("Data Y:"); Serial.print(dataY);
  radio.write(data, sizeof(data));

  }

}
type or paste code here

rc car recieving code:

#include <SPI.h>      //the communication interface with the modem
#include "RF24.h"     //the library which helps us to control the radio modem (nRF24L)
#include <Servo.h>

Servo ESC;                                                         //Create instances of type Servo. servo1 is the steering servo and servo2 is the ESC.
Servo turn;

int offset = 0; // put this with the other global variables (above the void setup() function).
int steer = 127; // put this with the other global variables (above the void setup() function).

int dataX;
int dataY;

int data[2];

RF24 radio(9, 10); //10 and 9 are a digital pin numbers to which signals CE and CSN are connected

const uint64_t pipe = 0xE8E8F0F0E1LL; //the address of the modem,that will receive data from the Arduino


void setup() {
  Serial.begin(115200);
  pinmode(5, OUTPUT);
  pinmode(3, OUTPUT);
  turn.attach(5);                                                  //Steering servo on digital pin 5
  ESC.attach(3);

  radio.begin();                    //it activates the modem
  radio.openReadingPipe(1, pipe);   //determines the address of our modem which receive data
  radio.startListening();           //enable receiving data via modem
}

void loop() {
  if (radio.available()) {
    radio.read(data, sizeof(data));
    dataX = data[0];
    dataY = data[1];
    Serial.print(F("\r\nData X:")); 
    Serial.print(dataX); //steer
    Serial.print(F("\tData Y:")); 
    Serial.print(dataY); //throttle
    //data X
    dataX = dataX + offset; // incorporate trim into steer command
    if(dataX > 180) dataX = 180; // enforce upper limit
    if(dataX < 0) dataX = 0; // enforce lower limit
    
    turn.write(map(dataX, 0, 255, 0, 180)); // write steer command
    ESC.write(map(dataY, 0, 255, 180, 0)); // write throttle command
    
  }
  else 
   {
    turn.write(90);
    ESC.write(90);
   }
    
    

  }


any help would be appreciated, thank you for taking the time to read my post.


Did you try to comment out the Serial.print() statements?
Why not:

const byte _X=0;
const byte _Y=1;
data[_X]=map(blabla);
data[_Y]=map(morebla);

I added the _ because X is too short for a variable name... especially if it is global. X_axis would be better, but I leave that to you.

Alright! Thank you very much for the suggestion. I will try it right now.

thanks for your help, unfortunately, commenting out the serial monitor did not work.

I uploaded a video of the issue just now on yt, the link is here; https://youtube.com/shorts/iDczsgIsbqE?si=KdMvGriiqNtPxj_P

Why do you have the same mapping on sender and receiver side?

If the radio is not available for a very short period, the turn and ESC will be set to 90.
Maybe you should only go to this default setting if the radio is unavailable for 0.1 sec or so...

Thanks for the reply, but yeah I think this might be the solution, I’ll try it out in a few.

Best, A-person8484

Does this imply that the radio is connected to pin1?
Usually pin 0 and 1 are reserved for USB communication...
Also better to have all pin definitions at top...

const byte radioPin=1;

Many of us would have seen this error much earlier...

No idea, I just followed along from another persons program. I’ll try removing it soon.

Oh yeah, that’s to select radio channels for the nrf24. It uses channels 1-5 & I picked channel 1.

(post deleted by author)

Thank you, the car finally works. It finally works! It’s been so long since I’ve been working on this project, just check my profile. All the usb host shields that I used, radios that I tried to get working, the coding classes I took. It was all worth it. Thank you, because of you, this project finally works. Ur my hero man.

Best, Aperson8484

Glad you got it working!
What was the essential fix?

Looking at the code it would have been the "else" in his loop

If radio.available would only trigger say 60 times a second (or whatever his send rate is)

So everytime that fails (lets say 1000 times a second) it jumps to setting the servo and esc to 90.

So 99% of the time servo and esc would be set to 90. Then there would be brief pulses where it chances when a packet comes through

Just getting rid of the “else” loop & that was it.

Wow you got it spot on, that’s exactly what happened during testing.