标题:
基于Arduino的超声波测距倒车系统源程序 lcd1602显示
[打印本页]
作者:
923899652
时间:
2020-10-5 22:58
标题:
基于Arduino的超声波测距倒车系统源程序 lcd1602显示
代码已包括蜂鸣器 图中并没有显示出蜂鸣器 需自行加入
超声波.png
(323.8 KB, 下载次数: 78)
下载附件
2020-10-5 22:58 上传
Arduino源程序:
#include <LiquidCrystal.h>
const int RS=2, EN=3, DB4=4, DB5=5, DB6=6, DB7=9, beep=8;
const int TrigPin = 13;
const int EchoPin = 11;
float cm;
LiquidCrystal lcd(RS, EN, DB4, DB5, DB6, DB7);
void setup()
{
lcd.begin(16,2);
Serial.begin(9600);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
pinMode(13, OUTPUT);
pinMode(beep,OUTPUT); //蜂鸣器引脚设定为输出
lcd.print("yan wu"); //将hello,world!显示在LCD上
}
void loop()
{ digitalWrite(TrigPin, LOW); //低高低电平发一个短时间脉冲去TrigPinno
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
cm = pulseIn(EchoPin, HIGH) / 58; //将回波时间换算成cm
cm = (int(cm * 100)) / 100; //保留两位小数
for(int i = 0 ; i < 100 ; i++) //循环100次
{
digitalWrite(beep,HIGH); //设置输出高电平
delayMicroseconds(220); //延时PotBuffer值 us
digitalWrite(beep,LOW); //设置输出低电平
delayMicroseconds(220); //延时100us
}
if (cm < 10)
{
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
/*lcd.print(cm);
lcd.print("cm");//串口输出
lcd.println();*/
}
else if (cm > 10 && cm < 50)
{digitalWrite(13, HIGH);
delay(300);
digitalWrite(13, LOW);
/* lcd.print(cm);
lcd.print("cm");//串口输出
lcd.println();*/
}
else if (cm > 50)
{digitalWrite(13, HIGH);
delay(10);
digitalWrite(13, LOW);
/*lcd.print(cm);
lcd.print("cm");//串口输出
lcd.println();*/
}
Serial.print(cm);
Serial.print("cm");//串口输出
Serial.println();
lcd.setCursor(0, 1); //将闪烁的光标设置到column 0, line 1 (注释:从0开始数起,line 0是显示第一行,line 1是第二行。)
lcd.setCursor(5, 1);
lcd.print(cm);
lcd.setCursor(11, 1);
lcd.print("cm");//串口输出
lcd.println();
}
复制代码
作者:
黑狗与莲花
时间:
2021-11-10 18:45
这个显示的代码是完整的嘛?
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1