Why do I get random numbers when using a joystick and an nRF24L01 module?

I’m trying to send code between an arduino nano and an arduino uno, the nano sending the values from a joystick held within a struct over to the uno. However, when the values are received, the numbers are seemingly random and make no sense. What is a potential solution to my code?

Transmitter

typedef struct
{
    int x;
    int y;   
} joystick; 

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

//Defining pins for later use
int joy[2]; 
const int xAxis = A0; 
const int yAxis = A1;

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

joystick joy; 

//Address to use later
const uint64_t pipe = 0xE8E8F0F0E1LL;

void setup() 
{
    radio.begin();
    radio.openWritingPipe(pipe);
    radio.setPALevel(RF24_PA_MIN);
    //radio.setDataRate(RF24_250KBPS);
    radio.stopListening(); 
  
    Serial.begin(9600);
    pinMode(xAxis, INPUT);
    pinMode(yAxis, INPUT); 
    delay(1000);
}

void loop() 
{
    int xInput = analogRead(xAxis);
    int yInput = analogRead(yAxis); 

    Serial.print("x-axis: ");
    Serial.print(xInput);
    Serial.print(" y-axis: ");
    Serial.println(yInput);
    
    joy.x = map(xInput, 0, 1023, 1100, 1900);
    joy.y = map(yInput, 0, 1023, 0, 180);
    
    radio.write(&joy, sizeof(joy));
    delay(50);
    
}

Receiver:

typedef struct
{
    int x;
    int y;   
} joystick; 

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

//Defining pins for later use
const int EDFPin = 11;
const int servoPin = 6;

//Object declaration 
Servo edf;
Servo myServo; 

int x; 

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

//joystick joy; 
int joy[2]; 

//Address to use later
const uint64_t pipe = 0xE8E8F0F0E1LL;

void setup() 
{
    radio.begin();
    radio.openReadingPipe(1, pipe);
    radio.setPALevel(RF24_PA_MIN);
  //  radio.setDataRate(RF24_250KBPS);
    radio.startListening(); 
  
    Serial.begin(9600);
    edf.attach(EDFPin);
    edf.writeMicroseconds(1500);
    myServo.attach(servoPin); 
    myServo.write(90);
    delay(1000);
}

void loop() 
{
    if (radio.available())
    {   
        while(radio.available())
        {
            radio.read(&joy, sizeof(joy)); 
            Serial.println(joy.x);
            Serial.println(joy.y);
            delay(50);
        }
        //Serial.println("Shit");
    }
/*
    
    delay(50);
    //edf.writeMicroseconds(joy.x);
    //myServo.write(joy.y); */

}
int joy[2];

An array of 2 ints named joy with global scope

joystick joy;

An instance of the joystick struct named joy with global scope
As a result, the code that you posted does not compile

conflicting declaration 'joystick joy'

Did you post the correct version of the code ?

I've had a quick look and no expert at this but have worked on a few projects in the past that have worked very well and had no issue's.

I've noticed you've got a delay in the RX never use delays because your program halts for that time. I would just try sending one joystick value first of the 0-1023 value, Once you know this working then add the second joystick and use the map function in the RX code and not the TX code and see if that works.

I did have some nano's set up with the modules attached that I know works and always used these to get my projects working, Might have to blow the dust of them.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.