Class library for a HC-SR04 Distance Sensor based on PwmOut (Trig) and InterruptIn (Echo).

Dependents:   ultra TDPS-COM1 HCSR04

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HCSR04.h Source File

HCSR04.h

00001 /* HCSR04 Library v1.0
00002  * Copyright (c) 2016 Grant Phillips
00003  * grant.phillips@nmmu.ac.za
00004  *
00005  *
00006  * Permission is hereby granted, free of charge, to any person obtaining a copy
00007  * of this software and associated documentation files (the "Software"), to deal
00008  * in the Software without restriction, including without limitation the rights
00009  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00010  * copies of the Software, and to permit persons to whom the Software is
00011  * furnished to do so, subject to the following conditions:
00012  *
00013  * The above copyright notice and this permission notice shall be included in
00014  * all copies or substantial portions of the Software.
00015  *
00016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00019  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00020  * 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
00022  * THE SOFTWARE.
00023  */
00024  
00025 #ifndef HCSR04_H
00026 #define HCSR04_H
00027  
00028 #include "mbed.h"
00029 
00030 /** Class library for a HC-SR04 Distance Sensor based on PwmOut (Trig) and InterruptIn (Echo).
00031  *
00032  * Example:
00033  * @code
00034  * #include "mbed.h"
00035  * #include "HCSR04.h"
00036  *
00037  * HCSR04 distance(PB_8, PA_1);
00038  *
00039  * int main() {
00040  *     while(1){
00041  *         SWO.printf("Distance = %d (us)   %f (cm)\n", distance.read_us(), distance.read_cm()); 
00042  *     }
00043  * }
00044  * @endcode
00045  */
00046  
00047 class HCSR04 {
00048   public:
00049     /** Create a HCSR04 object connected to the specified pins. Once created, the PWM signal
00050     * on the Trig pin will start immediately and measurements will start in the background.
00051     * @param TrigPin PwmOut compatible pin used to connect to HC-SR04's Trig pin
00052     * @param EchoPin InterruptIn compatible pin used to connect to HC-SR04's Echo pin
00053     */
00054     HCSR04(PinName TrigPin, PinName EchoPin);
00055     
00056     /** Return the current pulse duration as microseconds (us).
00057     * @param 
00058     *     None
00059     * @return 
00060     *     Duration as microseconds.
00061     */
00062     unsigned int read_us();
00063     
00064     /** Return the current pulse duration as centimeters (cm).
00065     * @param 
00066     *     None
00067     * @return
00068     *     Duration as centimeters.
00069     */
00070     float read_cm();
00071  
00072   private:
00073     PwmOut trig;
00074     InterruptIn echo;
00075     Timer timer;
00076     long us;
00077     void HighTrigger();
00078     void LowTrigger();
00079 };
00080  
00081 #endif