RC_Boat/remote_controler/remote_controler.ino

76 lines
2.1 KiB
Arduino
Raw Normal View History

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-09 11:44:10 +00:00
const int Rp=0,Rr=1,interval=20;//Rp,Rr 分别为两个遥杆的接口
2023-09-04 13:38:24 +00:00
long propeller=0,rudder=0,dist;//propeller为螺旋桨0-1023512为静止rudder为舵0-1023512为静止
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用来读
2023-09-08 13:41:54 +00:00
long time=0;
2023-09-04 06:54:15 +00:00
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();
2023-09-09 11:44:10 +00:00
//radio.setChannel(115);//设置信道115号通道
radio.setDataRate(RF24_250KBPS);
radio.setRetries(3, 5);
2023-09-04 13:38:24 +00:00
radio.openWritingPipe(addresser);
radio.openReadingPipe(1,addresser);
2023-09-08 13:41:54 +00:00
radio.setPALevel(RF24_PA_MIN);
2023-09-09 11:44:10 +00:00
//Timer1.initialize(interval);
//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(){
2023-09-09 11:44:10 +00:00
// 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-08 13:41:54 +00:00
time=millis();
//Serial.println("send");
2023-09-04 13:38:24 +00:00
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;
2023-09-09 11:44:10 +00:00
bool b=radio.write(&sending_signal,sizeof(sending_signal));
Serial.println(b);
2023-09-06 09:06:17 +00:00
radio.startListening();
2023-09-09 11:44:10 +00:00
delay(interval);
//Serial.println("loop");
2023-09-06 09:06:17 +00:00
if(radio.available()){
2023-09-08 13:41:54 +00:00
Serial.print("response:");
2023-09-06 09:06:17 +00:00
radio.read(&response,sizeof(response));
2023-09-07 16:52:41 +00:00
Serial.println(response);
2023-09-06 09:06:17 +00:00
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
}