76 lines
2.1 KiB
C++
76 lines
2.1 KiB
C++
//player1,remote controler
|
||
#include<SPI.h>
|
||
#include<nRF24L01.h>
|
||
#include<RF24.h>
|
||
#include<TimerOne.h>
|
||
#include<SoftwareSerial.h>
|
||
RF24 radio(7,8);//端口可能要改
|
||
SoftwareSerial sendingSerial(5,6);
|
||
String sending="";
|
||
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;//发送的信号
|
||
long response=0;
|
||
const byte addresser[6]={"00001"};//创建通信通道地址,6用来写,8用来读
|
||
long time=0;
|
||
void setup() {
|
||
Serial.begin(9600);
|
||
sendingSerial.begin(38400);
|
||
radio.begin();
|
||
//radio.setChannel(115);//设置信道,115号通道
|
||
radio.setDataRate(RF24_250KBPS);
|
||
radio.setRetries(3, 5);
|
||
radio.openWritingPipe(addresser);
|
||
radio.openReadingPipe(1,addresser);
|
||
radio.setPALevel(RF24_PA_MIN);
|
||
//Timer1.initialize(interval);
|
||
//Timer1.attachInterrupt(send);
|
||
radio.startListening();
|
||
}
|
||
void send(){
|
||
|
||
// Serial.println(propeller);
|
||
// Serial.println(rudder);
|
||
// Serial.print("propeller sent ");
|
||
// Serial.println(propeller);
|
||
// Serial.print("rudder sent ");
|
||
// Serial.println(rudder);
|
||
// Serial.println(sending_signal);
|
||
|
||
//Serial.println(millis()-time);
|
||
}
|
||
void loop() {
|
||
// put your main code here, to run repeatedly:
|
||
time=millis();
|
||
//Serial.println("send");
|
||
sending_signal=0;
|
||
radio.stopListening();
|
||
propeller=analogRead(Rp);
|
||
rudder=analogRead(Rr);
|
||
if(propeller>=1000)propeller=999;
|
||
if(rudder>=1000)rudder=999;
|
||
sending_signal+=propeller;
|
||
sending_signal+=rudder*1000;
|
||
sending_signal+=1000000;
|
||
bool b=radio.write(&sending_signal,sizeof(sending_signal));
|
||
Serial.println(b);
|
||
radio.startListening();
|
||
delay(interval);
|
||
//Serial.println("loop");
|
||
if(radio.available()){
|
||
Serial.print("response:");
|
||
radio.read(&response,sizeof(response));
|
||
Serial.println(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');
|
||
}
|
||
}
|
||
}
|