SMART CLEO Ultrasonic DC

Committer:
SMART_CLEO
Date:
Wed Dec 06 08:51:15 2017 +0000
Revision:
0:36e2fa5ac56d
SMART_CLEO

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SMART_CLEO 0:36e2fa5ac56d 1 #ifndef HCSR04_H_TVZMT
SMART_CLEO 0:36e2fa5ac56d 2 #define HCSR04_H_TVZMT
SMART_CLEO 0:36e2fa5ac56d 3
SMART_CLEO 0:36e2fa5ac56d 4 /** A distance measurement class using ultrasonic sensor HC-SR04.
SMART_CLEO 0:36e2fa5ac56d 5 *
SMART_CLEO 0:36e2fa5ac56d 6 * Example of use:
SMART_CLEO 0:36e2fa5ac56d 7 * @code
SMART_CLEO 0:36e2fa5ac56d 8 * #include "mbed.h"
SMART_CLEO 0:36e2fa5ac56d 9 * #include "HCSR04.h"
SMART_CLEO 0:36e2fa5ac56d 10 *
SMART_CLEO 0:36e2fa5ac56d 11 * Serial pc(USBTX, USBRX);
SMART_CLEO 0:36e2fa5ac56d 12 * Timer timer;
SMART_CLEO 0:36e2fa5ac56d 13 *
SMART_CLEO 0:36e2fa5ac56d 14 * int main() {
SMART_CLEO 0:36e2fa5ac56d 15 * HCSR04 sensor(p5, p7);
SMART_CLEO 0:36e2fa5ac56d 16 * sensor.setRanges(10, 110);
SMART_CLEO 0:36e2fa5ac56d 17 * pc.printf("Min. range = %g cm\n\rMax. range = %g cm\n\r",
SMART_CLEO 0:36e2fa5ac56d 18 * sensor.getMinRange(), sensor.getMaxRange());
SMART_CLEO 0:36e2fa5ac56d 19 * while(true) {
SMART_CLEO 0:36e2fa5ac56d 20 * timer.reset();
SMART_CLEO 0:36e2fa5ac56d 21 * timer.start();
SMART_CLEO 0:36e2fa5ac56d 22 * sensor.startMeasurement();
SMART_CLEO 0:36e2fa5ac56d 23 * while(!sensor.isNewDataReady()) {
SMART_CLEO 0:36e2fa5ac56d 24 * // wait for new data
SMART_CLEO 0:36e2fa5ac56d 25 * // waiting time depends on the distance
SMART_CLEO 0:36e2fa5ac56d 26 * }
SMART_CLEO 0:36e2fa5ac56d 27 * pc.printf("Distance: %5.1f mm\r", sensor.getDistance_mm());
SMART_CLEO 0:36e2fa5ac56d 28 * timer.stop();
SMART_CLEO 0:36e2fa5ac56d 29 * wait_ms(500 - timer.read_ms()); // time the loop
SMART_CLEO 0:36e2fa5ac56d 30 * }
SMART_CLEO 0:36e2fa5ac56d 31 * }
SMART_CLEO 0:36e2fa5ac56d 32 * @endcode
SMART_CLEO 0:36e2fa5ac56d 33 */
SMART_CLEO 0:36e2fa5ac56d 34 class HCSR04 {
SMART_CLEO 0:36e2fa5ac56d 35
SMART_CLEO 0:36e2fa5ac56d 36 public:
SMART_CLEO 0:36e2fa5ac56d 37
SMART_CLEO 0:36e2fa5ac56d 38 /** Receives two PinName variables.
SMART_CLEO 0:36e2fa5ac56d 39 * @param echoPin mbed pin to which the echo signal is connected to
SMART_CLEO 0:36e2fa5ac56d 40 * @param triggerPin mbed pin to which the trigger signal is connected to
SMART_CLEO 0:36e2fa5ac56d 41 */
SMART_CLEO 0:36e2fa5ac56d 42 HCSR04(PinName echoPin, PinName triggerPin);
SMART_CLEO 0:36e2fa5ac56d 43
SMART_CLEO 0:36e2fa5ac56d 44 /** Start the measurement. Measurement time depends on the distance.
SMART_CLEO 0:36e2fa5ac56d 45 * Maximum measurement time is limited to 25 ms (400 cm).
SMART_CLEO 0:36e2fa5ac56d 46 */
SMART_CLEO 0:36e2fa5ac56d 47 void startMeasurement();
SMART_CLEO 0:36e2fa5ac56d 48
SMART_CLEO 0:36e2fa5ac56d 49 /** Returns the distance in cm. Requires previous call of startMeasurement().
SMART_CLEO 0:36e2fa5ac56d 50 * @returns distance of the measuring object in cm.
SMART_CLEO 0:36e2fa5ac56d 51 */
SMART_CLEO 0:36e2fa5ac56d 52 float getDistance_cm();
SMART_CLEO 0:36e2fa5ac56d 53
SMART_CLEO 0:36e2fa5ac56d 54 /** Returns the distance in mm. Requires previous call of startMeasurement().
SMART_CLEO 0:36e2fa5ac56d 55 * @returns distance of the measuring object in mm.
SMART_CLEO 0:36e2fa5ac56d 56 */
SMART_CLEO 0:36e2fa5ac56d 57 float getDistance_mm();
SMART_CLEO 0:36e2fa5ac56d 58
SMART_CLEO 0:36e2fa5ac56d 59 /** Sets the minimum and maximum ranges between the factory values of 2 cm and 400 cm.
SMART_CLEO 0:36e2fa5ac56d 60 * @param minRange Minimum range in cm. Must be between 2 cm and maxRange.
SMART_CLEO 0:36e2fa5ac56d 61 * @param maxRange Maximum range in cm. Must be between minRange and 400 cm.
SMART_CLEO 0:36e2fa5ac56d 62 */
SMART_CLEO 0:36e2fa5ac56d 63 void setRanges(float minRange, float maxRange);
SMART_CLEO 0:36e2fa5ac56d 64
SMART_CLEO 0:36e2fa5ac56d 65 /** Retreives the minimum sensor range set by the user.
SMART_CLEO 0:36e2fa5ac56d 66 * @returns the minimum sensor range set by the user in cm.
SMART_CLEO 0:36e2fa5ac56d 67 */
SMART_CLEO 0:36e2fa5ac56d 68 float getMinRange();
SMART_CLEO 0:36e2fa5ac56d 69
SMART_CLEO 0:36e2fa5ac56d 70 /** Retreives the maximum sensor range set by the user.
SMART_CLEO 0:36e2fa5ac56d 71 * @returns the maximum sensor range set by the user in cm.
SMART_CLEO 0:36e2fa5ac56d 72 */
SMART_CLEO 0:36e2fa5ac56d 73 float getMaxRange();
SMART_CLEO 0:36e2fa5ac56d 74
SMART_CLEO 0:36e2fa5ac56d 75 /** Checks if the new data is ready.
SMART_CLEO 0:36e2fa5ac56d 76 * @returns true if new data is ready, false otherwise.
SMART_CLEO 0:36e2fa5ac56d 77 */
SMART_CLEO 0:36e2fa5ac56d 78 bool isNewDataReady();
SMART_CLEO 0:36e2fa5ac56d 79
SMART_CLEO 0:36e2fa5ac56d 80 private:
SMART_CLEO 0:36e2fa5ac56d 81
SMART_CLEO 0:36e2fa5ac56d 82 InterruptIn echo; // echo pin
SMART_CLEO 0:36e2fa5ac56d 83 DigitalOut trigger; // trigger pin
SMART_CLEO 0:36e2fa5ac56d 84 Timer timer; // echo pulsewidth measurement
SMART_CLEO 0:36e2fa5ac56d 85 float distance; // store the distance in cm
SMART_CLEO 0:36e2fa5ac56d 86 float minDistance; // minimum measurable distance
SMART_CLEO 0:36e2fa5ac56d 87 float maxDistance; // maximum measurable distance
SMART_CLEO 0:36e2fa5ac56d 88 Timeout triggerTimeout, echoTimeout;
SMART_CLEO 0:36e2fa5ac56d 89 bool newDataReady, timerStarted;
SMART_CLEO 0:36e2fa5ac56d 90
SMART_CLEO 0:36e2fa5ac56d 91 /** Start the timer. */
SMART_CLEO 0:36e2fa5ac56d 92 void startTimer();
SMART_CLEO 0:36e2fa5ac56d 93
SMART_CLEO 0:36e2fa5ac56d 94 /** Stop the timer. */
SMART_CLEO 0:36e2fa5ac56d 95 void stopTimer();
SMART_CLEO 0:36e2fa5ac56d 96
SMART_CLEO 0:36e2fa5ac56d 97 /** Initialization. */
SMART_CLEO 0:36e2fa5ac56d 98 void init();
SMART_CLEO 0:36e2fa5ac56d 99
SMART_CLEO 0:36e2fa5ac56d 100 void turnOffTrigger();
SMART_CLEO 0:36e2fa5ac56d 101 };
SMART_CLEO 0:36e2fa5ac56d 102
SMART_CLEO 0:36e2fa5ac56d 103 #endif