超声波测距仪
<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 <LiquidCrystal.h></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">{ // 初始化串口通信及连接SR04的引脚</font></div><div align="left"><font color="#000000" size="4"> Serial.begin(9600); </font></div><div align="left"><font color="#000000" size="4"> pinMode(TrigPin, OUTPUT); </font></div><div align="left"><font color="#000000" size="4"> // 要检测引脚上输入的脉冲宽度,需要先设置为输入状态</font></div><div align="left"><font color="#000000" size="4"> pinMode(EchoPin, INPUT); </font></div><div align="left"><font color="#000000" size="4"> Serial.println("Ultrasonic sensor:");</font></div><div align="left"><font color="#000000" size="4"> lcd.begin(16,2);</font></div><div align="left"><font color="#000000" size="4"> lcd.clear(); //清屏</font></div><div align="left"><font color="#000000" size="4"> 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"> // 产生一个10us的高脉冲去触发TrigPin </font></div><div align="left"><font color="#000000" size="4"> digitalWrite(TrigPin, LOW); </font></div><div align="left"><font color="#000000" size="4"> delayMicroseconds(2); </font></div><div align="left"><font color="#000000" size="4"> digitalWrite(TrigPin, HIGH); </font></div><div align="left"><font color="#000000" size="4"> delayMicroseconds(10);</font></div><div align="left"><font color="#000000" size="4"> digitalWrite(TrigPin, LOW); </font></div><div align="left"><font color="#000000" size="4"> // 检测脉冲宽度,并计算出距离</font></div><div align="left"><font color="#000000" size="4"> distance = pulseIn(EchoPin, HIGH) / 58.00;</font></div><div align="left"><font color="#000000" size="4"> Serial.print(distance); </font></div><div align="left"><font color="#000000" size="4"> Serial.print("cm"); </font></div><div align="left"><font color="#000000" size="4"> Serial.println(); </font></div><div align="left"><font color="#000000" size="4"> delay(1000); </font></div><div align="left"><font color="#000000" size="4"> lcd.setCursor(0, 0) ; </font></div><div align="left"><font color="#000000" size="4"> lcd.print(distance); </font></div><div align="left"><font color="#000000" size="4"> lcd.print("cm"); </font></div><div align="left"><font color="#000000" size="4"> lcd.println(); </font></div><div align="left"><font color="#000000" size="4"> </font></div><div align="left"><font color="#000000" size="4"> lcd.setCursor(0, 1) ;</font></div><div align="left"><font color="#000000" size="4"> lcd.println(); </font></div><div align="left"><font size="4"> <font color="#000000">delay(500); </font></font></div><div align="left"><font color="#000000" size="4">}</font></div><font size="4"><br><br><br><br><br><br></font><br> 对LCD不是很了解,那个7,8,9,10,11,12引脚怎么接?求大神指点 在压缩包里有介绍的 LCD 您用的是什么lcd
页:
[1]