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