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

Dependents:   ultra TDPS-COM1 HCSR04

Committer:
grantphillips
Date:
Mon Feb 08 14:43:42 2016 +0000
Revision:
0:5541303b14e7
Child:
1:8286d0de19ce
v1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
grantphillips 0:5541303b14e7 1 /* HCSR04 Library v1.0
grantphillips 0:5541303b14e7 2 * Copyright (c) 2016 Grant Phillips
grantphillips 0:5541303b14e7 3 * grant.phillips@nmmu.ac.za
grantphillips 0:5541303b14e7 4 *
grantphillips 0:5541303b14e7 5 *
grantphillips 0:5541303b14e7 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
grantphillips 0:5541303b14e7 7 * of this software and associated documentation files (the "Software"), to deal
grantphillips 0:5541303b14e7 8 * in the Software without restriction, including without limitation the rights
grantphillips 0:5541303b14e7 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
grantphillips 0:5541303b14e7 10 * copies of the Software, and to permit persons to whom the Software is
grantphillips 0:5541303b14e7 11 * furnished to do so, subject to the following conditions:
grantphillips 0:5541303b14e7 12 *
grantphillips 0:5541303b14e7 13 * The above copyright notice and this permission notice shall be included in
grantphillips 0:5541303b14e7 14 * all copies or substantial portions of the Software.
grantphillips 0:5541303b14e7 15 *
grantphillips 0:5541303b14e7 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
grantphillips 0:5541303b14e7 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
grantphillips 0:5541303b14e7 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
grantphillips 0:5541303b14e7 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
grantphillips 0:5541303b14e7 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
grantphillips 0:5541303b14e7 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
grantphillips 0:5541303b14e7 22 * THE SOFTWARE.
grantphillips 0:5541303b14e7 23 */
grantphillips 0:5541303b14e7 24
grantphillips 0:5541303b14e7 25 #ifndef HCSR04_H
grantphillips 0:5541303b14e7 26 #define HCSR04_H
grantphillips 0:5541303b14e7 27
grantphillips 0:5541303b14e7 28 #include "mbed.h"
grantphillips 0:5541303b14e7 29
grantphillips 0:5541303b14e7 30 /** HC-SR04 Distance Sensor class based on PwmOut (Trig) and InterruptIn (Echo).
grantphillips 0:5541303b14e7 31 *
grantphillips 0:5541303b14e7 32 * Example:
grantphillips 0:5541303b14e7 33 * @code
grantphillips 0:5541303b14e7 34 * #include "mbed.h"
grantphillips 0:5541303b14e7 35 * #include "HCSR04.h"
grantphillips 0:5541303b14e7 36 *
grantphillips 0:5541303b14e7 37 * HCSR04 distance(PB_8, PA_1);
grantphillips 0:5541303b14e7 38 *
grantphillips 0:5541303b14e7 39 * int main() {
grantphillips 0:5541303b14e7 40 * while(1){
grantphillips 0:5541303b14e7 41 * SWO.printf("Distance = %d (us) %f (cm)\n", distance.read_us(), distance.read_cm());
grantphillips 0:5541303b14e7 42 * }
grantphillips 0:5541303b14e7 43 * }
grantphillips 0:5541303b14e7 44 * @endcode
grantphillips 0:5541303b14e7 45 */
grantphillips 0:5541303b14e7 46
grantphillips 0:5541303b14e7 47 class HCSR04 {
grantphillips 0:5541303b14e7 48 public:
grantphillips 0:5541303b14e7 49 /** Create a HCSR04 object connected to the specified pins. Once created, the PWM signal
grantphillips 0:5541303b14e7 50 * on the Trig pin will start immediately and measurements will start in the background.
grantphillips 0:5541303b14e7 51 * @param TrigPin PwmOut compatible pin used to connect to HC-SR04's Trig pin
grantphillips 0:5541303b14e7 52 * @param EchoPin InterruptIn compatible pin used to connect to HC-SR04's Echo pin
grantphillips 0:5541303b14e7 53 */
grantphillips 0:5541303b14e7 54 HCSR04(PinName TrigPin, PinName EchoPin);
grantphillips 0:5541303b14e7 55
grantphillips 0:5541303b14e7 56 /** Return the current pulse duration as microseconds (us).
grantphillips 0:5541303b14e7 57 * @param
grantphillips 0:5541303b14e7 58 * None
grantphillips 0:5541303b14e7 59 * @return
grantphillips 0:5541303b14e7 60 * Duration as microseconds.
grantphillips 0:5541303b14e7 61 */
grantphillips 0:5541303b14e7 62 unsigned int read_us();
grantphillips 0:5541303b14e7 63
grantphillips 0:5541303b14e7 64 /** Return the current pulse duration as centimeters (cm).
grantphillips 0:5541303b14e7 65 * @param
grantphillips 0:5541303b14e7 66 * None
grantphillips 0:5541303b14e7 67 * @return
grantphillips 0:5541303b14e7 68 * Duration as centimeters.
grantphillips 0:5541303b14e7 69 */
grantphillips 0:5541303b14e7 70 float read_cm();
grantphillips 0:5541303b14e7 71
grantphillips 0:5541303b14e7 72 private:
grantphillips 0:5541303b14e7 73 PwmOut trig;
grantphillips 0:5541303b14e7 74 InterruptIn echo;
grantphillips 0:5541303b14e7 75 Timer timer;
grantphillips 0:5541303b14e7 76 long us;
grantphillips 0:5541303b14e7 77 void HighTrigger();
grantphillips 0:5541303b14e7 78 void LowTrigger();
grantphillips 0:5541303b14e7 79 };
grantphillips 0:5541303b14e7 80
grantphillips 0:5541303b14e7 81 #endif