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.cpp@0:8871082486ac, 2015-12-05 (annotated)
- Committer:
- tbjazic
- Date:
- Sat Dec 05 09:02:35 2015 +0000
- Revision:
- 0:8871082486ac
- Child:
- 3:9a7899cf5e3a
Initial library commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tbjazic | 0:8871082486ac | 1 | #include "mbed.h" |
tbjazic | 0:8871082486ac | 2 | #include "HCSR04.h" |
tbjazic | 0:8871082486ac | 3 | |
tbjazic | 0:8871082486ac | 4 | HCSR04::HCSR04(PinName echoPin, PinName triggerPin) : echo(echoPin), trigger(triggerPin) { |
tbjazic | 0:8871082486ac | 5 | init(); |
tbjazic | 0:8871082486ac | 6 | } |
tbjazic | 0:8871082486ac | 7 | |
tbjazic | 0:8871082486ac | 8 | void HCSR04::init() { |
tbjazic | 0:8871082486ac | 9 | /** configure the rising edge to start the timer */ |
tbjazic | 0:8871082486ac | 10 | echo.rise(this, &HCSR04::startTimer); |
tbjazic | 0:8871082486ac | 11 | |
tbjazic | 0:8871082486ac | 12 | /** configure the falling edge to stop the timer */ |
tbjazic | 0:8871082486ac | 13 | echo.fall(this, &HCSR04::stopTimer); |
tbjazic | 0:8871082486ac | 14 | |
tbjazic | 0:8871082486ac | 15 | distance = -1; // initial distance |
tbjazic | 0:8871082486ac | 16 | } |
tbjazic | 0:8871082486ac | 17 | |
tbjazic | 0:8871082486ac | 18 | void HCSR04::startTimer() { |
tbjazic | 0:8871082486ac | 19 | timer.start(); // start the timer |
tbjazic | 0:8871082486ac | 20 | } |
tbjazic | 0:8871082486ac | 21 | |
tbjazic | 0:8871082486ac | 22 | void HCSR04::stopTimer() { |
tbjazic | 0:8871082486ac | 23 | timer.stop(); // stop the timer |
tbjazic | 0:8871082486ac | 24 | } |
tbjazic | 0:8871082486ac | 25 | |
tbjazic | 0:8871082486ac | 26 | void HCSR04::startMeasurement() { |
tbjazic | 0:8871082486ac | 27 | /** Start the measurement by sending the 10us trigger pulse. */ |
tbjazic | 0:8871082486ac | 28 | trigger = 1; |
tbjazic | 0:8871082486ac | 29 | wait_us(10); |
tbjazic | 0:8871082486ac | 30 | trigger = 0; |
tbjazic | 0:8871082486ac | 31 | |
tbjazic | 0:8871082486ac | 32 | /** Wait for the sensor to finish measurement (generate rise and fall interrupts). |
tbjazic | 0:8871082486ac | 33 | * Minimum wait time is determined by maximum measurement distance of 400 cm. |
tbjazic | 0:8871082486ac | 34 | * t_min = 400 * 58 = 23200 us = 23.2 ms */ |
tbjazic | 0:8871082486ac | 35 | wait_ms(25); |
tbjazic | 0:8871082486ac | 36 | |
tbjazic | 0:8871082486ac | 37 | /** calculate the distance in cm */ |
tbjazic | 0:8871082486ac | 38 | distance = timer.read() * 1e6 / 58; |
tbjazic | 0:8871082486ac | 39 | timer.reset(); // reset the timer to 0 after storing the distance |
tbjazic | 0:8871082486ac | 40 | } |
tbjazic | 0:8871082486ac | 41 | |
tbjazic | 0:8871082486ac | 42 | float HCSR04::getDistance_cm() { |
tbjazic | 0:8871082486ac | 43 | startMeasurement(); |
tbjazic | 0:8871082486ac | 44 | return distance; |
tbjazic | 0:8871082486ac | 45 | } |