標題: 用arduino實現超聲波測距儀 [打印本頁]
作者: yzy_km 時間: 2025-11-1 00:15
標題: 用arduino實現超聲波測距儀
本帖最后由 yzy_km 于 2025-11-1 00:19 編輯
arduino板子,LCD1602,超聲波模塊
程序如下:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //接LCD1602
const int TrigPin = 8;//接超聲波模塊
const int EchoPin = 9;//接超聲波模塊
float cm;
void setup()
{
Serial.begin(9600);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
lcd.begin(16, 2); //設置LCD顯
lcd.print("hello,world!"); //將hello,world!顯示在LCD上
}
void loop()
{
digitalWrite(TrigPin, LOW); //
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
cm = pulseIn(EchoPin, HIGH) / 58.0; //將回波時間換算成cm
cm = (int(cm * 100.0)) / 100.0; //保留兩位小數
Serial.print("Distance:");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(1000);
lcd.setCursor(0, 1);
lcd.print("Dis:");
lcd.setCursor(5, 1);
lcd.print(cm);
lcd.setCursor(11, 1);
lcd.print("cm");
}
| 歡迎光臨 (http://www.raoushi.com/bbs/) |
Powered by Discuz! X3.1 |