Added timeout functionality
Fork of HCSR04 by
hcsr04.h@1:e01896ab28d7, 2016-10-24 (annotated)
- Committer:
- EmbeddedSam
- Date:
- Mon Oct 24 12:59:41 2016 +0000
- Revision:
- 1:e01896ab28d7
- Parent:
- 0:86b2086be101
added timeout functionality
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| antoniolinux | 0:86b2086be101 | 1 | /* File: HCSR04.h |
| antoniolinux | 0:86b2086be101 | 2 | * Author: Antonio Buonanno |
| antoniolinux | 0:86b2086be101 | 3 | *Board: STM NUCLEO F401RE, |
| antoniolinux | 0:86b2086be101 | 4 | *Hardware: Ultrasonic Range HC-SR04, |
| antoniolinux | 0:86b2086be101 | 5 | * |
| antoniolinux | 0:86b2086be101 | 6 | *This work derived from Arduino library, |
| antoniolinux | 0:86b2086be101 | 7 | * |
| antoniolinux | 0:86b2086be101 | 8 | * Desc: driver for HCSR04 Ultrasonic Range Finder. The returned range |
| antoniolinux | 0:86b2086be101 | 9 | * is in units of meters. |
| antoniolinux | 0:86b2086be101 | 10 | * |
| antoniolinux | 0:86b2086be101 | 11 | * |
| antoniolinux | 0:86b2086be101 | 12 | * |
| antoniolinux | 0:86b2086be101 | 13 | */ |
| antoniolinux | 0:86b2086be101 | 14 | |
| antoniolinux | 0:86b2086be101 | 15 | /* EXAMPLE |
| antoniolinux | 0:86b2086be101 | 16 | #include "mbed.h" |
| antoniolinux | 0:86b2086be101 | 17 | #include "hcsr04.h" |
| antoniolinux | 0:86b2086be101 | 18 | |
| antoniolinux | 0:86b2086be101 | 19 | //D12 TRIGGER D11 ECHO |
| antoniolinux | 0:86b2086be101 | 20 | HCSR04 sensor(D12, D11); |
| antoniolinux | 0:86b2086be101 | 21 | int main() { |
| antoniolinux | 0:86b2086be101 | 22 | while(1) { |
| antoniolinux | 0:86b2086be101 | 23 | |
| antoniolinux | 0:86b2086be101 | 24 | long distance = sensor.distance(); |
| antoniolinux | 0:86b2086be101 | 25 | printf("distanza %d \n",distance); |
| antoniolinux | 0:86b2086be101 | 26 | wait(1.0); // 1 sec |
| antoniolinux | 0:86b2086be101 | 27 | |
| antoniolinux | 0:86b2086be101 | 28 | } |
| antoniolinux | 0:86b2086be101 | 29 | } |
| antoniolinux | 0:86b2086be101 | 30 | */ |
| antoniolinux | 0:86b2086be101 | 31 | #ifndef hcsr04_H |
| antoniolinux | 0:86b2086be101 | 32 | #define hcsr04_H |
| antoniolinux | 0:86b2086be101 | 33 | #include "mbed.h" |
| antoniolinux | 0:86b2086be101 | 34 | |
| antoniolinux | 0:86b2086be101 | 35 | |
| antoniolinux | 0:86b2086be101 | 36 | |
| antoniolinux | 0:86b2086be101 | 37 | class HCSR04 { |
| antoniolinux | 0:86b2086be101 | 38 | public: |
| antoniolinux | 0:86b2086be101 | 39 | HCSR04(PinName t, PinName e); |
| antoniolinux | 0:86b2086be101 | 40 | long echo_duration(); |
| antoniolinux | 0:86b2086be101 | 41 | long distance(); |
| EmbeddedSam | 1:e01896ab28d7 | 42 | bool timedOut; |
| antoniolinux | 0:86b2086be101 | 43 | |
| antoniolinux | 0:86b2086be101 | 44 | private: |
| antoniolinux | 0:86b2086be101 | 45 | DigitalOut trig; |
| antoniolinux | 0:86b2086be101 | 46 | DigitalIn echo; |
| antoniolinux | 0:86b2086be101 | 47 | Timer timer; |
| EmbeddedSam | 1:e01896ab28d7 | 48 | Timer timeout_timer; |
| antoniolinux | 0:86b2086be101 | 49 | long duration,distance_cm; |
| antoniolinux | 0:86b2086be101 | 50 | }; |
| antoniolinux | 0:86b2086be101 | 51 | |
| antoniolinux | 0:86b2086be101 | 52 | #endif |
