Libreria ultrasuoni
Dependents: TextLCD_HelloWorld ES_4_P4
HCSR04.h@0:f14a3481cfc3, 2016-10-24 (annotated)
- Committer:
- Mattinico
- Date:
- Mon Oct 24 12:53:49 2016 +0000
- Revision:
- 0:f14a3481cfc3
ghj
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mattinico | 0:f14a3481cfc3 | 1 | #ifndef HCSR04_H_TVZMT |
Mattinico | 0:f14a3481cfc3 | 2 | #define HCSR04_H_TVZMT |
Mattinico | 0:f14a3481cfc3 | 3 | |
Mattinico | 0:f14a3481cfc3 | 4 | /** A distance measurement class using ultrasonic sensor HC-SR04. |
Mattinico | 0:f14a3481cfc3 | 5 | * |
Mattinico | 0:f14a3481cfc3 | 6 | * Example of use: |
Mattinico | 0:f14a3481cfc3 | 7 | * @code |
Mattinico | 0:f14a3481cfc3 | 8 | * #include "mbed.h" |
Mattinico | 0:f14a3481cfc3 | 9 | * #include "HCSR04.h" |
Mattinico | 0:f14a3481cfc3 | 10 | * |
Mattinico | 0:f14a3481cfc3 | 11 | * Serial pc(USBTX, USBRX); |
Mattinico | 0:f14a3481cfc3 | 12 | * |
Mattinico | 0:f14a3481cfc3 | 13 | * int main() { |
Mattinico | 0:f14a3481cfc3 | 14 | * HCSR04 sensor(p5, p7); |
Mattinico | 0:f14a3481cfc3 | 15 | * sensor.setRanges(10, 110); |
Mattinico | 0:f14a3481cfc3 | 16 | * pc.printf("Min. range = %g cm\n\rMax. range = %g cm\n\r", |
Mattinico | 0:f14a3481cfc3 | 17 | * sensor.getMinRange(), sensor.getMaxRange()); |
Mattinico | 0:f14a3481cfc3 | 18 | * while(1) { |
Mattinico | 0:f14a3481cfc3 | 19 | * pc.printf("Distance: %5.1f mm\r", sensor.getDistance_mm()); |
Mattinico | 0:f14a3481cfc3 | 20 | * wait_ms(500); |
Mattinico | 0:f14a3481cfc3 | 21 | * } |
Mattinico | 0:f14a3481cfc3 | 22 | * } |
Mattinico | 0:f14a3481cfc3 | 23 | * @endcode |
Mattinico | 0:f14a3481cfc3 | 24 | */ |
Mattinico | 0:f14a3481cfc3 | 25 | class HCSR04 { |
Mattinico | 0:f14a3481cfc3 | 26 | |
Mattinico | 0:f14a3481cfc3 | 27 | public: |
Mattinico | 0:f14a3481cfc3 | 28 | |
Mattinico | 0:f14a3481cfc3 | 29 | /** Receives two PinName variables. |
Mattinico | 0:f14a3481cfc3 | 30 | * @param echoPin mbed pin to which the echo signal is connected to |
Mattinico | 0:f14a3481cfc3 | 31 | * @param triggerPin mbed pin to which the trigger signal is connected to |
Mattinico | 0:f14a3481cfc3 | 32 | */ |
Mattinico | 0:f14a3481cfc3 | 33 | HCSR04(PinName echoPin, PinName triggerPin); |
Mattinico | 0:f14a3481cfc3 | 34 | |
Mattinico | 0:f14a3481cfc3 | 35 | /** Calculates the distance in cm, with the calculation time of approximatelly 23.7 ms. |
Mattinico | 0:f14a3481cfc3 | 36 | * @returns distance of the measuring object in cm. |
Mattinico | 0:f14a3481cfc3 | 37 | */ |
Mattinico | 0:f14a3481cfc3 | 38 | float getDistance_cm(); |
Mattinico | 0:f14a3481cfc3 | 39 | |
Mattinico | 0:f14a3481cfc3 | 40 | /** Calculates the distance in mm, with the calculation time of approximatelly 23.7 ms. |
Mattinico | 0:f14a3481cfc3 | 41 | * @returns distance of the measuring object in mm. |
Mattinico | 0:f14a3481cfc3 | 42 | */ |
Mattinico | 0:f14a3481cfc3 | 43 | float getDistance_mm(); |
Mattinico | 0:f14a3481cfc3 | 44 | |
Mattinico | 0:f14a3481cfc3 | 45 | /** Sets the minimum and maximum ranges between the factory values of 2 cm and 400 cm. |
Mattinico | 0:f14a3481cfc3 | 46 | * @param minRange Minimum range in cm. Must be between 2 cm and maxRange. |
Mattinico | 0:f14a3481cfc3 | 47 | * @param maxRange Maximum range in cm. Must be between minRange and 400 cm. |
Mattinico | 0:f14a3481cfc3 | 48 | */ |
Mattinico | 0:f14a3481cfc3 | 49 | void setRanges(float minRange, float maxRange); |
Mattinico | 0:f14a3481cfc3 | 50 | |
Mattinico | 0:f14a3481cfc3 | 51 | /** Retreives the minimum sensor range set by the user. |
Mattinico | 0:f14a3481cfc3 | 52 | * @returns the minimum sensor range set by the user in cm. |
Mattinico | 0:f14a3481cfc3 | 53 | */ |
Mattinico | 0:f14a3481cfc3 | 54 | float getMinRange(); |
Mattinico | 0:f14a3481cfc3 | 55 | |
Mattinico | 0:f14a3481cfc3 | 56 | /** Retreives the maximum sensor range set by the user. |
Mattinico | 0:f14a3481cfc3 | 57 | * @returns the maximum sensor range set by the user in cm. |
Mattinico | 0:f14a3481cfc3 | 58 | */ |
Mattinico | 0:f14a3481cfc3 | 59 | float getMaxRange(); |
Mattinico | 0:f14a3481cfc3 | 60 | |
Mattinico | 0:f14a3481cfc3 | 61 | private: |
Mattinico | 0:f14a3481cfc3 | 62 | |
Mattinico | 0:f14a3481cfc3 | 63 | InterruptIn echo; // echo pin |
Mattinico | 0:f14a3481cfc3 | 64 | DigitalOut trigger; // trigger pin |
Mattinico | 0:f14a3481cfc3 | 65 | Timer timer; // echo pulsewidth measurement |
Mattinico | 0:f14a3481cfc3 | 66 | float distance; // store the distance in cm |
Mattinico | 0:f14a3481cfc3 | 67 | float minDistance; // minimum measurable distance |
Mattinico | 0:f14a3481cfc3 | 68 | float maxDistance; // maximum measurable distance |
Mattinico | 0:f14a3481cfc3 | 69 | |
Mattinico | 0:f14a3481cfc3 | 70 | /** Start the timer. */ |
Mattinico | 0:f14a3481cfc3 | 71 | void startTimer(); |
Mattinico | 0:f14a3481cfc3 | 72 | |
Mattinico | 0:f14a3481cfc3 | 73 | /** Stop the timer. */ |
Mattinico | 0:f14a3481cfc3 | 74 | void stopTimer(); |
Mattinico | 0:f14a3481cfc3 | 75 | |
Mattinico | 0:f14a3481cfc3 | 76 | /** Initialization. */ |
Mattinico | 0:f14a3481cfc3 | 77 | void init(); |
Mattinico | 0:f14a3481cfc3 | 78 | |
Mattinico | 0:f14a3481cfc3 | 79 | /** Start the measurement. */ |
Mattinico | 0:f14a3481cfc3 | 80 | void startMeasurement(); |
Mattinico | 0:f14a3481cfc3 | 81 | }; |
Mattinico | 0:f14a3481cfc3 | 82 | |
Mattinico | 0:f14a3481cfc3 | 83 | #endif |