A distance measurement class using ultrasonic sensor HC-SR04.

Dependents:   Esercitazione4_4 HC-SR04 Group10_slave Oled_Gus ... more

The purpose of this library is to encourage students to develope their own classes. Instructions how to follow the development of this library for ultrasonic distance measurement are given here.

Committer:
tbjazic
Date:
Sat Dec 05 09:42:14 2015 +0000
Revision:
2:aa59a9c531be
Parent:
1:9989fcb7f189
Child:
3:9a7899cf5e3a
Another minor documentation update.

Who changed what in which revision?

UserRevisionLine numberNew 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 0:8871082486ac 11 * Serial pc(USBTX, USBRX); // communication with terminal
tbjazic 0:8871082486ac 12 *
tbjazic 0:8871082486ac 13 * int main() {
tbjazic 0:8871082486ac 14 * HCSR04 sensor(p5, p7); // instantiate the sensor object
tbjazic 0:8871082486ac 15 * while(1) {
tbjazic 0:8871082486ac 16 * pc.printf("Distance: %5.1f cm\r", sensor.getDistance_cm());
tbjazic 0:8871082486ac 17 * wait_ms(975); // print the result every 1 second
tbjazic 0:8871082486ac 18 * }
tbjazic 0:8871082486ac 19 * }
tbjazic 2:aa59a9c531be 20 * @endcode
tbjazic 0:8871082486ac 21 */
tbjazic 0:8871082486ac 22 class HCSR04 {
tbjazic 0:8871082486ac 23
tbjazic 0:8871082486ac 24 public:
tbjazic 0:8871082486ac 25
tbjazic 0:8871082486ac 26 /** Receives two PinName variables.
tbjazic 0:8871082486ac 27 * @param echoPin mbed pin to which the echo signal is connected to
tbjazic 0:8871082486ac 28 * @param triggerPin mbed pin to which the trigger signal is connected to
tbjazic 0:8871082486ac 29 */
tbjazic 0:8871082486ac 30 HCSR04(PinName echoPin, PinName triggerPin);
tbjazic 0:8871082486ac 31
tbjazic 0:8871082486ac 32 /** Calculates the distance in cm, with the calculation time of 25 ms.
tbjazic 0:8871082486ac 33 * @returns distance of the measuring object in cm.
tbjazic 0:8871082486ac 34 */
tbjazic 0:8871082486ac 35 float getDistance_cm();
tbjazic 0:8871082486ac 36
tbjazic 0:8871082486ac 37 private:
tbjazic 0:8871082486ac 38
tbjazic 0:8871082486ac 39 InterruptIn echo; // echo pin
tbjazic 0:8871082486ac 40 DigitalOut trigger; // trigger pin
tbjazic 0:8871082486ac 41 Timer timer; // echo pulsewidth measurement
tbjazic 0:8871082486ac 42 float distance; // store the distance in cm
tbjazic 0:8871082486ac 43
tbjazic 0:8871082486ac 44 /** Start the timer. */
tbjazic 0:8871082486ac 45 void startTimer();
tbjazic 0:8871082486ac 46
tbjazic 0:8871082486ac 47 /** Stop the timer. */
tbjazic 0:8871082486ac 48 void stopTimer();
tbjazic 0:8871082486ac 49
tbjazic 0:8871082486ac 50 /** Initialization. */
tbjazic 0:8871082486ac 51 void init();
tbjazic 0:8871082486ac 52
tbjazic 0:8871082486ac 53 /** Start the measurement. */
tbjazic 0:8871082486ac 54 void startMeasurement();
tbjazic 0:8871082486ac 55 };
tbjazic 0:8871082486ac 56
tbjazic 0:8871082486ac 57 #endif