changed range conditions
Fork of HCSR04 by
HCSR04.h@0:5461d44a187c, 2013-11-24 (annotated)
- Committer:
- rabad1
- Date:
- Sun Nov 24 21:45:46 2013 +0000
- Revision:
- 0:5461d44a187c
- Child:
- 2:3ebde19131af
initial release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rabad1 | 0:5461d44a187c | 1 | /* File: HCSR04.h |
rabad1 | 0:5461d44a187c | 2 | * Author: Robert Abad Copyright (c) 2013 |
rabad1 | 0:5461d44a187c | 3 | * |
rabad1 | 0:5461d44a187c | 4 | * Desc: driver for HCSR04 Ultrasonic Range Finder. The returned range |
rabad1 | 0:5461d44a187c | 5 | * is in units of meters. |
rabad1 | 0:5461d44a187c | 6 | * |
rabad1 | 0:5461d44a187c | 7 | * To use this driver you must call the methods ::startMeas() |
rabad1 | 0:5461d44a187c | 8 | * and ::getMeas(). The HCSR04 requires a trigger time of |
rabad1 | 0:5461d44a187c | 9 | * 10 usec (microseconds) which is initiated by ::startMeas(). |
rabad1 | 0:5461d44a187c | 10 | * If a successful measurement is made, getMeas() will return |
rabad1 | 0:5461d44a187c | 11 | * RANGE_MEAS_VALID. If unsuccessful, initiate a new measurement. |
rabad1 | 0:5461d44a187c | 12 | * |
rabad1 | 0:5461d44a187c | 13 | * The datasheet for this device can be found here: |
rabad1 | 0:5461d44a187c | 14 | * http://www.elecfreaks.com/store/download/product/Sensor/HC-SR04/HC-SR04_Ultrasonic_Module_User_Guide.pdf |
rabad1 | 0:5461d44a187c | 15 | * |
rabad1 | 0:5461d44a187c | 16 | * Below is some sample code: |
rabad1 | 0:5461d44a187c | 17 | * |
rabad1 | 0:5461d44a187c | 18 | * #include "mbed.h" |
rabad1 | 0:5461d44a187c | 19 | * #include "HCSR04.h" |
rabad1 | 0:5461d44a187c | 20 | * |
rabad1 | 0:5461d44a187c | 21 | * #define PIN_TRIGGER (p14) |
rabad1 | 0:5461d44a187c | 22 | * #define PIN_ECHO (p15) |
rabad1 | 0:5461d44a187c | 23 | * |
rabad1 | 0:5461d44a187c | 24 | * int main(void) |
rabad1 | 0:5461d44a187c | 25 | * { |
rabad1 | 0:5461d44a187c | 26 | * HCSR04 rangeFinder( PIN_TRIGGER, PIN_ECHO ); |
rabad1 | 0:5461d44a187c | 27 | * float range; |
rabad1 | 0:5461d44a187c | 28 | * |
rabad1 | 0:5461d44a187c | 29 | * while (1) |
rabad1 | 0:5461d44a187c | 30 | * { |
rabad1 | 0:5461d44a187c | 31 | * rangeFinder.startMeas(); |
rabad1 | 0:5461d44a187c | 32 | * wait(0.1); |
rabad1 | 0:5461d44a187c | 33 | * if ( rangeFinder.getMeas(range) == RANGE_MEAS_VALID ) |
rabad1 | 0:5461d44a187c | 34 | * { |
rabad1 | 0:5461d44a187c | 35 | * printf("range = %f\n\r", range); |
rabad1 | 0:5461d44a187c | 36 | * } |
rabad1 | 0:5461d44a187c | 37 | * } |
rabad1 | 0:5461d44a187c | 38 | * } |
rabad1 | 0:5461d44a187c | 39 | */ |
rabad1 | 0:5461d44a187c | 40 | |
rabad1 | 0:5461d44a187c | 41 | #ifndef __HCSR04_H__ |
rabad1 | 0:5461d44a187c | 42 | #define __HCSR04_H__ |
rabad1 | 0:5461d44a187c | 43 | |
rabad1 | 0:5461d44a187c | 44 | #include "mbed.h" |
rabad1 | 0:5461d44a187c | 45 | |
rabad1 | 0:5461d44a187c | 46 | typedef enum |
rabad1 | 0:5461d44a187c | 47 | { |
rabad1 | 0:5461d44a187c | 48 | RANGE_MEAS_INVALID, |
rabad1 | 0:5461d44a187c | 49 | RANGE_MEAS_VALID |
rabad1 | 0:5461d44a187c | 50 | } etHCSR04_RANGE_STATUS; |
rabad1 | 0:5461d44a187c | 51 | |
rabad1 | 0:5461d44a187c | 52 | class HCSR04 |
rabad1 | 0:5461d44a187c | 53 | { |
rabad1 | 0:5461d44a187c | 54 | public: |
rabad1 | 0:5461d44a187c | 55 | HCSR04( PinName pinTrigger, PinName pinEcho ); |
rabad1 | 0:5461d44a187c | 56 | void startMeas(void); |
rabad1 | 0:5461d44a187c | 57 | etHCSR04_RANGE_STATUS getMeas(float &rRangeMeters); |
rabad1 | 0:5461d44a187c | 58 | |
rabad1 | 0:5461d44a187c | 59 | private: |
rabad1 | 0:5461d44a187c | 60 | DigitalOut trigger; |
rabad1 | 0:5461d44a187c | 61 | Ticker triggerTicker; |
rabad1 | 0:5461d44a187c | 62 | InterruptIn echo; |
rabad1 | 0:5461d44a187c | 63 | Timer echoTimer; |
rabad1 | 0:5461d44a187c | 64 | unsigned long measTimeStart_us; |
rabad1 | 0:5461d44a187c | 65 | unsigned long measTimeStop_us; |
rabad1 | 0:5461d44a187c | 66 | |
rabad1 | 0:5461d44a187c | 67 | void triggerTicker_cb(void); // trigger ticker callback function |
rabad1 | 0:5461d44a187c | 68 | void ISR_echoRising(void); // ISR for rising edge |
rabad1 | 0:5461d44a187c | 69 | void ISR_echoFalling(void); // ISR for falling edge |
rabad1 | 0:5461d44a187c | 70 | }; |
rabad1 | 0:5461d44a187c | 71 | |
rabad1 | 0:5461d44a187c | 72 | #endif /* __HCSR04_H__ */ |