Getting nRF24L01 Transmitter and Controller to Sync

I'm trying a simple demo code (see below) to remotely control a servo using a joystick. However, it only works when I seemingly turn on both the controller and the receiver at just the right time. Otherwise, the servo turns to the right all the way and holds there. When I do get the timing right, the servo centers and works perfectly with the controls.

Does the receiver stop listening if it doesn't get a signal at a certain time and if so, is there a code to fix this timing issue?

Thanks!

TRANSMITTER

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); 
const byte address[6] = "00001";

int x_key = A1;                                               
int y_key = A0;                                               
int x_pos;
int y_pos;

void setup() {
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();

  pinMode (x_key, INPUT) ;                     
  pinMode (y_key, INPUT) ;    
}

void loop() {
  x_pos = analogRead (x_key) ;  
  y_pos = analogRead (y_key) ;   
  radio.write(&x_pos, sizeof(x_pos));
  delay(100);
}

RECEIVER

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
Servo servo;
RF24 radio(A0, A1);
const byte address[6] = "00001";
int servo_pin = 3;

void setup() {
 Serial.begin(9600);
 radio.begin();
 servo.attach (servo_pin ) ; 
 radio.openReadingPipe(0, address);
 radio.setPALevel(RF24_PA_MIN);
 radio.startListening();
}

void loop() {
 if (radio.available()) {
   int x_pos ;
   radio.read(&x_pos, sizeof(x_pos));
   Serial.println(x_pos);
   x_pos = map(x_pos, 0, 1023, 0, 180);
   if (x_pos>400 && x_pos<600)
   {
     
   }
   else{
   servo.write (x_pos) ;
   }
 }
}

I don't immediately see anything wrong with your code. Try slowing down the Tx to once per second for testing as that is a more human speed.

The Rx will never stop listening unless you tell it to.

It is just as likely that the problem is with the transmitter - there is no way to tell if nothing is received.

A common problem with nRF24 modules is insufficient 3.3v current from the Arduino 3.3v pin. Try powering them with a pair of AA alkaline cells (3v) with the battery GND connected to the Arduino GND.

Have a look at this Simple nRF24L01+ Tutorial.

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with. Start by getting the first example to work

...R

Thanks so much for the help! I'm pretty new and barely a step above script kitty.

I'm powering the transmitter (Nano) with two 18650 batteries. (It's a remote control shield made for those).

Also, the receiver is a bot that I've built and works well with other code. Now I'm trying to make it remote controlled starting with servo. (After that works consistently, I'll start coding the motors.) Things are getting pretty crowded on the Uno in the bot so I've had to use two analog pins for CE and CSN (A0, A1). Could there be any issues with using those rather than 2 digital pins?

Ricochet76:
I'm powering the transmitter (Nano) with two 18650 batteries. (It's a remote control shield made for those).

You have not said how the Rx is powered. It also needs to transmit its acknowledgement message.

Things are getting pretty crowded on the Uno in the bot so I've had to use two analog pins for CE and CSN (A0, A1). Could there be any issues with using those rather than 2 digital pins?

That should not be a problem but you must ensure that pin 10 is set as OUTPUT to ensure the Arduino is the SPI master. (I'm assuming the Rx is also a Nano).

I cannot overstate the value of getting the wireless stuff working BEFORE you add any other code.

...R