Nrf24L01 + Arduino Mega2560发送操纵杆数据

时间:2017-02-14 18:55:37

标签: arduino

我正在尝试使用nrf24l01在两个arduino mega 2560之间发送操纵杆的x和y值。到目前为止,我有一个发射器和接收器代码,但我在接收器上得到了错误的值。

发射器:

#include <SPI.h>
#include <RF24.h>

RF24 radio(7,8);
byte addresses[][6] = {"0x115331","0x222331"};

typedef struct{
  int X;
  int Y;
  int K;
}
Values;

Values v;

void setup() {
pinMode(10, INPUT);
Serial.begin(115200);
radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.setChannel(108);
radio.setPayloadSize(32);
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1,addresses[0]);
}

void loop() {
radio.stopListening();
v.X = analogRead(A0);
v.Y = analogRead(A1);
v.K = digitalRead(10);
Serial.print("X = ");
Serial.println(v.X);
Serial.print("Y = ");
Serial.println(v.Y);
Serial.print("Button status: ");
if (v.K == HIGH)
{
  Serial.println("pressed");
}
else
{
Serial.println("not pressed");
}
radio.write(&v,sizeof(v));
delay(500);
}

Reciever:

#include <SPI.h>
#include <RF24.h>

RF24 radio(7,8);
byte addresses[][6] = {"0x115331","0x222331"};

typedef struct{
  int X;
  int Y;
  int K;
}
Values;

Values v;

void setup() {
Serial.begin(115200);
radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
radio.setChannel(108);
radio.setPayloadSize(32);
radio.startListening();
}

void loop() {
radio.startListening();
if ( radio.available() )
{
  while(radio.available())
  {
    radio.read(&v, sizeof(v));
  }
}
  radio.stopListening();
  Serial.println(v.X);
  Serial.println(v.Y);
  Serial.println(v.K);
delay(1000);
}

我正在使用本网站的引脚配置:https://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo

0 个答案:

没有答案