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