[英]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这段代码将改变值
joy.x
will be between the range [1100,1900] joy.x
的值将在 [1100,1900] 范围内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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.