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.