![](/img/trans.png)
[英]How do I take nRF24l01(slave) Register data using stm32f103 (master) through MISO pin
[英]Why do I get random numbers when using a joystick and an nRF24L01 module?
我正在尝试在 arduino nano 和 arduino uno 之间发送代码,nano 将来自结构中的操纵杆的值发送到 uno。 但是,当接收到这些值时,这些数字似乎是随机的,没有任何意义。 我的代码的潜在解决方案是什么?
发射机
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);
}
接收者
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);
}
}
}
请注意,在发送器中,在发送数据之前,您为不同的范围进行值映射
joy.x = map(xInput, 0, 1023, 1100, 1900);
joy.y = map(yInput, 0, 1023, 0, 180);
这段代码将改变值
joy.x
的值将在 [1100,1900] 范围内joy.y
的值将在 [0,180] 范围内在发射器中,您打印原始值,而在接收器中,您打印映射值
如果您需要接收器中的原始值,则直接发送它而不进行如下映射
joy.x = xInput;
joy.y = yInput;
阅读更多关于map() function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.