简体   繁体   English

为什么我在使用操纵杆和 nRF24L01 模块时会得到随机数?

[英]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.我正在尝试在 arduino nano 和 arduino uno 之间发送代码,nano 将来自结构中的操纵杆的值发送到 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] joy.x的值将在 [1100,1900] 范围内
  2. The Value of joy.y will be between the range [0,180] joy.y的值将在 [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阅读更多关于map() function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过 MISO 引脚使用 stm32f103(主)获取 nRF24l01(从)寄存器数据 - How do I take nRF24l01(slave) Register data using stm32f103 (master) through MISO pin 为什么我总是用 rand() 得到相同的随机数序列? - Why do I always get the same sequence of random numbers with rand()? 我可以从传感器读取值,然后将其添加到RF24l01网络的JSON字符串 - Can i read value from sensors then add it to JSON string for RF24l01 network 为什么使用OpenMP生成随机数时无法加快速度? - Why is there no speed up when using OpenMP to generate random numbers? 为什么我在 C 中应用文件处理时得到随机字符 - why do I get random characters when applying file handling in C 如何使用 get_random_bytes() 获取 kernel 中 1-7 之间的随机数? - How can I get random numbers between 1-7 in kernel using get_random_bytes()? 当我从C语言的文件中读取数字时,为什么会收到忽略“ fscanf”返回值的警告? - Why do I get a warning of ignoring return value of ‘fscanf’ when I read numbers from a file in C? 为什么在使用rand()时会得到这种特殊的颜色模式? - Why do I get this particular color pattern when using rand()? 为什么在使用strncpy时出现分段错误? - Why do I get a segmentation fault when using strncpy? 为什么我从这段代码中得到一串看似随机的数字? - Why do I get string of seemingly random number from this code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM