$search
00001 /* Functions for various sensor types */ 00002 00003 float microsecondsToCm(long microseconds) 00004 { 00005 // The speed of sound is 340 m/s or 29 microseconds per cm. 00006 // The ping travels out and back, so to find the distance of the 00007 // object we take half of the distance travelled. 00008 return microseconds / 29 / 2; 00009 } 00010 00011 long Ping(int pin) { 00012 long duration, range; 00013 00014 // The PING))) is triggered by a HIGH pulse of 2 or more microseconds. 00015 // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: 00016 pinMode(pin, OUTPUT); 00017 digitalWrite(pin, LOW); 00018 delayMicroseconds(2); 00019 digitalWrite(pin, HIGH); 00020 delayMicroseconds(5); 00021 digitalWrite(pin, LOW); 00022 00023 // The same pin is used to read the signal from the PING))): a HIGH 00024 // pulse whose duration is the time (in microseconds) from the sending 00025 // of the ping to the reception of its echo off of an object. 00026 pinMode(pin, INPUT); 00027 duration = pulseIn(pin, HIGH); 00028 00029 // convert the time into meters 00030 range = microsecondsToCm(duration); 00031 00032 return(range); 00033 } 00034