求助arduino使用nRF24l01模块

陆佩佩西蒙 2020-04-041173 次点击
自从看了趣无尽中一个风力小船的作品后,就想自己做一个遥控小车什么的,但是中途遇到许多问题,基本上都是关于nRF24l01这个模块的,先是没有nRF24l01.h这个库文件,网上找了好久,才在github网站上找到了但是一大堆文件我也不是很懂,毕竟小白接触arduino这家伙不久,就只copy了其中的一个.h文件,变成C文件压缩后用IDE添加了这个库文件,最后程序编译是过了。上传后就遇到了新的问题,通信模块毫无作用!!!哇哦,好头疼!代码如下,请问那位大佬帮助一下,看是代码有问题呢,还是我库文件少了些什么啊?
十分感谢!!!

#include <SPI.h>
#include <nRF240L01.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>

int X , Y ;
String UP , DOWN , LEFT , RIGHT ;
String LeftWhileUP , LeftWhileDown , RightWhileUp , RightWhileDown
int temp , spead ;
RF24 radio(8, 9); //设置CE, CSN引脚
const byte address[6] = "00001" ; //定义通信地址

void setup() {‌
// put your setup code here, to run once:
pinMode (A1, INPUT) ;
pinMode (A2, INPUT) ;
pinMode (A3, INPUT) ;

UP = "up" ;
DOWN = "down" ;
LEFT = "left" ;
RIGHT = "right" ;
LeftWhileUP = "LeftWhileUp" ;
LeftWhileDown = "LeftWhileDown" ;
RightWhileUp = "RightWhileUp" ;
RightWhileDown = "RightWhileDown" ;
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address); //设置通信地址
radio.setPALevel(RF24_PA_MIN);
radio.stopListening(); //设置nRF24为发射模式
}

void loop() {‌
// put your main code here, to run repeatedly:
X = analogRead(A1); //读取X方向的值
Y = analogRead(A2); //读取Y方向的值
temp = analogRead(A3) ; //读取电位器的值
spead = map(temp, 0, 1023, 0, 255); //将电位器的数值等比转换位PWM以控制速度
Serial.println(spead);
// delay(1000);
radio.write(&spead, sizeof(spead)); //传输速度数据
if(Y > 700 && X > 300 && X < 700) //控制向下(后)移动
{‌
Serial.println("down");
Serial.print("x=");
Serial.println(X);
Serial.print("y=");
Serial.println(Y);
radio.write(&DOWN, sizeof(DOWN));
}
delay(10);
if(Y > 700 && X <300) //控制向右下(后)移动
{‌
Serial.println("RIGHT WHILE DOWN");
Serial.print("x=");
Serial.println(X);
Serial.print("y=");
Serial.println(Y);
radio.write(&DOWN, sizeof(RightWhileDown));
}
delay(10);
if(Y > 700 && X > 700) //控制向左下(后)移动
{‌
Serial.println("LEFT WHILE DOWN");
Serial.print("x=");
Serial.println(X);
Serial.print("y=");
Serial.println(Y);
radio.write(&DOWN, sizeof(LeftWhileDown));
}
delay(10);
if(Y < 300 && X > 300 && X < 700) //控制向上(前)移动
{‌
Serial.println("up");
Serial.print("x=");
Serial.println(X);
Serial.print("y=");
Serial.println(Y);
radio.write(&UP, sizeof(LeftWhileUP));
}
delay(10);
if(Y < 300 && X > 700) //控制向左上(前)移动
{‌
Serial.println("LEFT WHILE UP");
Serial.print("x=");
Serial.println(X);
Serial.print("y=");
Serial.println(Y);
radio.write(&UP, sizeof(UP));
}
delay(10);
if(Y < 300 && X <300) //控制向右上(前)移动
{‌
Serial.println("RIGHT WHILE UP");
Serial.print("x=");
Serial.println(X);
Serial.print("y=");
Serial.println(Y);
radio.write(&UP, sizeof(RightWhileUp));
}
delay(10);
if(X > 700 && Y > 300 && Y < 700) //控制向左移动
{‌
Serial.println("LEFT");
Serial.print("x=");
Serial.println(X);
Serial.print("y=");
Serial.println(Y);
radio.write(&LEFT, sizeof(LEFT));
}
delay(10);
if(X < 300 && Y > 300 && Y < 700) //控制向右移动
{‌
Serial.println("RIGHT");
Serial.print("x=");
Serial.println(X);
Serial.print("y=");
Serial.println(Y);
radio.write(&RIGHT, sizeof(RIGHT));
}
delay(10);
}

一下是我下载的库文件的代码:

/*
Copyright (c) 2007 Stefan Engelke <mbox@stefanengelke.de>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

/* Memory Map */
#define CONFIG 0x00
#define EN_AA 0x01
#define EN_RXADDR 0x02
#define SETUP_AW 0x03
#define SETUP_RETR 0x04
#define RF_CH 0x05
#define RF_SETUP 0x06
#define STATUS 0x07
#define OBSERVE_TX 0x08
#define CD 0x09
#define RX_ADDR_P0 0x0A
#define RX_ADDR_P1 0x0B
#define RX_ADDR_P2 0x0C
#define RX_ADDR_P3 0x0D
#define RX_ADDR_P4 0x0E
#define RX_ADDR_P5 0x0F
#define TX_ADDR 0x10
#define RX_PW_P0 0x11
#define RX_PW_P1 0x12
#define RX_PW_P2 0x13
#define RX_PW_P3 0x14
#define RX_PW_P4 0x15
#define RX_PW_P5 0x16
#define FIFO_STATUS 0x17
#define DYNPD 0x1C
#define FEATURE 0x1D

/* Bit Mnemonics */
#define MASK_RX_DR 6
#define MASK_TX_DS 5
#define MASK_MAX_RT 4
#define EN_CRC 3
#define CRCO 2
#define PWR_UP 1
#define PRIM_RX 0
#define ENAA_P5 5
#define ENAA_P4 4
#define ENAA_P3 3
#define ENAA_P2 2
#define ENAA_P1 1
#define ENAA_P0 0
#define ERX_P5 5
#define ERX_P4 4
#define ERX_P3 3
#define ERX_P2 2
#define ERX_P1 1
#define ERX_P0 0
#define AW 0
#define ARD 4
#define ARC 0
#define PLL_LOCK 4
#define RF_DR 3
#define RF_PWR 6
#define RX_DR 6
#define TX_DS 5
#define MAX_RT 4
#define RX_P_NO 1
#define TX_FULL 0
#define PLOS_CNT 4
#define ARC_CNT 0
#define TX_REUSE 6
#define FIFO_FULL 5
#define TX_EMPTY 4
#define RX_FULL 1
#define RX_EMPTY 0
#define DPL_P5 5
#define DPL_P4 4
#define DPL_P3 3
#define DPL_P2 2
#define DPL_P1 1
#define DPL_P0 0
#define EN_DPL 2
#define EN_ACK_PAY 1
#define EN_DYN_ACK 0

/* Instruction Mnemonics */
#define R_REGISTER 0x00
#define W_REGISTER 0x20
#define REGISTER_MASK 0x1F
#define ACTIVATE 0x50
#define R_RX_PL_WID 0x60
#define R_RX_PAYLOAD 0x61
#define W_TX_PAYLOAD 0xA0
#define W_ACK_PAYLOAD 0xA8
#define FLUSH_TX 0xE1
#define FLUSH_RX 0xE2
#define REUSE_TX_PL 0xE3
#define NOP 0xFF

/* Non-P omissions */
#define LNA_HCURR 0

/* P model memory Map */
#define RPD 0x09

/* P model bit Mnemonics */
#define RF_DR_LOW 5
#define RF_DR_HIGH 3
#define RF_PWR_LOW 1
#define RF_PWR_HIGH 2
收藏 ♥ 感谢
陆佩佩西蒙 2020-04-04 
@RaspiSQH 感谢🙏,应该是我自己copy的那个库文件有问题吧,但是我在Arduino IDE里下载的RF24库里也包含了一个nRF24l01的库文件,依旧是不行。发射机在使用的时候,tx的灯还会闪烁,而接收机没有任何有反应的迹象,rx的灯也不闪烁。
Spoony 小组长 2020-04-04 
@陆佩佩西蒙 那就不是缺少模块了,缺少模块是无法上传成功的。

登录注册 后可回复。