full functions

This commit is contained in:
clf3 2023-09-06 17:06:17 +08:00
parent 46182acec1
commit d901d7a080
4 changed files with 134 additions and 6 deletions

View File

@ -1,7 +1,6 @@
#include <Servo.h> #include <Servo.h>
#include <SPI.h> #include <SPI.h>
#include "Ucglib.h" #include "Ucglib.h"
#define trigPin 6 //超声波模块的Trig口 6# #define trigPin 6 //超声波模块的Trig口 6#
#define echoPin 5 //超声波模块的echo口 5# #define echoPin 5 //超声波模块的echo口 5#
#define ServoPin 3 //底座舵机端口 3# #define ServoPin 3 //底座舵机端口 3#

View File

@ -3,13 +3,18 @@
#include<nRF24L01.h> #include<nRF24L01.h>
#include<RF24.h> #include<RF24.h>
#include<TimerOne.h> #include<TimerOne.h>
#include<SoftwareSerial.h>
RF24 radio(7,8);//端口可能要改 RF24 radio(7,8);//端口可能要改
SoftwareSerial sendingSerial(5,6);
String sending="";
const int Rp=0,Rr=1,interval=20;//Rp,Rr 分别为两个遥杆的接口 const int Rp=0,Rr=1,interval=20;//Rp,Rr 分别为两个遥杆的接口
long propeller=0,rudder=0,dist;//propeller为螺旋桨0-1023512为静止rudder为舵0-1023512为静止 long propeller=0,rudder=0,dist;//propeller为螺旋桨0-1023512为静止rudder为舵0-1023512为静止
long sending_signal=0;//发送的信号 long sending_signal=0;//发送的信号
long response=0;
const byte addresser[6]={"00001"};//创建通信通道地址6用来写8用来读 const byte addresser[6]={"00001"};//创建通信通道地址6用来写8用来读
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
sendingSerial.begin(38400);
radio.begin(); radio.begin();
//radio.setChannel(114);//设置信道114号通道 //radio.setChannel(114);//设置信道114号通道
radio.openWritingPipe(addresser); radio.openWritingPipe(addresser);
@ -17,6 +22,7 @@ void setup() {
radio.setPALevel(RF24_PA_HIGH); radio.setPALevel(RF24_PA_HIGH);
Timer1.initialize(interval); Timer1.initialize(interval);
Timer1.attachInterrupt(send); Timer1.attachInterrupt(send);
radio.startListening();
} }
void send(){ void send(){
sending_signal=0; sending_signal=0;
@ -36,8 +42,21 @@ void send(){
Serial.print("rudder sent "); Serial.print("rudder sent ");
Serial.println(rudder); Serial.println(rudder);
Serial.println(sending_signal); Serial.println(sending_signal);
radio.startListening();
} }
void loop() { void loop() {
// put your main code here, to run repeatedly: // put your main code here, to run repeatedly:
if(radio.available()){
radio.read(&response,sizeof(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');
}
}
} }

View File

@ -0,0 +1,106 @@
#include <SPI.h>
#include<SoftwareSerial.h>
#include "Ucglib.h"
#define interruptPin 2
SoftwareSerial sendingSerial(6,7);
int Ymax = 128; //屏幕的竖向像素数
int Xmax = 160; //屏幕的横向像素数
int Xcent = Xmax / 2; //x中位
int base = 118; //基线高度
int scanline = 105; //雷达扫描线长度
int outcircle=80;
int midcircle=60;
int midcircle2=40;
int incircle=20;
int x=90;
int distance=50;
bool rec=false;
bool clr=false;
bool ignoreClr=false;
String received="";//用来接受字符
Ucglib_ST7735_18x128x160_HWSPI ucg(/*cd=*/ 9, /*cs=*/ 10, /*reset=*/ 8);
void fix_font()
{
ucg.setColor(0, 180, 0);
ucg.setPrintPos(70,46);
ucg.print("1.00");
ucg.setPrintPos(70,66);
ucg.print("0.75");
ucg.setPrintPos(70,86);
ucg.print("0.50");
ucg.setPrintPos(70,106);
ucg.print("0.25");
}
void fix()
{
ucg.setColor(0, 40, 0);
//画基线圆盘
ucg.drawDisc(Xcent, base+1, 3, UCG_DRAW_ALL);
ucg.drawCircle(Xcent, base+1, outcircle, UCG_DRAW_UPPER_LEFT);
ucg.drawCircle(Xcent, base+1, outcircle, UCG_DRAW_UPPER_RIGHT);
ucg.drawCircle(Xcent, base+1, midcircle, UCG_DRAW_UPPER_LEFT);
ucg.drawCircle(Xcent, base+1, midcircle, UCG_DRAW_UPPER_RIGHT);
ucg.drawCircle(Xcent, base+1, midcircle2, UCG_DRAW_UPPER_LEFT);
ucg.drawCircle(Xcent, base+1, midcircle2, UCG_DRAW_UPPER_RIGHT);
ucg.drawCircle(Xcent, base+1, incircle, UCG_DRAW_UPPER_LEFT);
ucg.drawCircle(Xcent, base+1, incircle, UCG_DRAW_UPPER_RIGHT);
ucg.drawLine(0, base+1, Xmax,base+1);
ucg.setColor(0, 120, 0);
}
void setup() {
ucg.begin(UCG_FONT_MODE_SOLID); //初始化屏幕
ucg.setRotate90(); //设置成横屏 如果屏幕显示方向是反的,可以修改函数 setRotate90 或 setRotate270
Serial.begin(9600); //设置串口传输率
sendingSerial.begin(38400);
sendingSerial.listen();//必须要加
ucg.clearScreen();
//cls();
ucg.setFontMode(UCG_FONT_MODE_SOLID);
ucg.setFont(ucg_font_orgv01_hr);
}
void loop() {
while(sendingSerial.available()){
if(sendingSerial.peek()!='\n'){//peek()查看当前字节是不是换行
received+=(char)sendingSerial.read();
}
else{
rec=true;
sendingSerial.read();//把换行符读掉
if(received[0]=='1'){
ignoreClr=false;
distance=received.substring(4).toInt();//个十百3位数
x=received.substring(1,4).toInt();//第456位 三位数
}
else if(received[0]=='2'){
if(!ignoreClr){
clr=true;
}
}
received="";
}
}
if(rec){
if(clr){
ucg.clearScreen();
fix();
fix_font(); //重绘屏幕背景元素
clr=false;
ignoreClr=true;
return;
}
else if(ignoreClr){
return;
}
if (distance < 100){
ucg.setColor(255,0,0);
ucg.drawDisc(0.8*distance*cos(radians(x))+Xcent,-0.8*distance*sin(radians(x))+base, 1, UCG_DRAW_ALL);
}
else{ //超过1米以上的用黄色画在边缘区域示意
ucg.setColor(255,255,0);
ucg.drawDisc((outcircle-1)*cos(radians(x))+Xcent,-(outcircle-1)*sin(radians(x))+base, 1, UCG_DRAW_ALL);
}
rec=false;
}
}

View File

@ -32,6 +32,7 @@ long dj,z;//dj是舵机z是螺旋桨转速
//long propeller=0,rudder=0;//propeller为螺旋桨0-1023512为静止rudder为舵0-1023512为静止 //long propeller=0,rudder=0;//propeller为螺旋桨0-1023512为静止rudder为舵0-1023512为静止
//long s_propeller=0,s_rudder=0;//发送的信号 //long s_propeller=0,s_rudder=0;//发送的信号
long propeller,rudder,content;//校验序列、读取内容和解码内容 long propeller,rudder,content;//校验序列、读取内容和解码内容
long response=0;
const byte addresses[6] = "00001"; //创建一个数组,建立接收机地址,或者说两个模块将用于通信的“管道” const byte addresses[6] = "00001"; //创建一个数组,建立接收机地址,或者说两个模块将用于通信的“管道”
int calculateDistance() int calculateDistance()
{ {
@ -71,10 +72,9 @@ long confirm(long x){
if(x>=1000000&&x<=2000000)return x%1000000; if(x>=1000000&&x<=2000000)return x%1000000;
else return -1; else return -1;
} }
void loop() { void loop() {
radio.startListening(); radio.startListening();
radio.startListening(); if(radio.available()){
if(radio.available()>0){
radio.read(&content,sizeof(content)); radio.read(&content,sizeof(content));
Serial.println("The content is"); Serial.println("The content is");
Serial.println(content);//调试 Serial.println(content);//调试
@ -148,6 +148,7 @@ void loop() {
//测距 //测距
distance = calculateDistance(); distance = calculateDistance();
//send distance //send distance
response=1000000+x*1000+distance;
} }
else if(status==1){ else if(status==1){
if(x>0){ if(x>0){
@ -159,6 +160,7 @@ void loop() {
RadarServo.write(x); //调整舵机角度 RadarServo.write(x); //调整舵机角度
//测距 //测距
distance = calculateDistance(); distance = calculateDistance();
response=1000000+x*1000+distance;
//send distance //send distance
} }
else if(status=2){ else if(status=2){
@ -173,7 +175,9 @@ void loop() {
} }
} }
//send clear //send clear
response=2000000;
} }
radio.write(response,sizeof(response));
delay(20-distance/17); delay(20-distance/17);
} }