RC_Boat/remote_controler/remote_controler.ino

45 lines
1.4 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);//端口可能要改
2023-09-04 13:38:24 +00:00
const int Rp=0,Rr=1,interval=20;//Rp,Rr 分别为两个遥杆的接口
long propeller=0,rudder=0,dist;//propeller为螺旋桨0-1023512为静止rudder为舵0-1023512为静止
long sending_signal=0;//发送的信号
2023-09-04 06:54:15 +00:00
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号通道
2023-09-04 13:38:24 +00:00
radio.openWritingPipe(addresser);
radio.openReadingPipe(1,addresser);
2023-09-04 06:54:15 +00:00
radio.setPALevel(RF24_PA_HIGH);
Timer1.initialize(interval);
2023-09-04 13:38:24 +00:00
Timer1.attachInterrupt(send);
2023-09-04 06:54:15 +00:00
}
2023-09-04 13:38:24 +00:00
void send(){
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;
radio.write(&sending_signal,sizeof(sending_signal));
2023-09-04 06:54:15 +00:00
Serial.println(propeller);
Serial.println(rudder);
2023-09-04 13:38:24 +00:00
Serial.print("propeller sent ");
2023-09-04 06:54:15 +00:00
Serial.println(propeller);
2023-09-04 13:38:24 +00:00
Serial.print("rudder sent ");
2023-09-04 06:54:15 +00:00
Serial.println(rudder);
2023-09-04 13:38:24 +00:00
Serial.println(sending_signal);
2023-09-04 06:54:15 +00:00
}
void loop() {
// put your main code here, to run repeatedly:
2023-09-04 13:38:24 +00:00
2023-09-04 06:54:15 +00:00
}