Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of HCSR04 by
HCSR04.h@3:89f21eb28e13, 2014-04-15 (annotated)
- Committer:
- Fairy_Paolina
- Date:
- Tue Apr 15 16:51:11 2014 +0000
- Revision:
- 3:89f21eb28e13
- Parent:
- 2:3ebde19131af
changed range;
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 | |
srsmitherman | 2:3ebde19131af | 46 | #define MTRS_TO_INCH (39.3701) |
srsmitherman | 2:3ebde19131af | 47 | |
rabad1 | 0:5461d44a187c | 48 | typedef enum |
rabad1 | 0:5461d44a187c | 49 | { |
rabad1 | 0:5461d44a187c | 50 | RANGE_MEAS_INVALID, |
rabad1 | 0:5461d44a187c | 51 | RANGE_MEAS_VALID |
rabad1 | 0:5461d44a187c | 52 | } etHCSR04_RANGE_STATUS; |
rabad1 | 0:5461d44a187c | 53 | |
srsmitherman | 2:3ebde19131af | 54 | |
srsmitherman | 2:3ebde19131af | 55 | |
rabad1 | 0:5461d44a187c | 56 | class HCSR04 |
rabad1 | 0:5461d44a187c | 57 | { |
rabad1 | 0:5461d44a187c | 58 | public: |
rabad1 | 0:5461d44a187c | 59 | HCSR04( PinName pinTrigger, PinName pinEcho ); |
rabad1 | 0:5461d44a187c | 60 | void startMeas(void); |
rabad1 | 0:5461d44a187c | 61 | etHCSR04_RANGE_STATUS getMeas(float &rRangeMeters); |
srsmitherman | 2:3ebde19131af | 62 | |
srsmitherman | 2:3ebde19131af | 63 | |
rabad1 | 0:5461d44a187c | 64 | private: |
rabad1 | 0:5461d44a187c | 65 | DigitalOut trigger; |
rabad1 | 0:5461d44a187c | 66 | Ticker triggerTicker; |
rabad1 | 0:5461d44a187c | 67 | InterruptIn echo; |
rabad1 | 0:5461d44a187c | 68 | Timer echoTimer; |
rabad1 | 0:5461d44a187c | 69 | unsigned long measTimeStart_us; |
rabad1 | 0:5461d44a187c | 70 | unsigned long measTimeStop_us; |
srsmitherman | 2:3ebde19131af | 71 | unsigned long dTime_us; |
srsmitherman | 2:3ebde19131af | 72 | etHCSR04_RANGE_STATUS status; |
rabad1 | 0:5461d44a187c | 73 | |
rabad1 | 0:5461d44a187c | 74 | void triggerTicker_cb(void); // trigger ticker callback function |
rabad1 | 0:5461d44a187c | 75 | void ISR_echoRising(void); // ISR for rising edge |
rabad1 | 0:5461d44a187c | 76 | void ISR_echoFalling(void); // ISR for falling edge |
rabad1 | 0:5461d44a187c | 77 | }; |
rabad1 | 0:5461d44a187c | 78 | |
rabad1 | 0:5461d44a187c | 79 | #endif /* __HCSR04_H__ */ |