How to measure water in a reservoir
Dependencies: Cayenne-MQTT-mbed Cayenne-LPP
HCSR04/hcsr04.cpp@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 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
wamae | 0:7bfeb237e600 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
wamae | 0:7bfeb237e600 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
wamae | 0:7bfeb237e600 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
wamae | 0:7bfeb237e600 | 8 | * furnished to do so, subject to the following conditions: |
wamae | 0:7bfeb237e600 | 9 | * |
wamae | 0:7bfeb237e600 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
wamae | 0:7bfeb237e600 | 11 | * substantial portions of the Software. |
wamae | 0:7bfeb237e600 | 12 | * |
wamae | 0:7bfeb237e600 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
wamae | 0:7bfeb237e600 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
wamae | 0:7bfeb237e600 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
wamae | 0:7bfeb237e600 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
wamae | 0:7bfeb237e600 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
wamae | 0:7bfeb237e600 | 18 | */ |
wamae | 0:7bfeb237e600 | 19 | |
wamae | 0:7bfeb237e600 | 20 | |
wamae | 0:7bfeb237e600 | 21 | #include "hcsr04.h" |
wamae | 0:7bfeb237e600 | 22 | |
wamae | 0:7bfeb237e600 | 23 | |
wamae | 0:7bfeb237e600 | 24 | HCSR04::HCSR04(PinName TrigPin,PinName EchoPin): |
wamae | 0:7bfeb237e600 | 25 | trigger(TrigPin), echo(EchoPin) |
wamae | 0:7bfeb237e600 | 26 | { |
wamae | 0:7bfeb237e600 | 27 | pulsetime.stop(); |
wamae | 0:7bfeb237e600 | 28 | pulsetime.reset(); |
wamae | 0:7bfeb237e600 | 29 | echo.rise(this,&HCSR04::isr_rise); |
wamae | 0:7bfeb237e600 | 30 | echo.fall(this,&HCSR04::isr_fall); |
wamae | 0:7bfeb237e600 | 31 | trigger=0; |
wamae | 0:7bfeb237e600 | 32 | } |
wamae | 0:7bfeb237e600 | 33 | |
wamae | 0:7bfeb237e600 | 34 | HCSR04::~HCSR04() |
wamae | 0:7bfeb237e600 | 35 | { |
wamae | 0:7bfeb237e600 | 36 | } |
wamae | 0:7bfeb237e600 | 37 | |
wamae | 0:7bfeb237e600 | 38 | void HCSR04::isr_rise(void) |
wamae | 0:7bfeb237e600 | 39 | { |
wamae | 0:7bfeb237e600 | 40 | pulsetime.start(); |
wamae | 0:7bfeb237e600 | 41 | } |
wamae | 0:7bfeb237e600 | 42 | void HCSR04::start(void) |
wamae | 0:7bfeb237e600 | 43 | { |
wamae | 0:7bfeb237e600 | 44 | trigger=1; |
wamae | 0:7bfeb237e600 | 45 | wait_us(10); |
wamae | 0:7bfeb237e600 | 46 | trigger=0; |
wamae | 0:7bfeb237e600 | 47 | } |
wamae | 0:7bfeb237e600 | 48 | |
wamae | 0:7bfeb237e600 | 49 | void HCSR04::isr_fall(void) |
wamae | 0:7bfeb237e600 | 50 | { |
wamae | 0:7bfeb237e600 | 51 | pulsetime.stop(); |
wamae | 0:7bfeb237e600 | 52 | pulsedur = pulsetime.read_us(); |
wamae | 0:7bfeb237e600 | 53 | distance= (pulsedur*343)/20000; |
wamae | 0:7bfeb237e600 | 54 | pulsetime.reset(); |
wamae | 0:7bfeb237e600 | 55 | } |
wamae | 0:7bfeb237e600 | 56 | |
wamae | 0:7bfeb237e600 | 57 | void HCSR04::rise (void (*fptr)(void)) |
wamae | 0:7bfeb237e600 | 58 | { |
wamae | 0:7bfeb237e600 | 59 | echo.rise(fptr); |
wamae | 0:7bfeb237e600 | 60 | } |
wamae | 0:7bfeb237e600 | 61 | void HCSR04::fall (void (*fptr)(void)) |
wamae | 0:7bfeb237e600 | 62 | { |
wamae | 0:7bfeb237e600 | 63 | echo.fall(fptr); |
wamae | 0:7bfeb237e600 | 64 | } |
wamae | 0:7bfeb237e600 | 65 | |
wamae | 0:7bfeb237e600 | 66 | unsigned int HCSR04::get_dist_cm() |
wamae | 0:7bfeb237e600 | 67 | { |
wamae | 0:7bfeb237e600 | 68 | return distance; |
wamae | 0:7bfeb237e600 | 69 | } |
wamae | 0:7bfeb237e600 | 70 | unsigned int HCSR04::get_pulse_us() |
wamae | 0:7bfeb237e600 | 71 | { |
wamae | 0:7bfeb237e600 | 72 | return pulsedur; |
wamae | 0:7bfeb237e600 | 73 | } |
wamae | 0:7bfeb237e600 | 74 | |
wamae | 0:7bfeb237e600 | 75 | |
wamae | 0:7bfeb237e600 | 76 | |
wamae | 0:7bfeb237e600 | 77 | /******************************************************* |
wamae | 0:7bfeb237e600 | 78 | Here is a sample code usage |
wamae | 0:7bfeb237e600 | 79 | ********************************************************* |
wamae | 0:7bfeb237e600 | 80 | #include "hcsr04.h" |
wamae | 0:7bfeb237e600 | 81 | HCSR04 usensor(p25,p6); |
wamae | 0:7bfeb237e600 | 82 | int main() |
wamae | 0:7bfeb237e600 | 83 | { |
wamae | 0:7bfeb237e600 | 84 | unsigned char count=0; |
wamae | 0:7bfeb237e600 | 85 | while(1) { |
wamae | 0:7bfeb237e600 | 86 | usensor.start(); |
wamae | 0:7bfeb237e600 | 87 | wait_ms(500); |
wamae | 0:7bfeb237e600 | 88 | dist=usensor.get_dist_cm(); |
wamae | 0:7bfeb237e600 | 89 | lcd.cls(); |
wamae | 0:7bfeb237e600 | 90 | lcd.locate(0,0); |
wamae | 0:7bfeb237e600 | 91 | lcd.printf("cm:%ld",dist ); |
wamae | 0:7bfeb237e600 | 92 | |
wamae | 0:7bfeb237e600 | 93 | count++; |
wamae | 0:7bfeb237e600 | 94 | lcd.locate(0,1); |
wamae | 0:7bfeb237e600 | 95 | lcd.printf("Distance =%d",count); |
wamae | 0:7bfeb237e600 | 96 | |
wamae | 0:7bfeb237e600 | 97 | } |
wamae | 0:7bfeb237e600 | 98 | */ |