简体   繁体   中英

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
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; 

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

joystick joy; 

//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);
        }
     
    }

}

Note that In transmitter, before you send the data you make value Mapping for different range

joy.x = map(xInput, 0, 1023, 1100, 1900);
joy.y = map(yInput, 0, 1023, 0, 180);

This piece of code will make change in the values

  1. Value of joy.x will be between the range [1100,1900]
  2. The Value of joy.y will be between the range [0,180]

In transmitter, you print the original value while in receiver you print the mapped value

if you need the original value in receiver send it directly without mapping like the following

joy.x = xInput;
joy.y = yInput;

read more about map() function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM