RC_Boat/remote_controler/remote_controler.ino

59 lines
1.7 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>
RF24 radio(7,8);//端口可能要改
const int Rp=0,Rr=1,interval=20;//Rp,Rr 分别为两个遥杆的接口,间断20ms
long propeller=0,rudder=0;//propeller为螺旋桨0-1023512为静止rudder为舵0-1023512为静止
long s_propeller=0,s_rudder=0;//发送的信号
long check_array[6];
const byte addresser[6]={"00001"};//创建通信通道地址6用来写8用来读
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
radio.begin();
//radio.setChannel(114);//设置信道114号通道
radio.openWritingPipe(addresser);//6
radio.openReadingPipe(1,addresser);//8
radio.setPALevel(RF24_PA_HIGH);
Timer1.initialize(interval);
Timer1.attachInterrupt(Send);
}
void Send(){//发送信号函数
radio.stopListening();
Serial.println("stop listening!");
propeller=analogRead(Rp);
rudder=analogRead(Rr);
Serial.println(propeller);
Serial.println(rudder);
propeller+=create_checknumber(propeller)*10000;
rudder+=create_checknumber(rudder)*10000;
propeller+=100000;
rudder+=200000;
radio.write(&propeller,sizeof(propeller));
radio.write(&rudder,sizeof(rudder));
Serial.print("propeller sent");
Serial.println(propeller);
Serial.print("rudder sent");
Serial.println(rudder);
}
long create_checknumber(long x){//创造校验码校验码规则个位×4十位×3百位×2千位×1
long add=0,sum=0;
while(x!=0){
check_array[add]=x%10;
x/=10;
add++;
}
for(int i=0,j=4;i<add;j--,i++){
sum+=check_array[i]*j;
}
return sum%10;
}
void loop() {
// put your main code here, to run repeatedly:
radio.startListening();
//读取雷达信息,还未写
}