Arduino Nano R/C car radio communication issues

I have been working on a remote control car using two nrf24lo1 modules (with adaptor boards) 2 DS04-NFC servo motors 2 Arduino Nanos and a joystick.

It has taken at least three or four months now and it still will not work. I have made the code as best as I can but it still won't do anything.

Here is the controller code:

// RF24 - Version: Latest 
#include "RF24.h"
#include <SPI.h>

// This is just the way the RF24 library works:
// Hardware configuration: Set up nRF24L01 radio on SPI bus (pins 10, 11, 12, 13) plus pins 7 & 8
RF24 radio(7, 8);

byte addresses[][6] = {"1Node","2Node"};

// Define the Joystick Connections
int joyY = 0;
int joyX = 1;

int valX;
int valY;

void setup() {

  serial.begin();
   Serial.println("THIS IS THE TRANSMITTER CODE - YOU NEED THE OTHER ARDIUNO TO SEND BACK A RESPONSE");
  
  radio.begin();                    //start radio communication

  radio.setPALevel(RF24_PA_LOW);    //power level

  radio.setDataRate(RF24_2MBPS);   //make it quick

  radio.setChannel(124);             // Use a channel unlikely to be used by Wifi

  radio.openWritingPipe(addresses[1]);       //send
  radio.openReadingPipe(1, addresses[0]);    //recieve
  
}

void loop() {
  
  valX = analogRead(joyX);
  valY = analogRead(joyY);
  
  uint8_t data[2]; // defines size of array
  data[0] = valX;  // puts the value from the variable valX into the first element 
                   // note elements numbered from 0
                   // also assumes that valX is defined as uint8_t or byte
  data[1] = valY;

  radio.write( &data, sizeof(data) );

  // Show user what we sent
  
  Serial.print("Sent: ");
  Serial.print(valX);
  Serial.print(valY);

}



and here is the car code:

// RF24 - Version: Latest 
#include "RF24.h"
               
#include <SPI.h>
                
// Servo - Version: Latest 
#include <Servo.h>
               
                
// This is just the way the RF24 library works:
// Hardware configuration: Set up nRF24L01 radio on SPI bus (pins 10, 11, 12, 13) plus pins 7 & 8
RF24 radio(7, 8);

byte addresses[][6] = {"1Node","2Node"};

int valX;
int valY;

Servo myservo1;
int myservo1Pin = 3;

Servo myservo2;
int myservo2Pin = 4;

void setup() {

  Serial.begin(9600);
  Serial.println("THIS IS THE RECEIVER CODE - YOU NEED THE OTHER ARDUINO TO TRANSMIT");
  
  myservo1.attach(myservo1Pin);
  myservo2.attach(myservo2Pin);
  
  radio.begin();                    //start radio communication

  radio.setPALevel(RF24_PA_LOW);    //power level

  radio.setDataRate(RF24_2MBPS);   //make it quick

  radio.setChannel(124);             // Use a channel unlikely to be used by Wifi

  radio.openReadingPipe(1, addresses[1]);    //recieve
  
  radio.startListening();          //start listening for radio

}

void loop() {
  
  // This is what we receive from the other device (the transmitter)
  unsigned char data;

  // Is there any data for us to get?
  if ( radio.available()) {

  // Go and read the data and put it into that variable
  while (radio.available()) {
  radio.read( &data, sizeof(unsigned int));
  }
  
  radio.stopListening();
  
  Serial.print("recieved");
  Serial.print(data);
  
  }
  
  {

  if (valX < 460) // turn left
  {
  valY = map(valY, 0, 1023, 0, 180);
  valX = map(valX, 0, 1023, 0, 90);
  myservo1.write(valY-valX/2);                    // servo 1 slows
  myservo2.write(180-valY);
  delay(15);
  }
  else if (valX > 564) // turn right
  {
  valY = map(valY, 0, 1023, 0, 180);
  valX = map(valX, 0, 1023, 0, 90);
  myservo1.write(valY);
  myservo2.write((180-valY)-valX/2);              // servo 2 slows
  delay(15);
  }
  
  }
  
  radio.startListening();
  
}

I used Ralf Bacon's code from his video as a base ( #73 nRF24L01 Send (and receive) data with your Arduino! - YouTube )

Any help would be appreciated so much!

thank you

Jethro Crooke

The receiver code looks to be incomplete

Do you know what I would need to add?

Some code to convert the received data to valX and valY, bu that doesn't address the problem in the transmitter code.

I thought that was just the radio.read command.

also what is the problem in the transmitter code?

analogRead returns 10 bits in an int.
You try to cram that into a single byte.

How could I fix this?

Sorry about my lack of Arduino knowledge I am still a bit of an arduino noob.

Hi,
Have you written some code just to prove that the NRL can talk to each other.
Forget the controller code for the moment.

If you developed this project in stages, you must have some code that JUST tests that the NRL can communicate.
Also do you have code that JUST reads your joysticks and makes sure you are getting valid levels from them?

To trouble shoot you will need to isolate each part of your code and test it individually.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

thank you very much! I will try that now.

I have not tested the joystick yet but when I brought up the serial monitor to see what it said it showed the same messages from my old (abandoned) controller sketch.

Do you know how to fix that?

Try dividing the 10-bit analog reading by 4 to fit it in the 8-bit data array. Then, on the receiving side, use the range 0-255 instead of 0-1023.

  uint8_t data[2]; // defines size of array
  data[0] = valX/4;  // puts the value from the variable valX into the first element 
                   // note elements numbered from 0
                   // also assumes that valX is defined as uint8_t or byte
  data[1] = valY/4;

like this?

If valX and valY are 'int'. If they are 'byte' or 'uint8_t' you will need:

  valX = analogRead(joyX) / 4;
  valY = analogRead(joyY) / 4;

oh ok
thank you!

I will see if that works now

the car still will not work

Post your code