Ultrasound Ranging Sensor module

Dependents:   Obstacle_avoidance servourfmatlab hcsr04 DistanceOnSevenSegLed ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers hcsr04.h Source File

hcsr04.h

00001 /* Copyright (c) 2013 Prabhu Desai
00002  * pdtechworld@gmail.com
00003  *
00004  *
00005  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00006  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00007  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00008  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in all copies or
00012  * substantial portions of the Software.
00013  *
00014  * For more details on the sensor :
00015  * http://www.elecfreaks.com/store/hcsr04-ultrasonic-sensor-distance-measuring-module-p-91.html?zenid=pgm8pgnvaodbe36dibq5s1soi3
00016  *
00017  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00018  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00019  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00020  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00022  */
00023 
00024 #ifndef MBED_HCSR04_H
00025 #define MBED_HCSR04_H
00026 
00027 #include "mbed.h"
00028 
00029 /** HCSR04 Class(es)
00030  */
00031 
00032 class HCSR04
00033 {
00034 public:
00035     /** Create a HCSR04 object connected to the specified pin
00036     * @param pin i/o pin to connect to
00037     */
00038     HCSR04(PinName TrigPin,PinName EchoPin);
00039     ~HCSR04();
00040 
00041     /** Return the distance from obstacle in cm
00042     * @param distance in cms and returns -1, in case of failure
00043     */
00044     unsigned int get_dist_cm(void);
00045     /** Return the pulse duration equal to sonic waves travelling to obstacle and back to receiver.
00046     * @param pulse duration in microseconds.
00047     */
00048     unsigned int get_pulse_us(void);
00049     /** Generates the trigger pulse of 10us on the trigger PIN.
00050     */
00051     void start(void );
00052     void isr_rise(void);
00053     void isr_fall(void);
00054     void fall (void (*fptr)(void));
00055     void rise (void (*fptr)(void));
00056 
00057 
00058 
00059 private:
00060 
00061     Timer pulsetime;
00062     DigitalOut  trigger;
00063     InterruptIn echo;
00064     unsigned int pulsedur;
00065     unsigned int distance;
00066 };
00067 
00068 #endif