RC_Boat_bluetooth/remote_controler/remote_controler.ino

69 lines
1.9 KiB
Arduino
Raw Permalink Normal View History

2023-09-09 12:00:13 +00:00
//player1,remote controler
#include<SPI.h>
#include<SoftwareSerial.h>
2023-09-09 15:48:56 +00:00
//RF24 radio(7,8);//端口可能要改
bool flag=false;
2023-09-09 12:00:13 +00:00
SoftwareSerial sendingSerial(5,6);
2023-09-09 15:48:56 +00:00
SoftwareSerial bluetoothSerial(7,8);
String sending="",received="";
2023-09-11 17:59:02 +00:00
const int Rp=3,Rr=2,interval=100;//Rp,Rr 分别为两个遥杆的接口
2023-09-09 12:00:13 +00:00
long propeller=0,rudder=0,dist;//propeller为螺旋桨0-1023512为静止rudder为舵0-1023512为静止
long sending_signal=0;//发送的信号
long response=0;
2023-09-09 15:48:56 +00:00
//const byte addresser[6]={"00001"};//创建通信通道地址6用来写8用来读
2023-09-09 12:00:13 +00:00
long time=0;
void setup() {
2023-09-11 17:59:02 +00:00
2023-09-09 12:00:13 +00:00
Serial.begin(9600);
sendingSerial.begin(38400);
2023-09-09 15:48:56 +00:00
bluetoothSerial.begin(57600);
bluetoothSerial.listen();
2023-09-09 12:00:13 +00:00
}
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:
2023-09-09 15:48:56 +00:00
//time=millis();
2023-09-09 12:00:13 +00:00
//Serial.println("send");
sending_signal=0;
propeller=analogRead(Rp);
rudder=analogRead(Rr);
2023-09-12 09:14:26 +00:00
propeller=propeller*1.18;
rudder=rudder*1.18;
2023-09-09 12:00:13 +00:00
if(propeller>=1000)propeller=999;
if(rudder>=1000)rudder=999;
2023-09-12 09:14:26 +00:00
rudder=999-rudder;
2023-09-09 12:00:13 +00:00
sending_signal+=propeller;
sending_signal+=rudder*1000;
sending_signal+=1000000;
2023-09-09 15:48:56 +00:00
sending=String(sending_signal);
bluetoothSerial.println(sending);
//Serial.println(sending);
2023-09-09 12:00:13 +00:00
delay(interval);
2023-09-09 15:48:56 +00:00
while(bluetoothSerial.available()>0){
//Serial.println("available!");
if(bluetoothSerial.peek()!='\n'){//peek()查看当前字节是不是换行
received+=(char)bluetoothSerial.read();
//Serial.println(received);
//Serial.println("no \n");
2023-09-09 12:00:13 +00:00
}
2023-09-09 15:48:56 +00:00
else{
bluetoothSerial.read();//把换行符读掉
Serial.println(received);
sendingSerial.print(received);
2023-09-09 12:00:13 +00:00
sendingSerial.print('\n');
2023-09-09 15:48:56 +00:00
received="";
2023-09-09 12:00:13 +00:00
}
}
}