[英]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.