系统连线图:
Arduino 程序:
#include <LiquidCrystal.h>//1602头文件
#include<SoftwareSerial.h>//软串口头文件
SoftwareSerial Serial1(16, 17); //定义软串口名称为Serial1,并把A2定为RX,A3定为TX
/*对于有多个串口的arduino板如DUE板,注释掉上面两段代码,直接使用Serial1串口*/
#define MAX_FIFO 10//距离fifo的个数,连续这些个距离达标才会响应
int dist;//雷达实测距离值
int strength;//雷达信号强度
int check;//校验数值存放
int i;
int uart[9];//存放雷达测量的数据
const int HEADER = 0x59; //数据包帧头
int outputpin = 14;//状态输出IO,14==A0引脚
int outputpin_GND = 15;//状态输出IO的地,15==A1引脚
int outputpin1 = 12;//状态输出IO,12==D12引脚
int outputpin_GND1 = 13;//状态输出IO的地,13==D13引脚
int outputpin2 = 10;//状态输出IO,10==D10引脚
int outputpin_GND2 = 11;//状态输出IO的地,11==D11引脚
int fifo_dis[MAX_FIFO];//距离值fifo,用于消除距离抖动
unsigned int num = 0;//记录大于距离阈值的个数
int uart_eeprom_rec[5];//接收设定距离阈值串口数据
int check1;//串口0的校验和
unsigned int th_value;//距离阈值,根据这个距离值采取动作
String x;//串口输出状态字符串
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 8, en = 6, d4 = 5, d5 = 4, d6 = 3, d7 = 2, rw = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
pinMode(outputpin, OUTPUT);
pinMode(outputpin_GND, OUTPUT);
pinMode(outputpin1, OUTPUT);
pinMode(outputpin_GND1, OUTPUT);
pinMode(outputpin2, OUTPUT);
pinMode(outputpin_GND2, OUTPUT);
pinMode(rw, OUTPUT);
digitalWrite(outputpin_GND, LOW);//IO置低
digitalWrite(outputpin_GND1, LOW);//IO置低
digitalWrite(outputpin_GND2, LOW);//IO置低
digitalWrite(rw, LOW);//IO置低
Serial.begin(115200);//设置arduino与电脑连接串口的波特率
Serial1.begin(115200);//设置雷达与arduino连接串口的波特率
if(th_value <30 || th_value >1200)//距离阈值保护,在正常范围才采纳,否则设为1.5米
th_value = 150;//如果阈值设置超过量程那么默认设为1米5
delay(1);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
}
void loop()
{
if (Serial1.available())//查看串口是否有数据输入,接收雷达发送的数据
{
if (Serial1.read() == HEADER) //判断数据包帧头0x59
{
uart[0] = HEADER;
if (Serial1.read() == HEADER) //判断数据包帧头0x59
{
uart[1] = HEADER;
for (i = 2; i < 9; i++) //存储数据到数组
{
uart[i] = Serial1.read();
}
check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7];
if (uart[8] == (check & 0xff)) //按照协议对收到的数据进行校验
{
dist = uart[2] + uart[3] * 256; //计算距离值
//消除距离抖动的fifo
for(i=MAX_FIFO;i>=1;i--)
fifo_dis[i]=fifo_dis[i-1];
fifo_dis[0] = dist;
//fifo 里距离判断,连续MAX_FIFO个数值
for(i=0;i<MAX_FIFO;i++)
{
if(fifo_dis[i]>=th_value)
num++;
}
lcd.setCursor(0, 0);
lcd.print("D=");
if(dist<100 && dist >=10)
{
lcd.print(" ");
lcd.print(dist);
}
else if(dist>0 && dist<10)
{
lcd.print(" ");
lcd.print(dist);
}
else
lcd.print(dist);
lcd.print("cm S=");
strength = uart[4] + uart[5] * 256; //计算信号强度值
if(strength>=100 && strength<=999)
{
lcd.print(strength);
lcd.print(" ");
}
else if(strength<100 && strength>=10)
{
lcd.print(strength);
lcd.print(" ");
}
else if(strength>=1000)
lcd.print(strength);
//串口输出信息
Serial.print("dist = ");
Serial.print(dist);//输出雷达测试距离值
Serial.print('\t');
Serial.print("strength = ");
Serial.print(strength);//输出信号强度值
Serial.print('\t');
//进行距离阈值判断
if(num>=MAX_FIFO)//连续MAX_FIFO个距离值都大于阈值IO就输出高
{
digitalWrite(outputpin, HIGH);//IO输出高
digitalWrite(outputpin1, LOW);//IO置低
digitalWrite(outputpin2, LOW);//IO置低
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
x=" OPEN ";
}
else if(num == 0)//连续MAX_FIFO个距离值都小于阈值IO就输出低
{
digitalWrite(outputpin, LOW);//IO置低
digitalWrite(outputpin1, HIGH);//IO置低
digitalWrite(outputpin2, HIGH);//IO置高
digitalWrite(LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level)
x="CLOSE";
}
num = 0;
lcd.setCursor(0, 1);
lcd.print("Th:");
lcd.print(th_value);
lcd.print(" Sa:");
lcd.print(MAX_FIFO);
lcd.print(" ");
lcd.print(x);
Serial.print(x);
Serial.print('\t');
Serial.print("thres = ");
Serial.print(th_value);
Serial.print('\n');
}
}
}
}
}