Cannot get variables to parse using nRF24L01 and 2 nanos

Please see page #2. Hardware works with test sketches provided by Robin2.
Below is history using different code

Hi All,

Still very new to Arduino and coding. I have setup the following code for a receiver and transmitter
both use nRF24L01 modules, and an Arduino Nano board each.

I have a joystick attached to the Transmitter that reads the X, Y and switch states.
Viewing this in the monitor shows the values as expected, these change as I move the stick / press switch.

Position X: 500
Position Y: 500
Switch: 0

I can see the following on the Receiver monitor:

Position X: 14
Position Y: 14
Switch: 0

Only the switch responds and acts as expected, as you can see the X and Y get different numbers, however they do not change when the joystick is moved. I assume theres a different way to parse integers that maybe I don’t yet know how to code?

Any suggestions / help is greatly appreciated. Thank you

Note: I did take out the checks to confirm the radio was connecting as the variables wouldn’t parse fullstop. I had used a demo set of code (link below) that worked and the only thing I could see dfifferent was that it did not have the radio connecting checks.

I will need to revist this at a later stage, but for now I just want to see data from one going to the other.

Transmitter

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <FastLED.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";

#define JoyX A0 // Joystick X pin connected to A0 on the UNO
#define JoyY A2 // Joystick Y pin connected to A2 on the UNO



int Switchpin = 3; // Digital pin connected to switch output
int buttonState;

#define LED_PIN  5
#define NUM_LEDS 1
#define NUM_STRIPS 1

CRGB Leds[NUM_STRIPS][NUM_LEDS]; 




void setup() 
{
pinMode(JoyX, INPUT);
pinMode(JoyY, INPUT);
pinMode(Switchpin, INPUT_PULLUP);
digitalWrite(Switchpin, HIGH);

Serial.begin(9600);



  FastLED.addLeds<WS2812, LED_PIN, GRB>(Leds[0], NUM_LEDS);
  Leds[0][0] = CRGB(140,255,0); // Orange
  FastLED.show();
  delay(100);

  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MAX);
  radio.stopListening();

}

void loop() 
{
int buttonState = (digitalRead(Switchpin));

    radio.write(&JoyX, sizeof(JoyX));
    radio.write(&JoyY, sizeof(JoyY));
    radio.write(&buttonState, sizeof(buttonState));

Serial.print("JoyX : ");
Serial.println(analogRead(JoyX));
Serial.print("JoyY : ");
Serial.println(analogRead(JoyY));
Serial.print("buttonState : ");
Serial.println(buttonState);
Serial.println("=============");
delay(500);

Leds[0][0] = CRGB(255,0,0); // GREEN
FastLED.show();


delay(1000);

 }

Receiver

#include "HCPCA9685.h" // Include the HCPCA9685 library created by Andrew Davies
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <FastLED.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";

#define LED_PIN  5
#define NUM_LEDS 1
#define NUM_STRIPS 1

CRGB Leds[NUM_STRIPS][NUM_LEDS]; 

#define I2CAdd 0x40 // Default address of the PCA9685 Module (Servo array) // this comes later


int JoyX;
int JoyY;
int buttonState;



uint32_t start=0;
uint32_t timeout=0;
//const int Switch_Pin = 3; // digital pin connected to switch output
//int buttonState = 0;

// Used to store the mapping of the Joystick X and Y values
int LefteyeY;
int LefteyeX;
int RighteyeY;
int RighteyeX;

HCPCA9685 HCPCA9685(I2CAdd); // Define Library to use I2C communication

void setup() 
{

HCPCA9685.Init(SERVO_MODE); // Set to Servo Mode

HCPCA9685.Sleep(false); // Wake up PCA9685 module

Serial.begin(9600);

  FastLED.addLeds<WS2812, LED_PIN, GRB>(Leds[0], NUM_LEDS);
  Leds[0][0] = CRGB(140,255,0); // Orange
  FastLED.show();

 
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MAX);
  radio.startListening();
  delay(100);


//set all servos to 90

HCPCA9685.Servo(0, 90); // Move Servo 0
HCPCA9685.Servo(1, 90); // Move Servo 1
HCPCA9685.Servo(2, 90); // Move Servo 5
HCPCA9685.Servo(3, 90); // Move Servo 6

HCPCA9685.Servo(4, 90); // Move Servo 0 // Right Eye Up and down
HCPCA9685.Servo(5, 90); // Move Servo 1 // Right Eye left and right
HCPCA9685.Servo(6, 90); // Move Servo 5
HCPCA9685.Servo(7, 90); // Move Servo 6

delay(1000);

}


void loop() 
{


       Leds[0][0] = CRGB(255,0,0); // GREEN
       FastLED.show();
       
       radio.read(&JoyX, sizeof(JoyX));
       radio.read(&JoyY, sizeof(JoyY));
       radio.read(&buttonState, sizeof(buttonState));


//monitoring

    Serial.print("Joy X : ");
    Serial.println(JoyX);
    Serial.print("Joy Y : ");
    Serial.println(JoyY);
    Serial.print("Button : ");
    Serial.println(buttonState);
    Serial.println("========");
    delay(1000);


// end monitoring
 
//blink();
  
  int val1X = JoyX; // Read current value of Joystick 1 X axis
  int val1Y = JoyY; // Read current value of Joystick 1 Y axis
  


  // Map Joystick Axis values to servo Min and Max position 
  RighteyeY = map(val1X, 4, 1023, 0, 110 ); // Used to move Servo 4 (Right Eye up and down)
  RighteyeX = map(val1Y, 5, 1023, 80, 110); // Used to move Servo 3 (Left Eye up and down)
  LefteyeY = map(val1Y, 3, 1023, 0, 110); // Used to move Servo 3 (Left Eye up and down)
  LefteyeX = map(val1Y, 2, 1023, 800, 110); // Used to move Servo 3 (Left Eye up and down)



  // Move Servos to read postion from Joystick
  HCPCA9685.Servo(2, LefteyeX); // Move Servo 2
  HCPCA9685.Servo(3, LefteyeY); // Move Servo 3
  HCPCA9685.Servo(4, RighteyeY); // Move Servo 6
  HCPCA9685.Servo(5, RighteyeX); // Move Servo 7
  delay(200);
  }



//blink code

void blink() {
  if(millis() - start >= timeout) {
    start = millis();
 timeout = random(5000,15000); // between 5 - 15 seconds;
  
  HCPCA9685.Servo(0, 0); // Move Servo 0 
  HCPCA9685.Servo(1,180); // Move Servo 1 
  HCPCA9685.Servo(6, 0); // Move Servo 6 
  HCPCA9685.Servo(7, 0); // Move Servo 7 

  delay(200);


  }


}

DisasterofPuppets:

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

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

You're sending the pin numbers here. A0 happens to be defined as 14. I don't know why you would be getting 14 also for Y though, because A2 is defined as 16.

DisasterofPuppets:

Serial.print("JoyX : ");

Serial.println(analogRead(JoyX));
Serial.print("JoyY : ");
Serial.println(analogRead(JoyY));

Here, you are correctly printing the analog readings of the pins. So you see, you are sending different data over the nRF24L01 than you are over Serial. That does not make for easy troubleshooting. Best practices would be to define variables with the values you want to send and then send the same variables over the nRF24L01 and serial.

DisasterofPuppets:
Position X: 500
Position Y: 500
Switch: 0

I can see the following on the Receiver monitor:

Position X: 14
Position Y: 14
Switch: 0

This is mysterious to me. The text "Position X:", "Position Y:" and "Switch:" doesn't occur anywhere in either of your sketches. I would expect it to be something like:

Joy X : 500
Joy Y : 500
buttonState : 0

and:

Joy X : 14
Joy Y : 16
Button : 0

There should be no need to parse values when sending data with an nRF24. Just put the three values into an array and send the array. At the receiving side put the data into an identical array. Job done.

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

pert:
You're sending the pin numbers here. A0 happens to be defined as 14. I don't know why you would be getting 14 also for Y though, because A2 is defined as 16.
Here, you are correctly printing the analog readings of the pins. So you see, you are sending different data over the nRF24L01 than you are over Serial. That does not make for easy troubleshooting. Best practices would be to define variables with the values you want to send and then send the same variables over the nRF24L01 and serial.
This is mysterious to me. The text "Position X:", "Position Y:" and "Switch:" doesn't occur anywhere in either of your sketches. I would expect it to be something like:

Joy X : 500
Joy Y : 500
buttonState : 0

and:

Joy X : 14
Joy Y : 16
Button : 0

Champion, That looks like the problem :slight_smile:
Now sending the analogRead values of X and Y instead of the pin number :stuck_out_tongue:

I've updated my original post with the new code.

All seems well but I get 0 values every 4th read or so...eg

Joy X : 519
Joy Y : 510
Button : 1

Joy X : 519
Joy Y : 510
Button : 1

Joy X : 519
Joy Y : 510
Button : 1

Joy X : 0
Joy Y : 0
Button : 0

Joy X : 519
Joy Y : 510
Button : 1

DisasterofPuppets:
I've updated my original post with the new code.

Please put the Original Post back to the way it was and post your revised code in your next Reply.

That way other people can follow the discussion and learn from your experience.

In general do not modify earlier posts other than to correct typos. When you make a change to an earlier Post the subsequent comments can't be understood.

...R

DisasterofPuppets:
All seems well but I get 0 values every 4th read or so...eg

What do you see in Serial Monitor with the transmitter's serial port open?

I don't have any experience with the RF24 library, but I think you're supposed to check radio.available() before calling radio.read().

I think you probably need a

if(radio.available())

to prevent reading zeros - there just isn't a valid package to read. Mind you, I haven't tested it, but I've done similar before and my code has that 'if' before allowing it to read my array (through no skill of my own - thank you other Arduino users).

Rl4ndom:
I think you probably need a

if(radio.available())

to prevent reading zeros - there just isn't a valid package to read. Mind you, I haven't tested it, but I've done similar before and my code has that 'if' before allowing it to read my array (through no skill of my own - thank you other Arduino users).

Makes sense, I'll throw the connection checking back in.

Robin2:
Please put the Original Post back to the way it was and post your revised code in your next Reply.

That way other people can follow the discussion and learn from your experience.

In general do not modify earlier posts other than to correct typos. When you make a change to an earlier Post the subsequent comments can’t be understood.

…R

Apologies, was thinking of conserving space. Waiting on forum time limits and fixing :slight_smile:

New code below

EDIT UPDATE. NOW working updated code below. Only issue I get is some 0 values on the receiver every 4th or 5th read. Added Radio available checks which removed this, hoever I get about 6 Radio not available spams then data again. The boards are physically next to each other. Am I checking too often?

Transmitter

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <FastLED.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";

#define JoyX A0 // Joystick X pin connected to A0 on the UNO
#define JoyY A2 // Joystick Y pin connected to A2 on the UNO



int Switchpin = 3; // Digital pin connected to switch output
int buttonState;

#define LED_PIN  5
#define NUM_LEDS 1
#define NUM_STRIPS 1

CRGB Leds[NUM_STRIPS][NUM_LEDS]; 




void setup() 
{
pinMode(JoyX, INPUT);
pinMode(JoyY, INPUT);
pinMode(Switchpin, INPUT_PULLUP);
digitalWrite(Switchpin, HIGH);

Serial.begin(9600);



  FastLED.addLeds<WS2812, LED_PIN, GRB>(Leds[0], NUM_LEDS);
  Leds[0][0] = CRGB(140,255,0); // Orange
  FastLED.show();
  delay(100);

  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MAX);
  radio.stopListening();

}

void loop() 
{
int buttonState = (digitalRead(Switchpin));
int X = (analogRead(JoyX));
int Y = (analogRead(JoyY));

    radio.write(&X, sizeof(X));
    radio.write(&Y, sizeof(Y));
    radio.write(&buttonState, sizeof(buttonState));

Serial.print("JoyX : ");
Serial.println(X);
Serial.print("JoyY : ");
Serial.println(Y);
Serial.print("buttonState : ");
Serial.println(buttonState);
Serial.println("=============");
delay(500);

Leds[0][0] = CRGB(255,0,0); // GREEN
FastLED.show();


delay(1000);

 }

Reciever

#include "HCPCA9685.h" // Include the HCPCA9685 library created by Andrew Davies
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <FastLED.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";

#define LED_PIN  5
#define NUM_LEDS 1
#define NUM_STRIPS 1

CRGB Leds[NUM_STRIPS][NUM_LEDS]; 

#define I2CAdd 0x40 // Default address of the PCA9685 Module (Servo array) // this comes later


int X;
int Y;
int buttonState;



uint32_t start=0;
uint32_t timeout=0;
//const int Switch_Pin = 3; // digital pin connected to switch output
//int buttonState = 0;

// Used to store the mapping of the Joystick X and Y values
int LefteyeY;
int LefteyeX;
int RighteyeY;
int RighteyeX;

HCPCA9685 HCPCA9685(I2CAdd); // Define Library to use I2C communication

void setup() 
{

HCPCA9685.Init(SERVO_MODE); // Set to Servo Mode

HCPCA9685.Sleep(false); // Wake up PCA9685 module

Serial.begin(9600);

  FastLED.addLeds<WS2812, LED_PIN, GRB>(Leds[0], NUM_LEDS);
  Leds[0][0] = CRGB(140,255,0); // Orange
  FastLED.show();

 
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MAX);
  radio.startListening();
  delay(100);


//set all servos to 90

HCPCA9685.Servo(0, 90); // Move Servo 0
HCPCA9685.Servo(1, 90); // Move Servo 1
HCPCA9685.Servo(2, 90); // Move Servo 5
HCPCA9685.Servo(3, 90); // Move Servo 6

HCPCA9685.Servo(4, 90); // Move Servo 0 // Right Eye Up and down
HCPCA9685.Servo(5, 90); // Move Servo 1 // Right Eye left and right
HCPCA9685.Servo(6, 90); // Move Servo 5
HCPCA9685.Servo(7, 90); // Move Servo 6

delay(1000);

}


void loop() 
{

if(radio.available())
{
       Leds[0][0] = CRGB(255,0,0); // GREEN
       FastLED.show();
       
       radio.read(&X, sizeof(X));
       radio.read(&Y, sizeof(Y));
       radio.read(&buttonState, sizeof(buttonState));


//monitoring

    Serial.print("Joy X : ");
    Serial.println(X);
    Serial.print("Joy Y : ");
    Serial.println(Y);
    Serial.print("Button : ");
    Serial.println(buttonState);
    Serial.println("========");
    delay(1000);


// end monitoring
 
//blink();
  
  int val1X = X; // Read current value of Joystick 1 X axis
  int val1Y = Y; // Read current value of Joystick 1 Y axis
  


  // Map Joystick Axis values to servo Min and Max position 
  RighteyeY = map(val1X, 4, 1023, 0, 110 ); // Used to move Servo 4 (Right Eye up and down)
  RighteyeX = map(val1Y, 5, 1023, 80, 110); // Used to move Servo 3 (Left Eye up and down)
  LefteyeY = map(val1Y, 3, 1023, 0, 110); // Used to move Servo 3 (Left Eye up and down)
  LefteyeX = map(val1Y, 2, 1023, 800, 110); // Used to move Servo 3 (Left Eye up and down)



  // Move Servos to read postion from Joystick
  HCPCA9685.Servo(2, LefteyeX); // Move Servo 2
  HCPCA9685.Servo(3, LefteyeY); // Move Servo 3
  HCPCA9685.Servo(4, RighteyeY); // Move Servo 6
  HCPCA9685.Servo(5, RighteyeX); // Move Servo 7
  delay(200);
 
   }
 
else
  {
    Leds[0][0] = CRGB(0,255,0); // RED
    FastLED.show();
    Serial.println("Radio not Available");
  }

}
//blink code

void blink() {
  if(millis() - start >= timeout) {
    start = millis();
 timeout = random(5000,15000); // between 5 - 15 seconds;
  
  HCPCA9685.Servo(0, 0); // Move Servo 0 
  HCPCA9685.Servo(1,180); // Move Servo 1 
  HCPCA9685.Servo(6, 0); // Move Servo 6 
  HCPCA9685.Servo(7, 0); // Move Servo 7 

  delay(200);


                                  }
            }

You should NOT be using three radio.write() commands like this

    radio.write(&X, sizeof(X));
    radio.write(&Y, sizeof(Y));
    radio.write(&buttonState, sizeof(buttonState));

You only need one and it will make everything much simpler - this is one way

int dataToSend[3];
dataToSend[0] = X;
dataToSend[1] = Y;
dataToSend[2] = buttonState;
radio.write(&dataToSend, sizeof(dataToSend);

You can send up to 32 bytes in a single message.

In the receiver have an identical array - like this

int dataToReceive[3];
if (radio.available() ) {
   radio.read(&dataToReceive, sizeof(dataToReceive);
}

...R

Robin2:
You only need one and it will make everything much simpler - this is one way

int dataToSend[3];

dataToSend[0] = X;
dataToSend[1] = Y;
dataToSend[2] = buttonState;
radio.write(&dataToSend, sizeof(dataToSend);




...R

Makes sense now :slight_smile: original code page for that made my head spin :slight_smile: Thank you, you made it simple enough for me to understand.
I also added a delay to the radio failed message to stop the spam, all working still but still getting the Radio not Available every 4th or 5th data read.

Transmitter

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <FastLED.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";

#define JoyX A0 // Joystick X pin connected to A0 on the UNO
#define JoyY A2 // Joystick Y pin connected to A2 on the UNO



int Switchpin = 3; // Digital pin connected to switch output
int buttonState;

#define LED_PIN  5
#define NUM_LEDS 1
#define NUM_STRIPS 1

CRGB Leds[NUM_STRIPS][NUM_LEDS]; 




void setup() 
{
pinMode(JoyX, INPUT);
pinMode(JoyY, INPUT);
pinMode(Switchpin, INPUT_PULLUP);
digitalWrite(Switchpin, HIGH);

Serial.begin(9600);



  FastLED.addLeds<WS2812, LED_PIN, GRB>(Leds[0], NUM_LEDS);
  Leds[0][0] = CRGB(140,255,0); // Orange
  FastLED.show();
  delay(100);

  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MAX);
  radio.stopListening();

}

void loop() 
{
int buttonState = (digitalRead(Switchpin));
int X = (analogRead(JoyX));
int Y = (analogRead(JoyY));

int dataToSend[3];
dataToSend[0] = X;
dataToSend[1] = Y;
dataToSend[2] = buttonState;
radio.write(&dataToSend, sizeof(dataToSend));

Serial.print("JoyX : ");
Serial.println(X);
Serial.print("JoyY : ");
Serial.println(Y);
Serial.print("buttonState : ");
Serial.println(buttonState);
Serial.println("=============");
delay(500);

Leds[0][0] = CRGB(255,0,0); // GREEN
FastLED.show();


delay(1000);

 }

Receiver

#include "HCPCA9685.h" // Include the HCPCA9685 library created by Andrew Davies
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <FastLED.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";

#define LED_PIN  5
#define NUM_LEDS 1
#define NUM_STRIPS 1

CRGB Leds[NUM_STRIPS][NUM_LEDS]; 

#define I2CAdd 0x40 // Default address of the PCA9685 Module (Servo array) // this comes later


int X;
int Y;
int buttonState;



uint32_t start=0;
uint32_t timeout=0;
//const int Switch_Pin = 3; // digital pin connected to switch output
//int buttonState = 0;

// Used to store the mapping of the Joystick X and Y values
int LefteyeY;
int LefteyeX;
int RighteyeY;
int RighteyeX;

HCPCA9685 HCPCA9685(I2CAdd); // Define Library to use I2C communication

void setup() 
{

HCPCA9685.Init(SERVO_MODE); // Set to Servo Mode

HCPCA9685.Sleep(false); // Wake up PCA9685 module

Serial.begin(9600);

  FastLED.addLeds<WS2812, LED_PIN, GRB>(Leds[0], NUM_LEDS);
  Leds[0][0] = CRGB(140,255,0); // Orange
  FastLED.show();

 
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MAX);
  radio.startListening();
  delay(100);


//set all servos to 90

HCPCA9685.Servo(0, 90); // Move Servo 0
HCPCA9685.Servo(1, 90); // Move Servo 1
HCPCA9685.Servo(2, 90); // Move Servo 5
HCPCA9685.Servo(3, 90); // Move Servo 6

HCPCA9685.Servo(4, 90); // Move Servo 0 // Right Eye Up and down
HCPCA9685.Servo(5, 90); // Move Servo 1 // Right Eye left and right
HCPCA9685.Servo(6, 90); // Move Servo 5
HCPCA9685.Servo(7, 90); // Move Servo 6

delay(1000);

}


void loop() 
{

int dataToReceive[3];

if(radio.available())
{
       Leds[0][0] = CRGB(255,0,0); // GREEN
       FastLED.show();
       
   radio.read(&dataToReceive, sizeof(dataToReceive));


//monitoring

    Serial.print("Joy X : ");
    Serial.println(dataToReceive[0]);
    Serial.print("Joy Y : ");
    Serial.println(dataToReceive[1]);
    Serial.print("Button : ");
    Serial.println(dataToReceive[2]);
    Serial.println("========");
    delay(1000);


// end monitoring
 
//blink();
  
  int val1X = X; // Read current value of Joystick 1 X axis
  int val1Y = Y; // Read current value of Joystick 1 Y axis
  


  // Map Joystick Axis values to servo Min and Max position 
  RighteyeY = map(val1X, 4, 1023, 0, 110 ); // Used to move Servo 4 (Right Eye up and down)
  RighteyeX = map(val1Y, 5, 1023, 80, 110); // Used to move Servo 3 (Left Eye up and down)
  LefteyeY = map(val1Y, 3, 1023, 0, 110); // Used to move Servo 3 (Left Eye up and down)
  LefteyeX = map(val1Y, 2, 1023, 800, 110); // Used to move Servo 3 (Left Eye up and down)



  // Move Servos to read postion from Joystick
  HCPCA9685.Servo(2, LefteyeX); // Move Servo 2
  HCPCA9685.Servo(3, LefteyeY); // Move Servo 3
  HCPCA9685.Servo(4, RighteyeY); // Move Servo 6
  HCPCA9685.Servo(5, RighteyeX); // Move Servo 7
  delay(200);
 
   }
 
else
  {
    Leds[0][0] = CRGB(0,255,0); // RED
    FastLED.show();
    Serial.println("Radio not Available");
    delay(1000);
  }

}
//blink code

void blink() {
  if(millis() - start >= timeout) {
    start = millis();
 timeout = random(5000,15000); // between 5 - 15 seconds;
  
  HCPCA9685.Servo(0, 0); // Move Servo 0 
  HCPCA9685.Servo(1,180); // Move Servo 1 
  HCPCA9685.Servo(6, 0); // Move Servo 6 
  HCPCA9685.Servo(7, 0); // Move Servo 7 

  delay(200);


                                  }
            }
delay(1000)

That delay() is not helping.

Thank you

Which one, I have 3 in the Receiver code, could be the issue I guess?
Are you saying I need to remove it or simply need to increase the delay?

The main culprit seems to be the delay in the else statement. Without it all I get is repeated 'Radio is not available' with no data. While I do have it I see the occasional date between 'Radio not available.'

eg

Radio not Available
Radio not Available
Joy X : 519
Joy Y : 510
Button : 1

Joy X : 519
Joy Y : 510
Button : 1

Joy X : 519
Joy Y : 510
Button : 1

Radio not Available
Radio not Available
Joy X : 519
Joy Y : 510
Button : 1

Radio not Available
Radio not Available
Joy X : 519
Joy Y : 510
Button : 1

EDIT

Ah the delay in the transmitter, makes sense :slight_smile:

Getting better, now IT's only occassionaly not available.

Radio not Available
Joy X : 519
Joy Y : 510
Button : 1

Joy X : 519
Joy Y : 510
Button : 1

Joy X : 519
Joy Y : 510
Button : 1

Joy X : 519
Joy Y : 510
Button : 1

Joy X : 519
Joy Y : 510
Button : 1

Radio not Available
Joy X : 519
Joy Y : 510
Button : 1

Removed all the delays on the Transmitter and lo and behold we have a winner.

Thank you ALL for your help, I now have working code!!!!

Final working code for those (mainly future me) interested. Note servo code not complete yet but radio working without interruption.

Big thanks again to all who helped!!!

Transmitter

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <FastLED.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";

#define JoyX A0 // Joystick X pin connected to A0 on the UNO
#define JoyY A2 // Joystick Y pin connected to A2 on the UNO



int Switchpin = 3; // Digital pin connected to switch output
int buttonState;

#define LED_PIN  5
#define NUM_LEDS 1
#define NUM_STRIPS 1

CRGB Leds[NUM_STRIPS][NUM_LEDS]; 




void setup() 
{
pinMode(JoyX, INPUT);
pinMode(JoyY, INPUT);
pinMode(Switchpin, INPUT_PULLUP);
digitalWrite(Switchpin, HIGH);

Serial.begin(9600);



  FastLED.addLeds<WS2812, LED_PIN, GRB>(Leds[0], NUM_LEDS);
  Leds[0][0] = CRGB(140,255,0); // Orange
  FastLED.show();

  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MAX);
  radio.stopListening();

}

void loop() 
{
int buttonState = (digitalRead(Switchpin));
int X = (analogRead(JoyX));
int Y = (analogRead(JoyY));

int dataToSend[3];
dataToSend[0] = X;
dataToSend[1] = Y;
dataToSend[2] = buttonState;
radio.write(&dataToSend, sizeof(dataToSend));

Serial.print("JoyX : ");
Serial.println(X);
Serial.print("JoyY : ");
Serial.println(Y);
Serial.print("buttonState : ");
Serial.println(buttonState);
Serial.println("=============");


Leds[0][0] = CRGB(255,0,0); // GREEN
FastLED.show();


 }

Receiver

#include "HCPCA9685.h" // Include the HCPCA9685 library created by Andrew Davies
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <FastLED.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";

#define LED_PIN  5
#define NUM_LEDS 1
#define NUM_STRIPS 1

CRGB Leds[NUM_STRIPS][NUM_LEDS]; 

#define I2CAdd 0x40 // Default address of the PCA9685 Module (Servo array) // this comes later


int X;
int Y;
int buttonState;



uint32_t start=0;
uint32_t timeout=0;
//const int Switch_Pin = 3; // digital pin connected to switch output
//int buttonState = 0;

// Used to store the mapping of the Joystick X and Y values
int LefteyeY;
int LefteyeX;
int RighteyeY;
int RighteyeX;

HCPCA9685 HCPCA9685(I2CAdd); // Define Library to use I2C communication

void setup() 
{

HCPCA9685.Init(SERVO_MODE); // Set to Servo Mode

HCPCA9685.Sleep(false); // Wake up PCA9685 module

Serial.begin(9600);

  FastLED.addLeds<WS2812, LED_PIN, GRB>(Leds[0], NUM_LEDS);
  Leds[0][0] = CRGB(140,255,0); // Orange
  FastLED.show();

 
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MAX);
  radio.startListening();


//set all servos to 90

HCPCA9685.Servo(0, 90); // Move Servo 0
HCPCA9685.Servo(1, 90); // Move Servo 1
HCPCA9685.Servo(2, 90); // Move Servo 5
HCPCA9685.Servo(3, 90); // Move Servo 6

HCPCA9685.Servo(4, 90); // Move Servo 0 // Right Eye Up and down
HCPCA9685.Servo(5, 90); // Move Servo 1 // Right Eye left and right
HCPCA9685.Servo(6, 90); // Move Servo 5
HCPCA9685.Servo(7, 90); // Move Servo 6

delay(1000);
}


void loop() 
{

int dataToReceive[3];

if(radio.available())
{
       Leds[0][0] = CRGB(255,0,0); // GREEN
       FastLED.show();
       
   radio.read(&dataToReceive, sizeof(dataToReceive));


//monitoring

    Serial.print("Joy X : ");
    Serial.println(dataToReceive[0]);
    Serial.print("Joy Y : ");
    Serial.println(dataToReceive[1]);
    Serial.print("Button : ");
    Serial.println(dataToReceive[2]);
    Serial.println("========");



// end monitoring
 
//blink();
  
  int val1X = X; // Read current value of Joystick 1 X axis
  int val1Y = Y; // Read current value of Joystick 1 Y axis
  


  // Map Joystick Axis values to servo Min and Max position 
  RighteyeY = map(val1X, 4, 1023, 0, 110 ); // Used to move Servo 4 (Right Eye up and down)
  RighteyeX = map(val1Y, 5, 1023, 80, 110); // Used to move Servo 3 (Left Eye up and down)
  LefteyeY = map(val1Y, 3, 1023, 0, 110); // Used to move Servo 3 (Left Eye up and down)
  LefteyeX = map(val1Y, 2, 1023, 800, 110); // Used to move Servo 3 (Left Eye up and down)



  // Move Servos to read postion from Joystick
  HCPCA9685.Servo(2, LefteyeX); // Move Servo 2
  HCPCA9685.Servo(3, LefteyeY); // Move Servo 3
  HCPCA9685.Servo(4, RighteyeY); // Move Servo 6
  HCPCA9685.Servo(5, RighteyeX); // Move Servo 7
  delay(200);
 
   }
 
else
  {
    Leds[0][0] = CRGB(0,255,0); // RED
    FastLED.show();
    Serial.println("Radio not Available");
    delay(1000);
  }

}
//blink code

void blink() {
  if(millis() - start >= timeout) {
    start = millis();
 timeout = random(5000,15000); // between 5 - 15 seconds;
  
  HCPCA9685.Servo(0, 0); // Move Servo 0 
  HCPCA9685.Servo(1,180); // Move Servo 1 
  HCPCA9685.Servo(6, 0); // Move Servo 6 
  HCPCA9685.Servo(7, 0); // Move Servo 7 

  delay(200);


                                  }
            }

DisasterofPuppets:
I also added a delay to the radio failed message to stop the spam,

It makes no sense to monitor the "radio not available" because that is the normal situation 99.99% of the time. The RX has no means to know that the TX has sent a message that did not arrive.

What you can do is measure the time between received messages and then (for example) if you know that the TX sends a message every second and if the time since the last message is 2 seconds you can be pretty sure there was a problem.

...R

Hi all,

I have followed the basic tutorial here

to check if my NRF24L01 wireless transmitter and receiver are working. (I have one on each nano board)

I am getting results like this on the Receiver:

PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

I expect to see “Hello World”

Any ideas?

I have changed the receiver and trasmitter hardware, same issue.
Tried different channels etc, same issue.

Transmitter

 /*
* Arduino Wireless Communication Tutorial
*     Example 1 - Transmitter Code
*                
* by Dejan Nedelkovski, www.HowToMechatronics.com
* 
* Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(7, 8); // CE, CSN

const byte address[6] = "00001";

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

void loop() {
  const char text[] = "Hello World";
  radio.write(&text, sizeof(text));
  delay(1000);
}

Receiver

 /*
* Arduino Wireless Communication Tutorial
*       Example 1 - Receiver Code
*                
* by Dejan Nedelkovski, www.HowToMechatronics.com
* 
* Library: TMRh20/RF24, https://github.com/tmrh20/RF24/
*/

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(7, 8); // CE, CSN

const byte address[6] = "00001";

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

void loop() {
  if (radio.available()) {
    char text[32] = "";
    radio.read(&text, sizeof(text));
    Serial.println(text);
  }
}

I’m lost :frowning:
Thank you.

I have followed the basic turorial here

How are we supposed to know which tutorial that you are referring to?
Post the code that you uploaded to the Nanos.

This is the best tutorial that I have found for the rf24 radios.

Ah yes. Good old haste and forgetting the basics. Apologies. Updating original post.

deleted. Double post and trying to edit on mobile

May I suggest that you read Robin2's tutorial (the one that I linked). There is some good advice on powering those radios. The most usual cause for problems is that the 3.3V supplies on most Arduino boards do not have the current capacity required. The addition of a 10 to 100uF cap on the radio power pins can help, but i usually have a LM1117 3.3 powered by 5V to power the radios. There are modules that you can buy that have a 3.3V regulator on them to make interfacing and powering the radios easier.