library for ulrasonic sensonr
71da0dbf4400/hcsr04.h@0:3e33a89d3a9f, 2018-05-18 (annotated)
- Committer:
- Andres0131
- Date:
- Fri May 18 01:13:12 2018 +0000
- Revision:
- 0:3e33a89d3a9f
worked better this way
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Andres0131 | 0:3e33a89d3a9f | 1 | /* Copyright (c) 2013 Prabhu Desai |
Andres0131 | 0:3e33a89d3a9f | 2 | * pdtechworld@gmail.com |
Andres0131 | 0:3e33a89d3a9f | 3 | * |
Andres0131 | 0:3e33a89d3a9f | 4 | * |
Andres0131 | 0:3e33a89d3a9f | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
Andres0131 | 0:3e33a89d3a9f | 6 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
Andres0131 | 0:3e33a89d3a9f | 7 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
Andres0131 | 0:3e33a89d3a9f | 8 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
Andres0131 | 0:3e33a89d3a9f | 9 | * furnished to do so, subject to the following conditions: |
Andres0131 | 0:3e33a89d3a9f | 10 | * |
Andres0131 | 0:3e33a89d3a9f | 11 | * The above copyright notice and this permission notice shall be included in all copies or |
Andres0131 | 0:3e33a89d3a9f | 12 | * substantial portions of the Software. |
Andres0131 | 0:3e33a89d3a9f | 13 | * |
Andres0131 | 0:3e33a89d3a9f | 14 | * For more details on the sensor : |
Andres0131 | 0:3e33a89d3a9f | 15 | * http://www.elecfreaks.com/store/hcsr04-ultrasonic-sensor-distance-measuring-module-p-91.html?zenid=pgm8pgnvaodbe36dibq5s1soi3 |
Andres0131 | 0:3e33a89d3a9f | 16 | * |
Andres0131 | 0:3e33a89d3a9f | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
Andres0131 | 0:3e33a89d3a9f | 18 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
Andres0131 | 0:3e33a89d3a9f | 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
Andres0131 | 0:3e33a89d3a9f | 20 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Andres0131 | 0:3e33a89d3a9f | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Andres0131 | 0:3e33a89d3a9f | 22 | */ |
Andres0131 | 0:3e33a89d3a9f | 23 | |
Andres0131 | 0:3e33a89d3a9f | 24 | #ifndef MBED_HCSR04_H |
Andres0131 | 0:3e33a89d3a9f | 25 | #define MBED_HCSR04_H |
Andres0131 | 0:3e33a89d3a9f | 26 | |
Andres0131 | 0:3e33a89d3a9f | 27 | #include "mbed.h" |
Andres0131 | 0:3e33a89d3a9f | 28 | |
Andres0131 | 0:3e33a89d3a9f | 29 | /** HCSR04 Class(es) |
Andres0131 | 0:3e33a89d3a9f | 30 | */ |
Andres0131 | 0:3e33a89d3a9f | 31 | |
Andres0131 | 0:3e33a89d3a9f | 32 | class HCSR04 |
Andres0131 | 0:3e33a89d3a9f | 33 | { |
Andres0131 | 0:3e33a89d3a9f | 34 | public: |
Andres0131 | 0:3e33a89d3a9f | 35 | /** Create a HCSR04 object connected to the specified pin |
Andres0131 | 0:3e33a89d3a9f | 36 | * @param pin i/o pin to connect to |
Andres0131 | 0:3e33a89d3a9f | 37 | */ |
Andres0131 | 0:3e33a89d3a9f | 38 | HCSR04(PinName TrigPin,PinName EchoPin); |
Andres0131 | 0:3e33a89d3a9f | 39 | ~HCSR04(); |
Andres0131 | 0:3e33a89d3a9f | 40 | |
Andres0131 | 0:3e33a89d3a9f | 41 | /** Return the distance from obstacle in cm |
Andres0131 | 0:3e33a89d3a9f | 42 | * @param distance in cms and returns -1, in case of failure |
Andres0131 | 0:3e33a89d3a9f | 43 | */ |
Andres0131 | 0:3e33a89d3a9f | 44 | unsigned int get_dist_cm(void); |
Andres0131 | 0:3e33a89d3a9f | 45 | /** Return the pulse duration equal to sonic waves travelling to obstacle and back to receiver. |
Andres0131 | 0:3e33a89d3a9f | 46 | * @param pulse duration in microseconds. |
Andres0131 | 0:3e33a89d3a9f | 47 | */ |
Andres0131 | 0:3e33a89d3a9f | 48 | unsigned int get_pulse_us(void); |
Andres0131 | 0:3e33a89d3a9f | 49 | /** Generates the trigger pulse of 10us on the trigger PIN. |
Andres0131 | 0:3e33a89d3a9f | 50 | */ |
Andres0131 | 0:3e33a89d3a9f | 51 | void start(void ); |
Andres0131 | 0:3e33a89d3a9f | 52 | void isr_rise(void); |
Andres0131 | 0:3e33a89d3a9f | 53 | void isr_fall(void); |
Andres0131 | 0:3e33a89d3a9f | 54 | void fall (void (*fptr)(void)); |
Andres0131 | 0:3e33a89d3a9f | 55 | void rise (void (*fptr)(void)); |
Andres0131 | 0:3e33a89d3a9f | 56 | |
Andres0131 | 0:3e33a89d3a9f | 57 | |
Andres0131 | 0:3e33a89d3a9f | 58 | |
Andres0131 | 0:3e33a89d3a9f | 59 | private: |
Andres0131 | 0:3e33a89d3a9f | 60 | |
Andres0131 | 0:3e33a89d3a9f | 61 | Timer pulsetime; |
Andres0131 | 0:3e33a89d3a9f | 62 | DigitalOut trigger; |
Andres0131 | 0:3e33a89d3a9f | 63 | InterruptIn echo; |
Andres0131 | 0:3e33a89d3a9f | 64 | unsigned int pulsedur; |
Andres0131 | 0:3e33a89d3a9f | 65 | unsigned int distance; |
Andres0131 | 0:3e33a89d3a9f | 66 | }; |
Andres0131 | 0:3e33a89d3a9f | 67 | |
Andres0131 | 0:3e33a89d3a9f | 68 | #endif |