arduino简单例子(2)

文章目录

arduino nrf 简单例子(1)
在上篇 文章中指出如何接收和发送 -----算是入门吧!
这一篇 加入了摇杆

1接线图

(1)发送端
在这里插入图片描述
(2)接收端
在这里插入图片描述

2代码

(1)发送端

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

const uint64_t pipeOut = 0xBBBBBBBBB;   //为何这么多B币?与接收器中相同的地址进行通信
RF24 radio(7, 8); // SPI通信,引脚对应关系:CE ->7,CSN ->8

const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogInPin1 = A1;  // Analog input pin that the potentiometer is attached to

int sensorValue[2] ;        // value read from the pot   这是发送的数据  摇杆x y轴的数据


void setup()
{
 
  radio.begin();
  radio.openWritingPipe(pipeOut);//pipeOut通信地址
  radio.stopListening(); //发射模式
   Serial.begin(115200);
}
void loop()
{   
   // 读取摇杆的数据
  sensorValue[0] = analogRead(analogInPin);
   sensorValue[1] = analogRead(analogInPin1);

  //串口打印
  Serial.print("sensor_A0 = ");
Serial.print(sensorValue[0]);
  Serial.print("      ");
  Serial.print("sensor_A1 = ");
  Serial.print(sensorValue[1]);
    Serial.print("\r\n");
    
      //发送
  radio.write(sensorValue, sizeof(sensorValue));//将数据发送出去 其中sizeof(sensorValue)一定要注意数组大小怎么求的  
                                                                          //不然有可能只发送数组中的第一个数据

}

(2)接收端

#include <SPI.h>
#include <nRF24L01.h> // 安装RF24库
#include <RF24.h>
int a[2];
unsigned long lastRecvTime = 0;
const uint64_t pipeIn = 0xBBBBBBBBB; // 与发射端地址相同

RF24 radio(7, 8);     // SPI通信,引脚对应关系:CE ->7,CSN ->8
//函数声明 开始----- 
void recvData( void);
//函数声明 结束----- 

void setup() {
 
  radio.begin();
  radio.openReadingPipe(1,pipeIn); // 与发射端地址相同
  radio.startListening(); // 接收模式
  Serial.begin(115200, SERIAL_8E2); // 将串口波特率设为100000,数据位8,偶校验,停止位2Serial.begin(100000, SERIAL_8E2)
}

void loop() { 
    recvData();

    Serial.print(a[0]);
        Serial.print("     ");
     Serial.println(a[1]);
  }

void recvData(){
  while( radio.available() ) {
    Serial.println("new data");
     radio.read(a, sizeof(a));//接收数据,*sizeof(a)是数组的大小而不是数组中的每个数的大小*
    lastRecvTime = millis();   //当前时间ms
  }
 }

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值