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.
Diff: HCSR04_Library/HCSR04.cpp
- Revision:
- 0:34c1f05d8d2c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/HCSR04_Library/HCSR04.cpp Sat Nov 16 10:14:58 2019 +0000
@@ -0,0 +1,81 @@
+#include "mbed.h"
+#include "HCSR04.h"
+
+HCSR04::HCSR04(PinName echoPin, PinName triggerPin) : echo(echoPin), trigger(triggerPin) {
+ init();
+}
+
+void HCSR04::init() {
+ distance = -1; // initial distance
+ minDistance = 2;
+ maxDistance = 400;
+ newDataReady = timerStarted = false;
+}
+
+void HCSR04::startTimer() {
+ if (!timerStarted) {
+ timer.start(); // start the timer
+ timerStarted = true;
+ echoTimeout.attach_us(this, &HCSR04::stopTimer, 25000); // in case echo fall does not occur
+ echo.fall(this, &HCSR04::stopTimer);
+ echo.rise(NULL);
+ }
+}
+
+void HCSR04::stopTimer() {
+ timer.stop(); // stop the timer
+ if (timerStarted) {
+ distance = timer.read() * 1e6 / 58;
+ if (distance < minDistance)
+ distance = minDistance;
+ if (distance > maxDistance)
+ distance = maxDistance;
+ newDataReady = true;
+ }
+ timer.reset();
+ timerStarted = false;
+ echoTimeout.detach();
+ echo.fall(NULL);
+}
+
+void HCSR04::turnOffTrigger() {
+ trigger = 0;
+}
+
+void HCSR04::startMeasurement() {
+ trigger = 1;
+ triggerTimeout.attach_us(this, &HCSR04::turnOffTrigger, 10);
+ echo.rise(this, &HCSR04::startTimer);
+ newDataReady = false;
+}
+
+float HCSR04::getDistance_cm() {
+ newDataReady = false;
+ return distance;
+}
+
+float HCSR04::getDistance_mm() {
+ newDataReady = false;
+ return distance * 10;
+}
+
+bool HCSR04::isNewDataReady() {
+ return newDataReady;
+}
+
+void HCSR04::setRanges(float minRange, float maxRange) {
+ if (minRange < maxRange) {
+ if (minRange >= 2 && minRange < 400) // bug from revs. 4 and 5 corrected
+ minDistance = minRange;
+ if (maxRange <= 400)
+ maxDistance = maxRange;
+ }
+}
+
+float HCSR04::getMinRange() {
+ return minDistance;
+}
+
+float HCSR04::getMaxRange() {
+ return maxDistance;
+}
\ No newline at end of file