2023-09-04 06:54:15 +00:00
|
|
|
|
//player1,remote controler
|
|
|
|
|
#include<SPI.h>
|
|
|
|
|
#include<nRF24L01.h>
|
|
|
|
|
#include<RF24.h>
|
|
|
|
|
#include<TimerOne.h>
|
2023-09-06 09:06:17 +00:00
|
|
|
|
#include<SoftwareSerial.h>
|
2023-09-04 06:54:15 +00:00
|
|
|
|
RF24 radio(7,8);//端口可能要改
|
2023-09-06 09:06:17 +00:00
|
|
|
|
SoftwareSerial sendingSerial(5,6);
|
|
|
|
|
String sending="";
|
2023-09-04 13:38:24 +00:00
|
|
|
|
const int Rp=0,Rr=1,interval=20;//Rp,Rr 分别为两个遥杆的接口
|
|
|
|
|
long propeller=0,rudder=0,dist;//propeller为螺旋桨(0-1023,512为静止),rudder为舵(0-1023,512为静止)
|
|
|
|
|
long sending_signal=0;//发送的信号
|
2023-09-06 09:06:17 +00:00
|
|
|
|
long response=0;
|
2023-09-04 06:54:15 +00:00
|
|
|
|
const byte addresser[6]={"00001"};//创建通信通道地址,6用来写,8用来读
|
|
|
|
|
void setup() {
|
|
|
|
|
Serial.begin(9600);
|
2023-09-06 09:06:17 +00:00
|
|
|
|
sendingSerial.begin(38400);
|
2023-09-04 06:54:15 +00:00
|
|
|
|
radio.begin();
|
|
|
|
|
//radio.setChannel(114);//设置信道,114号通道
|
2023-09-04 13:38:24 +00:00
|
|
|
|
radio.openWritingPipe(addresser);
|
|
|
|
|
radio.openReadingPipe(1,addresser);
|
2023-09-04 06:54:15 +00:00
|
|
|
|
radio.setPALevel(RF24_PA_HIGH);
|
|
|
|
|
Timer1.initialize(interval);
|
2023-09-04 13:38:24 +00:00
|
|
|
|
Timer1.attachInterrupt(send);
|
2023-09-06 09:06:17 +00:00
|
|
|
|
radio.startListening();
|
2023-09-04 06:54:15 +00:00
|
|
|
|
}
|
2023-09-04 13:38:24 +00:00
|
|
|
|
void send(){
|
|
|
|
|
sending_signal=0;
|
2023-09-04 06:54:15 +00:00
|
|
|
|
radio.stopListening();
|
|
|
|
|
propeller=analogRead(Rp);
|
|
|
|
|
rudder=analogRead(Rr);
|
2023-09-04 13:38:24 +00:00
|
|
|
|
if(propeller>=1000)propeller=999;
|
|
|
|
|
if(rudder>=1000)rudder=999;
|
|
|
|
|
sending_signal+=propeller;
|
|
|
|
|
sending_signal+=rudder*1000;
|
|
|
|
|
sending_signal+=1000000;
|
|
|
|
|
radio.write(&sending_signal,sizeof(sending_signal));
|
2023-09-04 06:54:15 +00:00
|
|
|
|
Serial.println(propeller);
|
|
|
|
|
Serial.println(rudder);
|
2023-09-04 13:38:24 +00:00
|
|
|
|
Serial.print("propeller sent ");
|
2023-09-04 06:54:15 +00:00
|
|
|
|
Serial.println(propeller);
|
2023-09-04 13:38:24 +00:00
|
|
|
|
Serial.print("rudder sent ");
|
2023-09-04 06:54:15 +00:00
|
|
|
|
Serial.println(rudder);
|
2023-09-04 13:38:24 +00:00
|
|
|
|
Serial.println(sending_signal);
|
2023-09-06 09:06:17 +00:00
|
|
|
|
radio.startListening();
|
2023-09-04 06:54:15 +00:00
|
|
|
|
}
|
|
|
|
|
void loop() {
|
|
|
|
|
// put your main code here, to run repeatedly:
|
2023-09-06 09:06:17 +00:00
|
|
|
|
if(radio.available()){
|
|
|
|
|
radio.read(&response,sizeof(response));
|
|
|
|
|
if(response%1000000==1){
|
|
|
|
|
sending=String(response);
|
|
|
|
|
sendingSerial.print(sending);
|
|
|
|
|
sendingSerial.print('\n');
|
|
|
|
|
}
|
|
|
|
|
else if(response==2000000){
|
|
|
|
|
sending=String(response);
|
|
|
|
|
sendingSerial.print(sending);
|
|
|
|
|
sendingSerial.print('\n');
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-04 06:54:15 +00:00
|
|
|
|
}
|