68 lines
1.9 KiB
C++
68 lines
1.9 KiB
C++
//player1,remote controler
|
||
#include<SPI.h>
|
||
#include<SoftwareSerial.h>
|
||
//RF24 radio(7,8);//端口可能要改
|
||
bool flag=false;
|
||
SoftwareSerial sendingSerial(5,6);
|
||
SoftwareSerial bluetoothSerial(7,8);
|
||
String sending="",received="";
|
||
const int Rp=3,Rr=2,interval=100;//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);
|
||
bluetoothSerial.begin(57600);
|
||
bluetoothSerial.listen();
|
||
}
|
||
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;
|
||
propeller=analogRead(Rp);
|
||
rudder=analogRead(Rr);
|
||
propeller=propeller*1.41;
|
||
rudder=rudder*1.41;
|
||
if(propeller>=1000)propeller=999;
|
||
if(rudder>=1000)rudder=999;
|
||
sending_signal+=propeller;
|
||
sending_signal+=rudder*1000;
|
||
sending_signal+=1000000;
|
||
sending=String(sending_signal);
|
||
bluetoothSerial.println(sending);
|
||
//Serial.println(sending);
|
||
delay(interval);
|
||
while(bluetoothSerial.available()>0){
|
||
//Serial.println("available!");
|
||
if(bluetoothSerial.peek()!='\n'){//peek()查看当前字节是不是换行
|
||
received+=(char)bluetoothSerial.read();
|
||
//Serial.println(received);
|
||
//Serial.println("no \n");
|
||
}
|
||
else{
|
||
bluetoothSerial.read();//把换行符读掉
|
||
Serial.println(received);
|
||
sendingSerial.print(received);
|
||
sendingSerial.print('\n');
|
||
received="";
|
||
}
|
||
}
|
||
}
|