Library to use the ultrasonic sensor
Dependents: test_ultrasonic AEB Car_Simulator
Diff: Ultrasonic.cpp
- Revision:
- 0:72ec72845f71
- Child:
- 1:b3518845e71a
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Ultrasonic.cpp Thu Jun 02 19:56:38 2016 +0000 @@ -0,0 +1,47 @@ +#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); +} \ No newline at end of file