krwlng 发表于 2017-6-20 10:07:57

超声波测距仪

<font size="5">超声波测距仪其实是个很简单的小玩意:要的材料有arduino开发板 超声波模块 lcd显示屏</font><br><div align="left"><font color="#000000" size="4">/*</font></div><div align="left"><font color="#000000"><font size="4"><font face="宋体">功能:利用</font>SR04超声波传感器进行测距,并用串口显示测出的距离值</font></font></div><div align="left"><font color="#000000" size="4">*/</font></div><div align="left"><font color="#000000"> </font></div><div align="left"><font color="#000000" size="4">#include &lt;LiquidCrystal.h&gt;</font></div><div align="left"><font color="#000000" size="4">LiquidCrystal lcd(12, 11, 10, 9, 8, 7);//设置接口</font></div><div align="left"><font color="#000000" size="4">// 设定SR04连接的Arduino引脚</font></div><div align="left"><font color="#000000" size="4">const int TrigPin = 2; </font></div><div align="left"><font color="#000000" size="4">const int EchoPin = 3; </font></div><div align="left"><font color="#000000" size="4">float distance; </font></div><div align="left"><font color="#000000" size="4">void setup() </font></div><div align="left"><font color="#000000" size="4">{&nbsp; &nbsp;// 初始化串口通信及连接SR04的引脚</font></div><div align="left"><font color="#000000" size="4">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Serial.begin(9600); </font></div><div align="left"><font color="#000000" size="4">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;pinMode(TrigPin, OUTPUT); </font></div><div align="left"><font color="#000000" size="4">&nbsp; &nbsp; // 要检测引脚上输入的脉冲宽度,需要先设置为输入状态</font></div><div align="left"><font color="#000000" size="4">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;pinMode(EchoPin, INPUT); </font></div><div align="left"><font color="#000000" size="4">&nbsp; &nbsp; Serial.println("Ultrasonic sensor:");</font></div><div align="left"><font color="#000000" size="4">&nbsp; &nbsp;&nbsp;&nbsp;lcd.begin(16,2);</font></div><div align="left"><font color="#000000" size="4">&nbsp;&nbsp;lcd.clear(); //清屏</font></div><div align="left"><font color="#000000" size="4">&nbsp;&nbsp;delay(1000); //延时1000ms</font></div><div align="left"><font color="#000000" size="4">} </font></div><div align="left"><font color="#000000" size="4">void loop() </font></div><div align="left"><font color="#000000" size="4">{ </font></div><div align="left"><font color="#000000" size="4">&nbsp; &nbsp; // 产生一个10us的高脉冲去触发TrigPin </font></div><div align="left"><font color="#000000" size="4">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;digitalWrite(TrigPin, LOW); </font></div><div align="left"><font color="#000000" size="4">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;delayMicroseconds(2); </font></div><div align="left"><font color="#000000" size="4">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;digitalWrite(TrigPin, HIGH); </font></div><div align="left"><font color="#000000" size="4">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;delayMicroseconds(10);</font></div><div align="left"><font color="#000000" size="4">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;digitalWrite(TrigPin, LOW); </font></div><div align="left"><font color="#000000" size="4">&nbsp; &nbsp; // 检测脉冲宽度,并计算出距离</font></div><div align="left"><font color="#000000" size="4">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;distance = pulseIn(EchoPin, HIGH) / 58.00;</font></div><div align="left"><font color="#000000" size="4">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Serial.print(distance); </font></div><div align="left"><font color="#000000" size="4">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Serial.print("cm"); </font></div><div align="left"><font color="#000000" size="4">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Serial.println(); </font></div><div align="left"><font color="#000000" size="4">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;delay(1000); </font></div><div align="left"><font color="#000000" size="4">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;lcd.setCursor(0, 0) ;&nbsp; &nbsp;</font></div><div align="left"><font color="#000000" size="4">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; lcd.print(distance); </font></div><div align="left"><font color="#000000" size="4">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;lcd.print("cm"); </font></div><div align="left"><font color="#000000" size="4">&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;lcd.println(); </font></div><div align="left"><font color="#000000" size="4">&nbsp; &nbsp; </font></div><div align="left"><font color="#000000" size="4">&nbsp; &nbsp;&nbsp;&nbsp;lcd.setCursor(0, 1) ;</font></div><div align="left"><font color="#000000" size="4">&nbsp; &nbsp;&nbsp; &nbsp;lcd.println(); </font></div><div align="left"><font size="4">&nbsp; &nbsp; &nbsp; &nbsp; <font color="#000000">delay(500);&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; </font></font></div><div align="left"><font color="#000000" size="4">}</font></div><font size="4"><br><br><br><br><br><br></font><br>

白先森 发表于 2017-6-20 14:19:33

对LCD不是很了解,那个7,8,9,10,11,12引脚怎么接?求大神指点

krwlng 发表于 2017-7-28 09:56:57

在压缩包里有介绍的

krwlng 发表于 2017-7-28 09:57:50

LCD

20060606 发表于 2020-8-13 05:02:03

您用的是什么lcd
页: [1]
查看完整版本: 超声波测距仪