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