test

Fork of HCSR04 by TVZ Mechatronics Team

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