Library to use the ultrasonic sensor
Dependents: test_ultrasonic AEB Car_Simulator
Ultrasonic.cpp
- Committer:
- AndreaAndreoli
- Date:
- 2016-06-05
- Revision:
- 3:9b06e5793b8b
- Parent:
- 2:12da800fd10a
- Child:
- 4:8c97476a5ebf
File content as of revision 3:9b06e5793b8b:
#include "mbed.h" #include "Ultrasonic.h" Ultrasonic::Ultrasonic(PinName trigger, PinName echo): _trigger(trigger) , _echo(echo) { //_trigger = trigger; //_echo = echo; Ultrasonic_init(); } /* * Call this function and it will return the distance in centimeter */ int Ultrasonic::read_cm() { return DistanceCM; } void Ultrasonic::start() { timer.start(); } void Ultrasonic::stop() { DistanceCM = timer.read_us()/58; timer.stop(); //pc.printf("Distance: %f \n", DistanceCM); timer.reset(); timeout.detach(); } void Ultrasonic::trig() { timeout.attach(this, this.timeout_err, 0.05); _trigger = 1; wait_us(10); _trigger = 0; } void Ultrasonic::Ultrasonic_init() { DistanceCM = 0; timer.reset(); _echo.rise(&Ultrasonic::start); _echo.fall(&stop); tick.attach(&trig,0.06); } void Ultrasonic::timeout_err() { // Timeout elapsed -> there is some problem // out the distance to zero to highligth error DistanceCM = 0; }