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);
}
}