Uso de Sensor de Ultrasonidos

UltraSonidos.cpp

Committer:
cr0n0s20
Date:
2011-02-24
Revision:
0:3b305cf58a40
Child:
1:42d7fd2a719e

File content as of revision 0:3b305cf58a40:

#include "UltraSonidos.h"
#include "mbed.h"


UltraSonidos::UltraSonidos(PinName pin) : _pinDigital(pin) {
  
}

long UltraSonidos::read(){

long duration;

  _pinDigital.output();
  _pinDigital = 0;
  wait_ms(2);
  _pinDigital = 1;
  wait_ms(15);
  _pinDigital = 0;
  wait_ms(20);

  _pinDigital.input();
  duration = pulseIn(_pinDigital);

  // convert the time into a distance
  
  return microsegundosAcentimetros(duration);
  
  
  

}

int UltraSonidos::pulseIn(DigitalInOut& pingPin) {
    Timer tmr;
    while (!pingPin); // wait for high
    tmr.start();
    while (pingPin); // wait for low
    return tmr.read_ms();
}


long UltraSonidos::microsegundosAcentimetros(long microsegundos)
{
  // 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 microsegundos / 29 / 2;
}