![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Proof of concept for distance and temperature monitoring
Dependencies: mbed mbedConnectorInterface mbedEndpointNetwork
hcsr04.cpp@8:c69fe28366d8, 2015-05-06 (annotated)
- Committer:
- coyotebush
- Date:
- Wed May 06 04:02:08 2015 +0000
- Revision:
- 8:c69fe28366d8
- Parent:
- 7:939fdc8df95b
- Child:
- 10:338191178cbf
Distance sensing improvements
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Luminoscity | 0:cb422b231ea5 | 1 | /* Copyright (c) 2013 Prabhu Desai |
Luminoscity | 0:cb422b231ea5 | 2 | * pdtechworld@gmail.com |
Luminoscity | 0:cb422b231ea5 | 3 | * |
Luminoscity | 0:cb422b231ea5 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
Luminoscity | 0:cb422b231ea5 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
Luminoscity | 0:cb422b231ea5 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
Luminoscity | 0:cb422b231ea5 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
Luminoscity | 0:cb422b231ea5 | 8 | * furnished to do so, subject to the following conditions: |
Luminoscity | 0:cb422b231ea5 | 9 | * |
Luminoscity | 0:cb422b231ea5 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
Luminoscity | 0:cb422b231ea5 | 11 | * substantial portions of the Software. |
Luminoscity | 0:cb422b231ea5 | 12 | * |
Luminoscity | 0:cb422b231ea5 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
Luminoscity | 0:cb422b231ea5 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
Luminoscity | 0:cb422b231ea5 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
Luminoscity | 0:cb422b231ea5 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Luminoscity | 0:cb422b231ea5 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Luminoscity | 0:cb422b231ea5 | 18 | */ |
Luminoscity | 0:cb422b231ea5 | 19 | |
Luminoscity | 0:cb422b231ea5 | 20 | |
Luminoscity | 0:cb422b231ea5 | 21 | #include "hcsr04.h" |
Luminoscity | 0:cb422b231ea5 | 22 | |
Luminoscity | 0:cb422b231ea5 | 23 | |
Luminoscity | 0:cb422b231ea5 | 24 | HCSR04::HCSR04(PinName TrigPin,PinName EchoPin): |
Luminoscity | 0:cb422b231ea5 | 25 | trigger(TrigPin), echo(EchoPin) |
Luminoscity | 0:cb422b231ea5 | 26 | { |
Luminoscity | 0:cb422b231ea5 | 27 | pulsetime.stop(); |
Luminoscity | 0:cb422b231ea5 | 28 | pulsetime.reset(); |
Luminoscity | 0:cb422b231ea5 | 29 | echo.rise(this,&HCSR04::isr_rise); |
Luminoscity | 0:cb422b231ea5 | 30 | echo.fall(this,&HCSR04::isr_fall); |
Luminoscity | 0:cb422b231ea5 | 31 | trigger=0; |
Luminoscity | 0:cb422b231ea5 | 32 | } |
Luminoscity | 0:cb422b231ea5 | 33 | |
Luminoscity | 0:cb422b231ea5 | 34 | HCSR04::~HCSR04() |
Luminoscity | 0:cb422b231ea5 | 35 | { |
Luminoscity | 0:cb422b231ea5 | 36 | } |
Luminoscity | 0:cb422b231ea5 | 37 | |
Luminoscity | 0:cb422b231ea5 | 38 | void HCSR04::isr_rise(void) |
Luminoscity | 0:cb422b231ea5 | 39 | { |
Luminoscity | 0:cb422b231ea5 | 40 | pulsetime.start(); |
coyotebush | 8:c69fe28366d8 | 41 | pulsetime.reset(); |
Luminoscity | 0:cb422b231ea5 | 42 | } |
Luminoscity | 0:cb422b231ea5 | 43 | void HCSR04::start(void) |
Luminoscity | 0:cb422b231ea5 | 44 | { |
coyotebush | 7:939fdc8df95b | 45 | pulsedur = 0; |
coyotebush | 7:939fdc8df95b | 46 | |
Luminoscity | 0:cb422b231ea5 | 47 | trigger=1; |
Luminoscity | 0:cb422b231ea5 | 48 | wait_us(10); |
Luminoscity | 0:cb422b231ea5 | 49 | trigger=0; |
Luminoscity | 0:cb422b231ea5 | 50 | } |
Luminoscity | 0:cb422b231ea5 | 51 | |
Luminoscity | 0:cb422b231ea5 | 52 | void HCSR04::isr_fall(void) |
Luminoscity | 0:cb422b231ea5 | 53 | { |
Luminoscity | 0:cb422b231ea5 | 54 | pulsetime.stop(); |
Luminoscity | 0:cb422b231ea5 | 55 | pulsedur = pulsetime.read_us(); |
Luminoscity | 0:cb422b231ea5 | 56 | } |
Luminoscity | 0:cb422b231ea5 | 57 | |
Luminoscity | 0:cb422b231ea5 | 58 | void HCSR04::rise (void (*fptr)(void)) |
Luminoscity | 0:cb422b231ea5 | 59 | { |
Luminoscity | 0:cb422b231ea5 | 60 | echo.rise(fptr); |
Luminoscity | 0:cb422b231ea5 | 61 | } |
Luminoscity | 0:cb422b231ea5 | 62 | void HCSR04::fall (void (*fptr)(void)) |
Luminoscity | 0:cb422b231ea5 | 63 | { |
Luminoscity | 0:cb422b231ea5 | 64 | echo.fall(fptr); |
Luminoscity | 0:cb422b231ea5 | 65 | } |
Luminoscity | 0:cb422b231ea5 | 66 | |
coyotebush | 8:c69fe28366d8 | 67 | double HCSR04::get_dist_cm() |
Luminoscity | 0:cb422b231ea5 | 68 | { |
coyotebush | 8:c69fe28366d8 | 69 | if (pulsedur == 0) |
coyotebush | 8:c69fe28366d8 | 70 | return -1; |
coyotebush | 8:c69fe28366d8 | 71 | return pulsedur * kMicrosecondsToCentimeters; |
Luminoscity | 0:cb422b231ea5 | 72 | } |
Luminoscity | 0:cb422b231ea5 | 73 | unsigned int HCSR04::get_pulse_us() |
Luminoscity | 0:cb422b231ea5 | 74 | { |
Luminoscity | 0:cb422b231ea5 | 75 | return pulsedur; |
Luminoscity | 0:cb422b231ea5 | 76 | } |
Luminoscity | 0:cb422b231ea5 | 77 | |
Luminoscity | 0:cb422b231ea5 | 78 | |
Luminoscity | 0:cb422b231ea5 | 79 | |
Luminoscity | 0:cb422b231ea5 | 80 | /******************************************************* |
Luminoscity | 0:cb422b231ea5 | 81 | Here is a sample code usage |
Luminoscity | 0:cb422b231ea5 | 82 | ********************************************************* |
Luminoscity | 0:cb422b231ea5 | 83 | #include "hcsr04.h" |
Luminoscity | 0:cb422b231ea5 | 84 | HCSR04 usensor(p25,p6); |
Luminoscity | 0:cb422b231ea5 | 85 | int main() |
Luminoscity | 0:cb422b231ea5 | 86 | { |
Luminoscity | 0:cb422b231ea5 | 87 | unsigned char count=0; |
Luminoscity | 0:cb422b231ea5 | 88 | while(1) { |
Luminoscity | 0:cb422b231ea5 | 89 | usensor.start(); |
Luminoscity | 0:cb422b231ea5 | 90 | wait_ms(500); |
Luminoscity | 0:cb422b231ea5 | 91 | dist=usensor.get_dist_cm(); |
Luminoscity | 0:cb422b231ea5 | 92 | lcd.cls(); |
Luminoscity | 0:cb422b231ea5 | 93 | lcd.locate(0,0); |
Luminoscity | 0:cb422b231ea5 | 94 | lcd.printf("cm:%ld",dist ); |
Luminoscity | 0:cb422b231ea5 | 95 | |
Luminoscity | 0:cb422b231ea5 | 96 | count++; |
Luminoscity | 0:cb422b231ea5 | 97 | lcd.locate(0,1); |
Luminoscity | 0:cb422b231ea5 | 98 | lcd.printf("Distance =%d",count); |
Luminoscity | 0:cb422b231ea5 | 99 | |
Luminoscity | 0:cb422b231ea5 | 100 | } |
Luminoscity | 0:cb422b231ea5 | 101 | */ |