Library to use the ultrasonic sensor
Dependents: test_ultrasonic AEB Car_Simulator
Ultrasonic.cpp
- Committer:
- AndreaAndreoli
- Date:
- 2016-06-02
- Revision:
- 0:72ec72845f71
- Child:
- 1:b3518845e71a
File content as of revision 0:72ec72845f71:
#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; /* * 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(); } void trig() { trigger = 1; wait_us(10); trigger = 0; } void Ultrasonic_init() { timer.reset(); echo.rise(&start); echo.fall(&stop); tick.attach(&trig,0.06); }