Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
PING.cpp
00001 #include "PING.h" 00002 #include "mbed.h" 00003 00004 /* 00005 * Constructor 00006 */ 00007 PING::PING(PinName trigger) : _trigger(trigger) 00008 { 00009 // Attach interrupts 00010 _ticker.attach(this, &PING::_startRange, 0.1); 00011 } 00012 00013 void PING::_startRange() 00014 { 00015 // send a trigger pulse, 20uS long 00016 _trigger.output(); // set pin to output trigger pulse 00017 _trigger = 0; // ensure pin starts low 00018 wait_us(2); // wait two microseconds 00019 _trigger = 1; // send trigger pulse by setting pin high 00020 wait_us(5); // keep sending pulse for 5 microseconds 00021 _trigger = 0; // set pin low to stop pulse 00022 00023 _trigger.input(); 00024 00025 // pulseIn 00026 while (!_trigger); // wait for high 00027 _timer.reset(); 00028 _timer.start(); 00029 while (_trigger); // wait for low 00030 _timer.stop(); 00031 _dist = _timer.read_us(); //provides echo time in microseconds 00032 } 00033 00034 // returns distance in meters 00035 // The speed of sound is 340 m/s or 58 microseconds per meter. 00036 // The ping travels out and back, so to find the distance of the 00037 // object we take half of the distance travelled. 00038 float PING::read(void) 00039 { 00040 // spin until there is a good value 00041 return (_dist/29/2); 00042 } 00043 00044 // returns distance in inches 00045 // According to Parallax's datasheet for the PING))), there are 00046 // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per 00047 // second). This gives the distance travelled by the ping, outbound 00048 // and return, so we divide by 2 to get the distance of the obstacle. 00049 // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf 00050 float PING::read_in(void) 00051 { 00052 // spin until there is a good value 00053 return (_dist/74/2); 00054 } 00055 00056 PING::operator float() 00057 { 00058 return read(); 00059 }
Generated on Fri Jul 15 2022 02:34:19 by
1.7.2