Rc car with steering and pedal control

I am trying to control an RC Car using nrf24l01 transceivers, arduino uno, potentiometers on the tx side and servos on the receiver side. Trying to make the throttle work first. Somehow nothing seems to be working. I am also trying to code the trottle servo in such a way that one servo can control both throttle and brake (left - throttle, right - brake) but am not able to write the code for that. Both brake and throttle have different potentiomenters attached which will transmit individual data. Can someone help.
Receiver side code:

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h> // Comes with Arduino IDE
#include <Servo.h> // Regular Servo library creates timer conflict!
/-----( Declare Constants and Pin Numbers )-----/
#define CE_PIN 7 // The pins to be used for CE and SN
#define CSN_PIN 8

int servoPin = 5;

/-----( Declare objects )-----/
/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus (usually) pins 7 & 8 (Can be changed) */
RF24 radio(7,8);
const uint64_t pipe = 0xE8E8F0F0E1LL;
Servo ThrottleServo; // create servo objects to control servos

void setup() /****** SETUP: RUNS ONCE ******/
{
Serial.begin(9600);

radio.begin(); // Initialize the nRF24L01 Radio
radio.setChannel(108); // 2.508 Ghz - Above most Wifi Channels
radio.setDataRate(RF24_250KBPS); // Fast enough… Better range
radio.setPALevel(RF24_PA_MAX);
// radio.setPALevel(RF24_PA_MAX);

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

// Start the radio listening for data
radio.startListening();
}//–(end setup )—

void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
while(Serial.available()== 0);

int data = Serial.read() - ‘0’;

int pos = map(data, 0, 9, 0, 150);
pos = constrain(pos, 0, 15xc0);

//Turn the servo
ThrottleServo.write(pos);
Serial.flush();
}//–(end main loop )—

Tx side code:

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h> // Comes with Arduino IDE
#include <Servo.h> // Regular Servo library creates timer conflict!
/-----( Declare Constants and Pin Numbers )-----/
#define CE_PIN 7 // The pins to be used for CE and SN
#define CSN_PIN 8

int potPin = 0;

RF24 radio(7, 8);
const uint64_t pipe = 0xE8E8F0F0E1LL;

void setup() /****** SETUP: RUNS ONCE ******/
{
Serial.begin(9600);

radio.begin(); // Initialize the nRF24L01 Radio
radio.setChannel(108); // Above most WiFi frequencies
radio.setDataRate(RF24_250KBPS); // Fast enough… Better range

radio.setPALevel(RF24_PA_MAX);
// radio.setPALevel(RF24_PA_MAX);

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

}//–(end setup )—

void loop()
{
int val = map(analogRead(potPin), 0, 1023, 0, 255);
char buffer[30];
sprintf(buffer,"%d",val);
radio.write(buffer, 30);
delay(50);
}

NRF connections:
1 - GND
2 - VCC 3.3V !!! NOT 5V
3 - CE to Arduino pin 7
4 - CSN to Arduino pin 8
5 - SCK to Arduino pin 13
6 - MOSI to Arduino pin 11
7 - MISO to Arduino pin 12
8 - UNUSED

Assuming nothing is working you need to start back at the beginning.

I got my nRF24s working with this Tutorial

I suggest you use the TMRh20 version of the RF24 library - it solves some problems from the ManiacBug version

The pair of programs in this link may contain most of what you need. They are derived from a system to control model trains.

...R

I'm using the same library that u mentioned and also worked with the example. Still not working. Can it be that I'm powering one side with laptop so the power is not enough for thr RF24?

What exactly is not working? Sending any data across the wireless? servo not working?

I'm doing the exact same project, except i'm making an RC sailboat. i can get my transmitter to send a signal to steer the boat and switches to draw/release the sail.

Have you ever connected your transceiver to +5 and not +3.3? I did that once and immediately fried the transceiver.

I don't know, it's hard to debug with what's provided.

Not that i can remember. I have only connected it to 3.3V. I'm connecting my arduino uno board to a 7.4V Lipo battery to which the servo is attached (Futaba S3003). Since the Servo operating voltage is 4.8-6V, I think the servo should move.
On the tx side I am powering the Arduino Uno with USB cable to which the pots are attached. There is no external power supply for the transceivers attached and these are drawing power off the arduino.

Is the code ok? I cannot even see anything on the Serial Monitor when I am pushing the pedals nor the servo is moving which makes me suspect that perhaps something is wrong with my code.

Please take the time to tell us the steps you have taken.

What worked?

What example do you mean when you say "and also worked with the example"?

What did you change that made it stop working?

Have you put a 10µF capacitor between Vcc and GND for the nRF24 - as close to the nRF24 as possible?

Make a pencil drawing showing how everything is connected and post a photo of the drawing. Verbal descriptions of wiring are too easy to misunderstand.

...R

I put a 10uF capacitor on the NRF and used the same code in the link https://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-ExampleSketches#js1

On the transmitter side I'm getting the following on the serial port. What is the issue? I'm following the exact same code and guidelines as given in the above link.

Send pot data by NRF
Now sending - Transmit Failed
Send pot data by NRF
Now sending - sent16580, Got response 12264, Round-trip delay 4316 microseconds
Now sending - sent122720, Got response 118408, Round-trip delay 4312 microseconds
Now sending - sent228968, Got response 224656, Round-trip delay 4312 microseconds
Now sending - sent335056, Got response 330888, Round-trip delay 4168 microseconds
Now sending - sent441260, Got response 436984, Round-trip delay 4276 microseconds
Now sending - sent547476, Got response 543200, Round-trip delay 4276 microseconds
Now sending - sent653684, Got response 649408, Round-trip delay 4276 microseconds
Now sending - sent761040, Got response 755616, Round-trip delay 5424 microseconds
Now sending - sent867244, Got response 862968, Round-trip delay 4276 microseconds
Now sending - sent973496, Got response 969184, Round-trip delay 4312 microseconds
Now sending - sent1079636, Got response 1075432, Round-trip delay 4204 microseconds
Now sending - sent1185876, Got response 1181664, Round-trip delay 4212 microseconds
Now sending - Transmit Failed
Send pot data by NRF
Now sending - sent16512, Got response 12264, Round-trip delay 4248 microseconds
Now sending - Transmit Failed
Send pot data by NRF
Now sending - Transmit Failed
Send pot data by NRF
Now sending - Transmit Failed

Whenever I reset the board there is initial transmission but then it again stops.
Send pot data by NRF
Now sending - sent16544, Got response 12264, Round-trip delay 4280 microseconds
Now sending - sent122688, Got response 118376, Round-trip delay 4312 microseconds
Now sending - sent228864, Got response 224624, Round-trip delay 4240 microseconds
Now sending - sent334952, Got response 330784, Round-trip delay 4168 microseconds
Now sending - sent441156, Got response 436880, Round-trip delay 4276 microseconds
Now sending - sent547300, Got response 543096, Round-trip delay 4204 microseconds
Now sending - Transmit Failed
Send pot data by NRF
Now sending - sent16440, Got response 12264, Round-trip delay 4176 microseconds
Now sending - sent122548, Got response 118272, Round-trip delay 4276 microseconds
Now sending - sent228764, Got response 224488, Round-trip delay 4276 microseconds
Now sending - Transmit Failed

Hi,
We will need to see you code, both ends to see what the problem is?
How have you got your pot wired?

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.

Thanks.. Tom.. :slight_smile:

My pot is wired as per the link http://www.wikihow.com/Wire-a-Potentiometer . I have not connected the middle pin of the pot as there is no input to the pot from arduino. Rest is same as in 1st pin to gnd and 3rd pin to A0 on arduino as per code.

Here is the transmitter side code

  #include <SPI.h>
   #include "RF24.h"
   #include "printf.h"

   #define CE_PIN 7
   #define CSN_PIN 8

   #define SteeringPot A0
   #define ThrottlePot A2
   
   RF24 radio (CE_PIN, CSN_PIN);

   byte addresses[][6] = {"1Node", "2Node"}; //Names of pipes

   unsigned long timeNow;  // Used to grab the current time, calculate delays
unsigned long started_waiting_at;
boolean timeout;       // Timeout? True or False

   boolean hasHardware = true; //boolean hasHardware = true;

   struct datastruct {
    unsigned long _micros; //to save response time
    int Steeringposition; //steering pot position values
    int Throttleposition;
   } myData;

   void setup()
   {
    Serial.begin(115200); 
    Serial.println(F("Send pot data by NRF"));
    printf_begin(); //needed for "printdetails. Takes up memory
    pinMode(SteeringPot, INPUT_PULLUP);

    radio.begin(); //Initialize nrf24l01 radio
    radio.setChannel(108); //Above most wifi freq
    radio.setDataRate(RF24_250KBPS); //Fast enough...better range

    radio.setPALevel(RF24_PA_MAX); //radio.setPALevel(RF24_PA_MAX);

    //Open a writing and reading pipe on each radio, with opposite addresses

    radio.openWritingPipe(addresses[0]);
    radio.openReadingPipe(1, addresses[1]);

    //start the radio listenning for data

   radio.startListening();

   }

   void loop()
   {
    radio.stopListening();
    if (hasHardware)
    {
      myData.Steeringposition = analogRead(SteeringPot);
      myData.Throttleposition = analogRead(ThrottlePot);
   }
else
{
  myData.Steeringposition = 256;
  myData.Throttleposition = 256;
}
myData._micros = micros(); //Send back for timing


Serial.print(F("Now sending - "));

if (!radio.write( &myData, sizeof(myData) ))
{
  Serial.println(F("Transmit Failed"));
}
radio.startListening();

started_waiting_at = micros(); //timeout period, get the current microseconds
timeout = false;  //variable to indicate if a response was received or not

while (!radio.available() )
{  //while nothing is received
  if (micros() - started_waiting_at > 200000 )
  
  { //if waited longer than 200ms, indicate timeout and exit the loop
    timeout = true;
  }}
  if (timeout)
  {//Describe results
    Serial.println(F("response timed out - no acknowledge."));
  } else
  {
    //Grab the response, compare, and send to Serial Monitor
    radio.read( &myData, sizeof(myData)  );
    timeNow = micros();

    //Show it
    Serial.print(F("sent"));
     Serial.print(timeNow);
    Serial.print(F(", Got response "));
    Serial.print(myData._micros);
    Serial.print(F(", Round-trip delay "));
    Serial.print(timeNow - myData._micros);
    Serial.println(F(" microseconds "));
    }

//send again after delay. When working Ok, change to something like 100
delay(100);
}

Here is the receiver side code

#include <SPI.h>   // Comes with Arduino IDE
#include "RF24.h"  // Download and Install (See above)
#include "printf.h" // Needed for "printDetails" Takes up some memory
#include <SoftwareServo.h>  // Regular Servo library creates timer conflict!
/*-----( Declare Constants and Pin Numbers )-----*/
#define  CE_PIN  7   // The pins to be used for CE and SN
#define  CSN_PIN 8

#define ServoSteeringPIN 3   //Pin Numbers for servos and laser/LED
#define ServoThrottlePIN 4

#define ServoMIN_H  30  // Don't go to very end of servo travel
#define ServoMAX_H  150 // which may not be all the way from 0 to 180. 
#define ServoMIN_V  30  // Don't go to very end of servo travel
#define ServoMAX_V  150 // which may not be all the way from 0 to 180

/*-----( Declare objects )-----*/
/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus (usually) pins 7 & 8 (Can be changed) */
RF24 radio(CE_PIN, CSN_PIN);

SoftwareServo SteeringServo;
SoftwareServo ThrottleServo;  // create servo objects to control servos

/*-----( Declare Variables )-----*/
byte addresses[][6] = {"1Node", "2Node"}; // These will be the names of the "Pipes"

// Allows testing of radios and code without servo hardware. Set 'true' when servos connected
boolean hasHardware = true;  // Allows testing of radios and code without Joystick hardware.
//boolean hasHardware = true;

int SteeringPotReceived; // Variable to store received Joystick values
int SteeringServoPosition;    // variable to store the servo position

int ThrottlePotReceived;   // Variable to store received Joystick values
int ThrottleServoPosition;      // variable to store the servo positio

struct dataStruct {
  unsigned long _micros;  // to save response times
  int Steeringposition; //steering pot position values
    int Throttleposition;
} myData;                 // This can be accessed in the form:  myData.Xposition  etc.

void setup()   /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(115200);   // MUST reset the Serial Monitor to 115200 (lower right of window )
  // NOTE: The "F" in the print statements means "unchangable data; save in Flash Memory to conserve SRAM"
  Serial.println(F("YourDuino.com Example: Receive joystick data by nRF24L01 radio from another Arduino"));
  Serial.println(F("and control servos if attached (Check 'hasHardware' variable"));
  printf_begin(); // Needed for "printDetails" Takes up some memory
  /*-----( Set up servos )-----*/
  if (hasHardware)
  {
    SteeringServo.attach(ServoSteeringPIN);  // attaches the servo to the servo object
    ThrottleServo.attach(ServoThrottlePIN);
  }

  radio.begin();          // Initialize the nRF24L01 Radio
  radio.setChannel(108);  // 2.508 Ghz - Above most Wifi Channels
  radio.setDataRate(RF24_250KBPS); // Fast enough.. Better range

  radio.setPALevel(RF24_PA_MAX);
  //   radio.setPALevel(RF24_PA_MAX);

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

  // Start the radio listening for data
  radio.startListening();
//  radio.printDetails(); //Uncomment to show LOTS of debugging information
}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{

  if ( radio.available())
  {

    while (radio.available())   // While there is data ready to be retrieved from the receive pipe
    {
      radio.read( &myData, sizeof(myData) );             // Get the data
    }

    radio.stopListening();                               // First, stop listening so we can transmit
    radio.write( &myData, sizeof(myData) );              // Send the received data back.
    radio.startListening();                              // Now, resume listening so we catch the next packets.

    Serial.print(F("Packet Received - Sent response "));  // Print the received packet data
    Serial.print(myData._micros);
    Serial.print(F("uS X= "));
    Serial.print(myData.Steeringposition);
    Serial.print(F(" Y= "));
    Serial.print(myData.Throttleposition);
   

  } // END radio available

  if (hasHardware)
  {
    /*-----( Calculate servo position values, send to the servos )-----*/
    SoftwareServo::refresh();//refreshes servo to keep them updating
    SteeringPotReceived  = myData.Steeringposition;  // Get the values received
    ThrottlePotReceived    = myData.Throttleposition;

    // scale it to use it with the servo (value between MIN and MAX)
    SteeringServoPosition  = map(SteeringPotReceived, 0, 1023, ServoMIN_H , ServoMAX_H);
    ThrottleServoPosition    = map(ThrottlePotReceived,   0, 1023, ServoMIN_V , ServoMAX_V);

    // tell servos to go to position
    SteeringServo.write(SteeringServoPosition);
    ThrottleServo.write(ThrottleServoPosition);
  } // END hasHardware
}//--(end main loop )---

Hi,

I have not connected the middle pin of the pot as there is no input to the pot from arduino. Rest is same as in 1st pin to gnd and 3rd pin to A0 on arduino as per code.

This will not give you a proper analog signal.
This will.

https://www.arduino.cc/en/tutorial/potentiometer

Have you got a DMM to measure voltages?

Tom.. :slight_smile:

TomGeorge:
Have you got a DMM to measure voltages?

Tom.. :slight_smile:

No I do not :frowning:

TomGeorge:
Hi,This will not give you a proper analog signal.
This will.

Arduino - Potentiometer

After re-wiring the pot still the serial output is same

Send pot data by NRF
Now sending - Transmit Failed
Send pot data by NRF
Now sending - sent19400, Got response 12264, Round-trip delay 7136 microseconds
Now sending - sent125552, Got response 121240, Round-trip delay 4312 microseconds
Now sending - sent231784, Got response 227472, Round-trip delay 4312 microseconds
Now sending - sent337956, Got response 333712, Round-trip delay 4244 microseconds
Now sending - sent444632, Got response 439888, Round-trip delay 4744 microseconds
Now sending - sent550764, Got response 546560, Round-trip delay 4204 microseconds
Now sending - sent656944, Got response 652704, Round-trip delay 4240 microseconds
Now sending - Transmit Failed

Have you tried the pair of programs in the link I gave you in Reply #1. They don't need any external hardware apart from the nRF24s.

...R

Robin2:
Have you tried the pair of programs in the link I gave you in Reply #1. They don't need any external hardware apart from the nRF24s.

...R

Yes I had. It was working fine yesterday but today I again tried with (hasHardware) = false; to identify whether it is the pot which is causing the problem or something else. The serial output was

Send pot data bmit Failed
Send pot data by NRF
Now sending - Transmit Failed
response timed out - no acknowledge.
Now sending - Send pot data by NRF
Now sending - Transmit Failed
Send pot data by NRF
Now sending - sent16148, Got response 11944, Round-trip delay 4204 microseconds
Now sending - sent121904, Got response 117772, Round-trip delay 4132 microseconds
Now sending - sent227744, Got response 223612, Round-trip delay 4132 microseconds
Now sending - Transmit Failed

Hi,
How far apart are the two NRF units?

Tom... :slight_smile:

TomGeorge:
Hi,
How far apart are the two NRF units?

Both are placed very close to each other (30 cms apart) and on the same work desk.

Hi,
Move them apart , say a metre, they may be overloading the Rx front-ends.

Tom... :slight_smile:

Hi,
Good to hear.

You need to add some serialprint in you TX code to show on the IDE monitor what the pot value is that you are sending.
At the moment it just says sending pot data, but not the value.

Tom... :slight_smile:

TomGeorge:
Hi,
Move them apart , say a metre, they may be overloading the Rx front-ends.

Tom… :slight_smile:

Thanks!! The communication now works fine but the pot is still not controlling the servo. The servo is a high torque 35kg-cm servo built to operate on 7.2/8.4V so I am using an external 7.4V LiPo to run it. The connections are made according to the image below and pot connections according the link Arduino - Potentiometer

Its really frustrating :frowning: