Library to use the ultrasonic sensor
Dependents: test_ultrasonic AEB Car_Simulator
Diff: Ultrasonic.h
- Revision:
- 4:8c97476a5ebf
- Parent:
- 3:9b06e5793b8b
--- a/Ultrasonic.h Sun Jun 05 12:23:48 2016 +0000 +++ b/Ultrasonic.h Sun Jun 05 14:29:33 2016 +0000 @@ -3,13 +3,35 @@ #include "mbed.h" -//#define TRIGGER D2 // The trigger pin -//#define ECHO D4 // The echo pin - +/** Ultrasonic Class +* This class is intended to be used with a +* ultrasonic sensor HC-SR04. +* Once initialized the Ultrasonic object just call the read function to +* get the distance from object. +*/ class Ultrasonic { public : + /** Constructor + * + * @note This is used to initialize the ultrasonic object corresponding to one sensor, + * just pass as input the pin where the sensor is connected (they must be digital pins). + * + * @param trigger the trigger pin name + * @param echo the echo pin name + * + */ Ultrasonic(PinName trigger, PinName echo); + + /** Function to read the distance in cm + * + * @note the distance is read internally every 60 ms so invoke this function + * not less than every 60 ms + * @return + * 0 if timeout(50ms) elapsed (error or out of range measure), + * 1-400 distance from object in cm + * + */ int read_cm(); private : @@ -25,10 +47,6 @@ void trig(); void Ultrasonic_init(); void timeout_err(); - -//DigitalOut trigger(TRIGGER); -//InterruptIn echo(ECHO); // Attach interrupt to the echo pin - }; #endif