RC_Boat_bluetooth/remote_controler/remote_controler.ino

69 lines
1.9 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//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-1023512为静止rudder为舵0-1023512为静止
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.18;
rudder=rudder*1.18;
if(propeller>=1000)propeller=999;
if(rudder>=1000)rudder=999;
rudder=999-rudder;
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="";
}
}
}