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