RC_Boat/remote_controler/remote_controler.ino

70 lines
2.0 KiB
C++
Raw 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<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=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);
radio.begin();
//radio.setChannel(114);//设置信道,114号通道
radio.openWritingPipe(addresser);
radio.openReadingPipe(1,addresser);
radio.setPALevel(RF24_PA_MIN);
Timer1.initialize(interval);
Timer1.attachInterrupt(send);
radio.startListening();
}
void send(){
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;
radio.write(&sending_signal,sizeof(sending_signal));
// Serial.println(propeller);
// Serial.println(rudder);
// Serial.print("propeller sent ");
// Serial.println(propeller);
// Serial.print("rudder sent ");
// Serial.println(rudder);
// Serial.println(sending_signal);
radio.startListening();
//Serial.println(millis()-time);
}
void loop() {
// put your main code here, to run repeatedly:
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');
}
}
}