栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Arduino 超声波实验程序

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Arduino 超声波实验程序


1.首先要求测算声音的速度。可以根据温度进行计算,这样可以更精确。
例如:室内温度为28摄氏度。
声速为:331+28*0.6=347.8 m/s.
2.将声音速度转换成每微秒多少厘米。
34780 cm/S == 34.78cm /毫秒 == 0.03478 cm/微秒 == 28.752微秒/cm==287.52微秒/10cm
3.声音往返距离 28.752微秒 * 2 / cm =57.504 微秒/cm ~58 微秒/cm
.

--------------------------------------程序--------------------------------------

/超声探测实验/
const int TrigPin=3;
const int EchoPin=2;
const int buzzerPin=8;
int distance;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(TrigPin,OUTPUT);
pinMode(EchoPin,INPUT);
pinMode(buzzerPin,OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(TrigPin,LOW);
delayMicroseconds(2);
digitalWrite(TrigPin,HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin,LOW);

distance=pulseIn(EchoPin,HIGH)/57.504;
Serial.print(distance);
Serial.println(“cm”);
if(distance<=2){
alarm();
}
delay(100);
}
void alarm(){
for(int i=1;i<=3;i++){
digitalWrite(buzzerPin,HIGH);
delay(1000);
digitalWrite(buzzerPin,LOW);
delay(1000);
}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/589229.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号