arduino简单例子(2)

文章目录

  • 1接线图
  • 2代码

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

1接线图

(1)发送端
arduino简单例子(2)_第1张图片
(2)接收端
arduino简单例子(2)_第2张图片

2代码

(1)发送端

#include 
#include 
#include 

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 
#include  // 安装RF24库
#include 
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
  }
 }

你可能感兴趣的:(代码,esp8266,arduino)