|
本产品主要用的是nrf24l01 PA通信
接收端代码
//发送端
#include<SPI.h>
#include"RF24.h"
#include<nRF24L01.h>
#include "LiquidCrystal_I2C.h"
#include "Wire.h"
//定义RF的引脚
int ce = 7;
int csn = 8;
//定义左边遥感的引脚
int vrx_left = A0;
int vry_left = A1;
int button_left = A2;
//定义右边遥感的引脚
int vrx_right = A3;
int vry_right = A4;
int button_right = A5;
//定义一个点位器的引脚
int dianweiqi1 = A8;
int dianweiqi2 = A9;
int button_left_read=45;
int button_right_read=22;
LiquidCrystal_I2C mylcd(0x27, 16, 2);//初始化一个IIC
RF24 radio(ce, csn);
//定义一个结构体来存放摇杆的值
int i ,clk,dangwei;
String lr,lr1;
float poting1, poting2;
const byte address[6] = "00001";
void setup()
{
mylcd.init(); //、、、、、、、、、、、、、、、、、、、、、、、初始化liquidcrytal_2c
mylcd.backlight();
Serial.begin(9600);
delay (1000);
delay (1000);
Serial.print("");
radio.begin();
radio.openWritingPipe(address); //要写入的地址
//数组长度,以及地址
radio.setPALevel(RF24_PA_MAX); //发送最小距离
radio.stopListening();
pinMode(vrx_left, INPUT); //定义了引脚,输出值据查,应该值为0-1023
pinMode(vry_left, INPUT);
pinMode(button_left, INPUT);
pinMode(vrx_right, INPUT);
pinMode(vry_right, INPUT);
pinMode(button_right, INPUT);
pinMode(dianweiqi1, INPUT);
pinMode(dianweiqi2, INPUT);
pinMode(button_left_read, INPUT);
pinMode(button_right_read=22, INPUT);
}
void loop()
{
struct data_pack
{
byte left_pot_Y;
byte left_pot_X;
byte left_button;
byte right_pot_Y;
byte right_pot_X;
byte right_button;
byte poting1;
byte poting2;
byte button_left_read;
byte button_right_read;
};
data_pack data;
data.left_pot_Y = map(analogRead(vrx_left), 504, 1023, 1, 255);
data.left_pot_X = map(analogRead(vry_left), 0, 1023, 1, 255);
data.left_button = digitalRead(button_left);
data.right_pot_Y = map(analogRead(vrx_right), 0, 1023, 1, 255);
data.right_pot_X = map(analogRead(vry_right), 0, 1023, 1, 255);
data.right_button = digitalRead(button_right);
data.poting1 = map(analogRead(dianweiqi1), 0, 1023, 1, 255);
data.poting2 = map(analogRead(dianweiqi2), 0, 1023, 1, 255);
data.button_left_read=digitalRead(button_left_read);
data.button_right_read=digitalRead(button_right_read);
radio.write(&data, sizeof(data_pack));
if(data.left_pot_Y>70)
{
dangwei=1;
}
if(data.left_pot_Y>170)
{
dangwei=2;
}
if(data.left_pot_Y>230)
{
dangwei=3;
}
if(data.right_pot_X==1)
{
lr="left";
}
else if(data.right_pot_X==255)
{
lr="righ";
}
else if(data.right_pot_X>120&&data.right_pot_X<135)
{
lr="goo!";
}
if(data.right_pot_Y<20)
{
lr1="dow";
}
else if(data.right_pot_Y>240)
{
lr1="UP~";
}else if(data.right_pot_X>120&&data.right_pot_X<135)
{
lr1="go!";
}
mylcd.setCursor(0,0);
mylcd.print("G:"); //在1602上面显示T;
mylcd.setCursor(2,0); //光标放在第五列第一行
mylcd.print(dangwei);
mylcd.setCursor(4,0); //光标放在第一行第二列
mylcd.print("F:");
mylcd.setCursor(6,0);
mylcd.print(lr);
mylcd.setCursor(11,0); //光标放在第一行第二列
mylcd.print("Q:");
mylcd.setCursor(13,0);
mylcd.print(lr1);
mylcd.setCursor(0,1);
mylcd.print("DL:");
mylcd.setCursor(3,1);
mylcd.print(data.poting1);
mylcd.setCursor(7,1);
mylcd.print("RP:");
mylcd.setCursor(10,1);
mylcd.print(data.poting2);
mylcd.setCursor(13,1);
mylcd.print(data.button_left_read);
mylcd.setCursor(15,1);
mylcd.print(data.button_right_read);
Serial.println(data.right_pot_X);
clk=millis();
if(clk%10==0)
{
mylcd.clear();
clk=0;
}
}
接收端
#include<SPI.h>
#include <RF24.h>
#include <Servo.h>
#include<nRF24L01.h>
int ce = 7;
int sec =8;
int dianji1_pin = A0; //电机
int dianji2_pin = A2; //电机
int myservo_left_pin = 3; //左翅膀电机
int myserve_right_pin = 5; //右翅膀电机
int myservo_last_pin = 9; // 机尾左右机
int myservo_buttom_pin = 4; //机尾升降机
int myservo_duraction_pin = 10; // 滑轮左右摇摆
int led = 6;
RF24 radio(ce, sec);
struct data_pack1
{
byte left_pot_Y;
byte left_pot_X;
byte left_button;
byte right_pot_Y;
byte right_pot_X;
byte right_button;
byte poting1;
byte poting2;
byte button_left_read;
byte button_right_read;
};
data_pack1 data1;
Servo dianji1;
Servo dianji2;
Servo myservo_left;
Servo myserve_right;
Servo myservo_last;
Servo myservo_buttom;
Servo myservo_duraction;
const byte address[6] = "00001";
int throttleValue, rudderValue = 90, elevatorValue = 80, aileron1Value = 90, aileron2Value, travelAdjust1, travelAdjust2;
void setup()
{
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address); //要写入的地址
radio.setPALevel(RF24_PA_MAX);
radio.startListening(); // Set the module as receiver
dianji1.attach(dianji1_pin);
dianji2.attach(dianji2_pin);
myservo_left.attach(myservo_left_pin);
myserve_right.attach(myserve_right_pin);
myservo_last.attach(myservo_last_pin);
myservo_buttom.attach(myservo_buttom_pin);
myservo_duraction.attach(myservo_duraction_pin);
pinMode(led, OUTPUT);
}
void loop()
{
if (radio.available())
{
travelAdjust1 = map(data1.poting1, 0, 255, 1000,1700 );
travelAdjust2 = map(data1.poting2, 0, 255, 0, 35);
radio.read(&data1, sizeof(data_pack1));//读取整个数据并存储到'data'结构中
throttleValue = map(data1.left_pot_Y, 0, 255, travelAdjust1, 2000);
// |
|