endpoint including temperature and distance sensors
Dependencies: mbed mbedConnectorInterface mbedEndpointNetwork TrashSensors
Fork of TempAndDistTest by
hcsr04.cpp@10:338191178cbf, 2015-05-18 (annotated)
- Committer:
- coyotebush
- Date:
- Mon May 18 00:44:59 2015 +0000
- Revision:
- 10:338191178cbf
- Parent:
- 8:c69fe28366d8
max distance
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): |
coyotebush | 10:338191178cbf | 25 | trigger(TrigPin), echo(EchoPin), maxpulse(-1) |
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 | 10:338191178cbf | 45 | pulsedur = -1; |
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(); |
coyotebush | 10:338191178cbf | 56 | if (pulsedur > maxpulse && pulsedur < kBogusPulse) |
coyotebush | 10:338191178cbf | 57 | maxpulse = pulsedur; |
Luminoscity | 0:cb422b231ea5 | 58 | } |
Luminoscity | 0:cb422b231ea5 | 59 | |
Luminoscity | 0:cb422b231ea5 | 60 | void HCSR04::rise (void (*fptr)(void)) |
Luminoscity | 0:cb422b231ea5 | 61 | { |
Luminoscity | 0:cb422b231ea5 | 62 | echo.rise(fptr); |
Luminoscity | 0:cb422b231ea5 | 63 | } |
Luminoscity | 0:cb422b231ea5 | 64 | void HCSR04::fall (void (*fptr)(void)) |
Luminoscity | 0:cb422b231ea5 | 65 | { |
Luminoscity | 0:cb422b231ea5 | 66 | echo.fall(fptr); |
Luminoscity | 0:cb422b231ea5 | 67 | } |
Luminoscity | 0:cb422b231ea5 | 68 | |
coyotebush | 8:c69fe28366d8 | 69 | double HCSR04::get_dist_cm() |
Luminoscity | 0:cb422b231ea5 | 70 | { |
coyotebush | 8:c69fe28366d8 | 71 | return pulsedur * kMicrosecondsToCentimeters; |
Luminoscity | 0:cb422b231ea5 | 72 | } |
coyotebush | 10:338191178cbf | 73 | double HCSR04::get_max_dist_cm() |
coyotebush | 10:338191178cbf | 74 | { |
coyotebush | 10:338191178cbf | 75 | return maxpulse * kMicrosecondsToCentimeters; |
coyotebush | 10:338191178cbf | 76 | } |
coyotebush | 10:338191178cbf | 77 | int HCSR04::get_pulse_us() |
Luminoscity | 0:cb422b231ea5 | 78 | { |
Luminoscity | 0:cb422b231ea5 | 79 | return pulsedur; |
Luminoscity | 0:cb422b231ea5 | 80 | } |
Luminoscity | 0:cb422b231ea5 | 81 | |
Luminoscity | 0:cb422b231ea5 | 82 | |
Luminoscity | 0:cb422b231ea5 | 83 | |
Luminoscity | 0:cb422b231ea5 | 84 | /******************************************************* |
Luminoscity | 0:cb422b231ea5 | 85 | Here is a sample code usage |
Luminoscity | 0:cb422b231ea5 | 86 | ********************************************************* |
Luminoscity | 0:cb422b231ea5 | 87 | #include "hcsr04.h" |
Luminoscity | 0:cb422b231ea5 | 88 | HCSR04 usensor(p25,p6); |
Luminoscity | 0:cb422b231ea5 | 89 | int main() |
Luminoscity | 0:cb422b231ea5 | 90 | { |
Luminoscity | 0:cb422b231ea5 | 91 | unsigned char count=0; |
Luminoscity | 0:cb422b231ea5 | 92 | while(1) { |
Luminoscity | 0:cb422b231ea5 | 93 | usensor.start(); |
Luminoscity | 0:cb422b231ea5 | 94 | wait_ms(500); |
Luminoscity | 0:cb422b231ea5 | 95 | dist=usensor.get_dist_cm(); |
Luminoscity | 0:cb422b231ea5 | 96 | lcd.cls(); |
Luminoscity | 0:cb422b231ea5 | 97 | lcd.locate(0,0); |
Luminoscity | 0:cb422b231ea5 | 98 | lcd.printf("cm:%ld",dist ); |
Luminoscity | 0:cb422b231ea5 | 99 | |
Luminoscity | 0:cb422b231ea5 | 100 | count++; |
Luminoscity | 0:cb422b231ea5 | 101 | lcd.locate(0,1); |
Luminoscity | 0:cb422b231ea5 | 102 | lcd.printf("Distance =%d",count); |
Luminoscity | 0:cb422b231ea5 | 103 | |
Luminoscity | 0:cb422b231ea5 | 104 | } |
Luminoscity | 0:cb422b231ea5 | 105 | */ |