Libreria ultrasuoni

Dependents:   TextLCD_HelloWorld ES_4_P4

Committer:
Mattinico
Date:
Mon Oct 24 12:53:49 2016 +0000
Revision:
0:f14a3481cfc3
ghj

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Mattinico 0:f14a3481cfc3 1 #include "mbed.h"
Mattinico 0:f14a3481cfc3 2 #include "HCSR04.h"
Mattinico 0:f14a3481cfc3 3
Mattinico 0:f14a3481cfc3 4 HCSR04::HCSR04(PinName echoPin, PinName triggerPin) : echo(echoPin), trigger(triggerPin) {
Mattinico 0:f14a3481cfc3 5 init();
Mattinico 0:f14a3481cfc3 6 }
Mattinico 0:f14a3481cfc3 7
Mattinico 0:f14a3481cfc3 8 void HCSR04::init() {
Mattinico 0:f14a3481cfc3 9 /** configure the rising edge to start the timer */
Mattinico 0:f14a3481cfc3 10 echo.rise(this, &HCSR04::startTimer);
Mattinico 0:f14a3481cfc3 11
Mattinico 0:f14a3481cfc3 12 /** configure the falling edge to stop the timer */
Mattinico 0:f14a3481cfc3 13 echo.fall(this, &HCSR04::stopTimer);
Mattinico 0:f14a3481cfc3 14
Mattinico 0:f14a3481cfc3 15 distance = -1; // initial distance
Mattinico 0:f14a3481cfc3 16 minDistance = 2;
Mattinico 0:f14a3481cfc3 17 maxDistance = 400;
Mattinico 0:f14a3481cfc3 18 }
Mattinico 0:f14a3481cfc3 19
Mattinico 0:f14a3481cfc3 20 void HCSR04::startTimer() {
Mattinico 0:f14a3481cfc3 21 timer.start(); // start the timer
Mattinico 0:f14a3481cfc3 22 }
Mattinico 0:f14a3481cfc3 23
Mattinico 0:f14a3481cfc3 24 void HCSR04::stopTimer() {
Mattinico 0:f14a3481cfc3 25 timer.stop(); // stop the timer
Mattinico 0:f14a3481cfc3 26 }
Mattinico 0:f14a3481cfc3 27
Mattinico 0:f14a3481cfc3 28 void HCSR04::startMeasurement() {
Mattinico 0:f14a3481cfc3 29 trigger = 1;
Mattinico 0:f14a3481cfc3 30 wait_us(10);
Mattinico 0:f14a3481cfc3 31 trigger = 0;
Mattinico 0:f14a3481cfc3 32 wait_us(23660); // just enough time to measure 400 cm
Mattinico 0:f14a3481cfc3 33 timer.stop(); // just in case echo fall did not occur
Mattinico 0:f14a3481cfc3 34 distance = timer.read() * 1e6 / 58;
Mattinico 0:f14a3481cfc3 35 if (distance < minDistance)
Mattinico 0:f14a3481cfc3 36 distance = minDistance;
Mattinico 0:f14a3481cfc3 37 if (distance > maxDistance)
Mattinico 0:f14a3481cfc3 38 distance = maxDistance;
Mattinico 0:f14a3481cfc3 39 timer.reset();
Mattinico 0:f14a3481cfc3 40 }
Mattinico 0:f14a3481cfc3 41
Mattinico 0:f14a3481cfc3 42 float HCSR04::getDistance_cm() {
Mattinico 0:f14a3481cfc3 43 startMeasurement();
Mattinico 0:f14a3481cfc3 44 return distance;
Mattinico 0:f14a3481cfc3 45 }
Mattinico 0:f14a3481cfc3 46
Mattinico 0:f14a3481cfc3 47 float HCSR04::getDistance_mm() {
Mattinico 0:f14a3481cfc3 48 startMeasurement();
Mattinico 0:f14a3481cfc3 49 return distance * 10;
Mattinico 0:f14a3481cfc3 50 }
Mattinico 0:f14a3481cfc3 51
Mattinico 0:f14a3481cfc3 52 void HCSR04::setRanges(float minRange, float maxRange) {
Mattinico 0:f14a3481cfc3 53 if (minRange < maxRange) {
Mattinico 0:f14a3481cfc3 54 if (minRange >= 2)
Mattinico 0:f14a3481cfc3 55 minDistance = minRange;
Mattinico 0:f14a3481cfc3 56 if (maxRange <= 400)
Mattinico 0:f14a3481cfc3 57 maxDistance = maxRange;
Mattinico 0:f14a3481cfc3 58 }
Mattinico 0:f14a3481cfc3 59 }
Mattinico 0:f14a3481cfc3 60
Mattinico 0:f14a3481cfc3 61 float HCSR04::getMinRange() {
Mattinico 0:f14a3481cfc3 62 return minDistance;
Mattinico 0:f14a3481cfc3 63 }
Mattinico 0:f14a3481cfc3 64
Mattinico 0:f14a3481cfc3 65 float HCSR04::getMaxRange() {
Mattinico 0:f14a3481cfc3 66 return maxDistance;
Mattinico 0:f14a3481cfc3 67 }