SMART CLEO Uart Ultrasonic

Committer:
SMART_CLEO
Date:
Thu Sep 28 03:42:40 2017 +0000
Revision:
0:5940ac32cd9f
SMART_CLEO

Who changed what in which revision?

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