Dependencies:   mbed

Committer:
mshoemaker
Date:
Sun Jan 03 13:11:03 2010 +0000
Revision:
0:504353f35956

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mshoemaker 0:504353f35956 1 #include "mbed.h"
mshoemaker 0:504353f35956 2
mshoemaker 0:504353f35956 3 Serial pc(USBTX,USBRX);
mshoemaker 0:504353f35956 4
mshoemaker 0:504353f35956 5 DigitalInOut pingPin(p18);
mshoemaker 0:504353f35956 6
mshoemaker 0:504353f35956 7 Timer tmr;
mshoemaker 0:504353f35956 8
mshoemaker 0:504353f35956 9 long microsecondsToInches(long microseconds);
mshoemaker 0:504353f35956 10 long microsecondsToCentimeters(long microseconds);
mshoemaker 0:504353f35956 11
mshoemaker 0:504353f35956 12 int main() {
mshoemaker 0:504353f35956 13 while (1) {
mshoemaker 0:504353f35956 14 // establish variables for duration of the ping,
mshoemaker 0:504353f35956 15 // and the distance result in inches and centimeters:
mshoemaker 0:504353f35956 16 long duration, inches, cm;
mshoemaker 0:504353f35956 17
mshoemaker 0:504353f35956 18 // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
mshoemaker 0:504353f35956 19 // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
mshoemaker 0:504353f35956 20 pingPin.output();
mshoemaker 0:504353f35956 21 pingPin = 0;
mshoemaker 0:504353f35956 22 wait_us(2);
mshoemaker 0:504353f35956 23 pingPin = 1;
mshoemaker 0:504353f35956 24 wait_us(5);
mshoemaker 0:504353f35956 25 pingPin = 0;
mshoemaker 0:504353f35956 26
mshoemaker 0:504353f35956 27 // The same pin is used to read the signal from the PING))): a HIGH
mshoemaker 0:504353f35956 28 // pulse whose duration is the time (in microseconds) from the sending
mshoemaker 0:504353f35956 29 // of the ping to the reception of its echo off of an object.
mshoemaker 0:504353f35956 30 pingPin.input();
mshoemaker 0:504353f35956 31
mshoemaker 0:504353f35956 32 // pulseIn
mshoemaker 0:504353f35956 33 while (!pingPin); // wait for high
mshoemaker 0:504353f35956 34 tmr.start();
mshoemaker 0:504353f35956 35 while (pingPin); // wait for low
mshoemaker 0:504353f35956 36 duration = tmr.read_us();
mshoemaker 0:504353f35956 37
mshoemaker 0:504353f35956 38 // convert the time into a distance
mshoemaker 0:504353f35956 39 inches = microsecondsToInches(duration);
mshoemaker 0:504353f35956 40 cm = microsecondsToCentimeters(duration);
mshoemaker 0:504353f35956 41
mshoemaker 0:504353f35956 42 pc.printf("in=%4d, cm=%4d\n", inches, cm);
mshoemaker 0:504353f35956 43 wait_ms(100);
mshoemaker 0:504353f35956 44 }
mshoemaker 0:504353f35956 45 }
mshoemaker 0:504353f35956 46
mshoemaker 0:504353f35956 47 long microsecondsToInches(long microseconds) {
mshoemaker 0:504353f35956 48 // According to Parallax's datasheet for the PING))), there are
mshoemaker 0:504353f35956 49 // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
mshoemaker 0:504353f35956 50 // second). This gives the distance travelled by the ping, outbound
mshoemaker 0:504353f35956 51 // and return, so we divide by 2 to get the distance of the obstacle.
mshoemaker 0:504353f35956 52 // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
mshoemaker 0:504353f35956 53 return microseconds / 74 / 2;
mshoemaker 0:504353f35956 54 }
mshoemaker 0:504353f35956 55
mshoemaker 0:504353f35956 56 long microsecondsToCentimeters(long microseconds) {
mshoemaker 0:504353f35956 57 // The speed of sound is 340 m/s or 29 microseconds per centimeter.
mshoemaker 0:504353f35956 58 // The ping travels out and back, so to find the distance of the
mshoemaker 0:504353f35956 59 // object we take half of the distance travelled.
mshoemaker 0:504353f35956 60 return microseconds / 29 / 2;
mshoemaker 0:504353f35956 61 }