2017-01-06 117 views
0

我正在修補一個簡單的Arduino草圖,使用超聲波傳感器檢測距離。從我所瞭解的情況來看,觸發器發送了一個ping。回聲監聽回來的回聲以計算距離。這是以微秒爲單位測量的。使用超聲波傳感器在Arduino中獲得英寸距離和Cm從超聲波傳感器獲取距離

如果這是正確的,問題是,如何確定英寸和釐米從平(這是微秒)?這只是基本的數學/物理學,並且這只是一個基本的公式嗎?

void loop() { 
digitalWrite(trigPin, LOW); 
delayMicroseconds(2); 
digitalWrite(trigPin, HIGH); 
delayMicroseconds(10); 
digitalWrite(trigPin, LOW); 

duration = pulseIn(echoPin, HIGH); 

這是我遇到的問題。我想鴻溝微秒至英寸的ping時間,然後釐米:

distance_in = duration ????; 
distance_cm = duration ????; 

Serial.print(distance_in); 
Serial.print(" in, "); 
Serial.print(distance_cm); 
Serial.print(" cm"); 
Serial.println(); 
delay(1500); 
} 

任何幫助,以儘可能多的解釋,將不勝感激。我是一個新手,Arduino的(而不是重升降數學)

回答

0
distance_cm = duration/29/2; 
distance_in = distance_cm * 0,393701;  

聲音傳播以每秒343米,這意味着它需要每釐米29.155微秒。因此,我們必須將持續時間除以29,然後減去2,因爲聲音必須兩次行進。它傳播到物體,然後回到傳感器。

1釐米= 0,393701中

+0

這很有道理。謝謝! –

1

我已經在這個問題上找到的最好的文章其實是在Arduino的。 https://www.arduino.cc/en/Tutorial/Ping

/* Ping))) Sensor 

    This sketch reads a PING))) ultrasonic rangefinder and returns the 
    distance to the closest object in range. To do this, it sends a pulse 
    to the sensor to initiate a reading, then listens for a pulse 
    to return. The length of the returning pulse is proportional to 
    the distance of the object from the sensor. 

    The circuit: 
    * +V connection of the PING))) attached to +5V 
    * GND connection of the PING))) attached to ground 
    * SIG connection of the PING))) attached to digital pin 7 

    http://www.arduino.cc/en/Tutorial/Ping 

    created 3 Nov 2008 
    by David A. Mellis 
    modified 30 Aug 2011 
    by Tom Igoe 

    This example code is in the public domain. 

*/ 

// this constant won't change. It's the pin number 
// of the sensor's output: 
const int pingPin = 7; 

void setup() { 
    // initialize serial communication: 
    Serial.begin(9600); 
} 

void loop() { 
    // establish variables for duration of the ping, 
    // and the distance result in inches and centimeters: 
    long duration, inches, cm; 

    // The PING))) is triggered by a HIGH pulse of 2 or more microseconds. 
    // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: 
    pinMode(pingPin, OUTPUT); 
    digitalWrite(pingPin, LOW); 
    delayMicroseconds(2); 
    digitalWrite(pingPin, HIGH); 
    delayMicroseconds(5); 
    digitalWrite(pingPin, LOW); 

    // The same pin is used to read the signal from the PING))): a HIGH 
    // pulse whose duration is the time (in microseconds) from the sending 
    // of the ping to the reception of its echo off of an object. 
    pinMode(pingPin, INPUT); 
    duration = pulseIn(pingPin, HIGH); 

    // convert the time into a distance 
    inches = microsecondsToInches(duration); 
    cm = microsecondsToCentimeters(duration); 

    Serial.print(inches); 
    Serial.print("in, "); 
    Serial.print(cm); 
    Serial.print("cm"); 
    Serial.println(); 

    delay(100); 
} 

long microsecondsToInches(long microseconds) { 
    // According to Parallax's datasheet for the PING))), there are 
    // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per 
    // second). This gives the distance travelled by the ping, outbound 
    // and return, so we divide by 2 to get the distance of the obstacle. 
    // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf 
    return microseconds/74/2; 
} 

long microsecondsToCentimeters(long microseconds) { 
    // The speed of sound is 340 m/s or 29 microseconds per centimeter. 
    // The ping travels out and back, so to find the distance of the 
    // object we take half of the distance travelled. 
    return microseconds/29/2; 
} 
相關問題