Library to use the ultrasonic sensor

Dependents:   test_ultrasonic AEB Car_Simulator

Committer:
AndreaAndreoli
Date:
Sun Jun 05 14:29:33 2016 +0000
Revision:
4:8c97476a5ebf
Parent:
3:9b06e5793b8b
fixed class bug (everything is working now !!!!!!)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AndreaAndreoli 0:72ec72845f71 1 #ifndef ULTRASONIC_H
AndreaAndreoli 0:72ec72845f71 2 #define ULTRASONIC_H
AndreaAndreoli 0:72ec72845f71 3
AndreaAndreoli 0:72ec72845f71 4 #include "mbed.h"
AndreaAndreoli 0:72ec72845f71 5
AndreaAndreoli 4:8c97476a5ebf 6 /** Ultrasonic Class
AndreaAndreoli 4:8c97476a5ebf 7 * This class is intended to be used with a
AndreaAndreoli 4:8c97476a5ebf 8 * ultrasonic sensor HC-SR04.
AndreaAndreoli 4:8c97476a5ebf 9 * Once initialized the Ultrasonic object just call the read function to
AndreaAndreoli 4:8c97476a5ebf 10 * get the distance from object.
AndreaAndreoli 4:8c97476a5ebf 11 */
AndreaAndreoli 3:9b06e5793b8b 12 class Ultrasonic
AndreaAndreoli 3:9b06e5793b8b 13 {
AndreaAndreoli 3:9b06e5793b8b 14 public :
AndreaAndreoli 4:8c97476a5ebf 15 /** Constructor
AndreaAndreoli 4:8c97476a5ebf 16 *
AndreaAndreoli 4:8c97476a5ebf 17 * @note This is used to initialize the ultrasonic object corresponding to one sensor,
AndreaAndreoli 4:8c97476a5ebf 18 * just pass as input the pin where the sensor is connected (they must be digital pins).
AndreaAndreoli 4:8c97476a5ebf 19 *
AndreaAndreoli 4:8c97476a5ebf 20 * @param trigger the trigger pin name
AndreaAndreoli 4:8c97476a5ebf 21 * @param echo the echo pin name
AndreaAndreoli 4:8c97476a5ebf 22 *
AndreaAndreoli 4:8c97476a5ebf 23 */
AndreaAndreoli 3:9b06e5793b8b 24 Ultrasonic(PinName trigger, PinName echo);
AndreaAndreoli 4:8c97476a5ebf 25
AndreaAndreoli 4:8c97476a5ebf 26 /** Function to read the distance in cm
AndreaAndreoli 4:8c97476a5ebf 27 *
AndreaAndreoli 4:8c97476a5ebf 28 * @note the distance is read internally every 60 ms so invoke this function
AndreaAndreoli 4:8c97476a5ebf 29 * not less than every 60 ms
AndreaAndreoli 4:8c97476a5ebf 30 * @return
AndreaAndreoli 4:8c97476a5ebf 31 * 0 if timeout(50ms) elapsed (error or out of range measure),
AndreaAndreoli 4:8c97476a5ebf 32 * 1-400 distance from object in cm
AndreaAndreoli 4:8c97476a5ebf 33 *
AndreaAndreoli 4:8c97476a5ebf 34 */
AndreaAndreoli 3:9b06e5793b8b 35 int read_cm();
AndreaAndreoli 0:72ec72845f71 36
AndreaAndreoli 3:9b06e5793b8b 37 private :
AndreaAndreoli 3:9b06e5793b8b 38 DigitalOut _trigger;
AndreaAndreoli 3:9b06e5793b8b 39 InterruptIn _echo;
AndreaAndreoli 3:9b06e5793b8b 40 Timer timer;
AndreaAndreoli 3:9b06e5793b8b 41 Ticker tick;
AndreaAndreoli 3:9b06e5793b8b 42 Timeout timeout;
AndreaAndreoli 3:9b06e5793b8b 43 int DistanceCM;
AndreaAndreoli 3:9b06e5793b8b 44
AndreaAndreoli 3:9b06e5793b8b 45 void start();
AndreaAndreoli 3:9b06e5793b8b 46 void stop();
AndreaAndreoli 3:9b06e5793b8b 47 void trig();
AndreaAndreoli 3:9b06e5793b8b 48 void Ultrasonic_init();
AndreaAndreoli 3:9b06e5793b8b 49 void timeout_err();
AndreaAndreoli 3:9b06e5793b8b 50 };
AndreaAndreoli 0:72ec72845f71 51
AndreaAndreoli 0:72ec72845f71 52 #endif