![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
SMART CLEO Ultrasonic DC
HCSR04/HCSR04.cpp@0:36e2fa5ac56d, 2017-12-06 (annotated)
- Committer:
- SMART_CLEO
- Date:
- Wed Dec 06 08:51:15 2017 +0000
- Revision:
- 0:36e2fa5ac56d
SMART_CLEO
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SMART_CLEO | 0:36e2fa5ac56d | 1 | #include "mbed.h" |
SMART_CLEO | 0:36e2fa5ac56d | 2 | #include "HCSR04.h" |
SMART_CLEO | 0:36e2fa5ac56d | 3 | |
SMART_CLEO | 0:36e2fa5ac56d | 4 | HCSR04::HCSR04(PinName echoPin, PinName triggerPin) : echo(echoPin), trigger(triggerPin) { |
SMART_CLEO | 0:36e2fa5ac56d | 5 | init(); |
SMART_CLEO | 0:36e2fa5ac56d | 6 | } |
SMART_CLEO | 0:36e2fa5ac56d | 7 | |
SMART_CLEO | 0:36e2fa5ac56d | 8 | void HCSR04::init() { |
SMART_CLEO | 0:36e2fa5ac56d | 9 | distance = -1; // initial distance |
SMART_CLEO | 0:36e2fa5ac56d | 10 | minDistance = 2; |
SMART_CLEO | 0:36e2fa5ac56d | 11 | maxDistance = 400; |
SMART_CLEO | 0:36e2fa5ac56d | 12 | newDataReady = timerStarted = false; |
SMART_CLEO | 0:36e2fa5ac56d | 13 | } |
SMART_CLEO | 0:36e2fa5ac56d | 14 | |
SMART_CLEO | 0:36e2fa5ac56d | 15 | void HCSR04::startTimer() { |
SMART_CLEO | 0:36e2fa5ac56d | 16 | if (!timerStarted) { |
SMART_CLEO | 0:36e2fa5ac56d | 17 | timer.start(); // start the timer |
SMART_CLEO | 0:36e2fa5ac56d | 18 | timerStarted = true; |
SMART_CLEO | 0:36e2fa5ac56d | 19 | echoTimeout.attach_us(this, &HCSR04::stopTimer, 25000); // in case echo fall does not occur |
SMART_CLEO | 0:36e2fa5ac56d | 20 | echo.fall(this, &HCSR04::stopTimer); |
SMART_CLEO | 0:36e2fa5ac56d | 21 | echo.rise(NULL); |
SMART_CLEO | 0:36e2fa5ac56d | 22 | } |
SMART_CLEO | 0:36e2fa5ac56d | 23 | } |
SMART_CLEO | 0:36e2fa5ac56d | 24 | |
SMART_CLEO | 0:36e2fa5ac56d | 25 | void HCSR04::stopTimer() { |
SMART_CLEO | 0:36e2fa5ac56d | 26 | timer.stop(); // stop the timer |
SMART_CLEO | 0:36e2fa5ac56d | 27 | if (timerStarted) { |
SMART_CLEO | 0:36e2fa5ac56d | 28 | distance = timer.read() * 1e6 / 58; |
SMART_CLEO | 0:36e2fa5ac56d | 29 | if (distance < minDistance) |
SMART_CLEO | 0:36e2fa5ac56d | 30 | distance = minDistance; |
SMART_CLEO | 0:36e2fa5ac56d | 31 | if (distance > maxDistance) |
SMART_CLEO | 0:36e2fa5ac56d | 32 | distance = maxDistance; |
SMART_CLEO | 0:36e2fa5ac56d | 33 | newDataReady = true; |
SMART_CLEO | 0:36e2fa5ac56d | 34 | } |
SMART_CLEO | 0:36e2fa5ac56d | 35 | timer.reset(); |
SMART_CLEO | 0:36e2fa5ac56d | 36 | timerStarted = false; |
SMART_CLEO | 0:36e2fa5ac56d | 37 | echoTimeout.detach(); |
SMART_CLEO | 0:36e2fa5ac56d | 38 | echo.fall(NULL); |
SMART_CLEO | 0:36e2fa5ac56d | 39 | } |
SMART_CLEO | 0:36e2fa5ac56d | 40 | |
SMART_CLEO | 0:36e2fa5ac56d | 41 | void HCSR04::turnOffTrigger() { |
SMART_CLEO | 0:36e2fa5ac56d | 42 | trigger = 0; |
SMART_CLEO | 0:36e2fa5ac56d | 43 | } |
SMART_CLEO | 0:36e2fa5ac56d | 44 | |
SMART_CLEO | 0:36e2fa5ac56d | 45 | void HCSR04::startMeasurement() { |
SMART_CLEO | 0:36e2fa5ac56d | 46 | trigger = 1; |
SMART_CLEO | 0:36e2fa5ac56d | 47 | triggerTimeout.attach_us(this, &HCSR04::turnOffTrigger, 10); |
SMART_CLEO | 0:36e2fa5ac56d | 48 | echo.rise(this, &HCSR04::startTimer); |
SMART_CLEO | 0:36e2fa5ac56d | 49 | newDataReady = false; |
SMART_CLEO | 0:36e2fa5ac56d | 50 | } |
SMART_CLEO | 0:36e2fa5ac56d | 51 | |
SMART_CLEO | 0:36e2fa5ac56d | 52 | float HCSR04::getDistance_cm() { |
SMART_CLEO | 0:36e2fa5ac56d | 53 | newDataReady = false; |
SMART_CLEO | 0:36e2fa5ac56d | 54 | return distance; |
SMART_CLEO | 0:36e2fa5ac56d | 55 | } |
SMART_CLEO | 0:36e2fa5ac56d | 56 | |
SMART_CLEO | 0:36e2fa5ac56d | 57 | float HCSR04::getDistance_mm() { |
SMART_CLEO | 0:36e2fa5ac56d | 58 | newDataReady = false; |
SMART_CLEO | 0:36e2fa5ac56d | 59 | return distance * 10; |
SMART_CLEO | 0:36e2fa5ac56d | 60 | } |
SMART_CLEO | 0:36e2fa5ac56d | 61 | |
SMART_CLEO | 0:36e2fa5ac56d | 62 | bool HCSR04::isNewDataReady() { |
SMART_CLEO | 0:36e2fa5ac56d | 63 | return newDataReady; |
SMART_CLEO | 0:36e2fa5ac56d | 64 | } |
SMART_CLEO | 0:36e2fa5ac56d | 65 | |
SMART_CLEO | 0:36e2fa5ac56d | 66 | void HCSR04::setRanges(float minRange, float maxRange) { |
SMART_CLEO | 0:36e2fa5ac56d | 67 | if (minRange < maxRange) { |
SMART_CLEO | 0:36e2fa5ac56d | 68 | if (minRange >= 2 && minRange < 400) // bug from revs. 4 and 5 corrected |
SMART_CLEO | 0:36e2fa5ac56d | 69 | minDistance = minRange; |
SMART_CLEO | 0:36e2fa5ac56d | 70 | if (maxRange <= 400) |
SMART_CLEO | 0:36e2fa5ac56d | 71 | maxDistance = maxRange; |
SMART_CLEO | 0:36e2fa5ac56d | 72 | } |
SMART_CLEO | 0:36e2fa5ac56d | 73 | } |
SMART_CLEO | 0:36e2fa5ac56d | 74 | |
SMART_CLEO | 0:36e2fa5ac56d | 75 | float HCSR04::getMinRange() { |
SMART_CLEO | 0:36e2fa5ac56d | 76 | return minDistance; |
SMART_CLEO | 0:36e2fa5ac56d | 77 | } |
SMART_CLEO | 0:36e2fa5ac56d | 78 | |
SMART_CLEO | 0:36e2fa5ac56d | 79 | float HCSR04::getMaxRange() { |
SMART_CLEO | 0:36e2fa5ac56d | 80 | return maxDistance; |
SMART_CLEO | 0:36e2fa5ac56d | 81 | } |