Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of HC-SR04 by
main.cpp@1:22043b67c31c, 2015-12-04 (annotated)
- Committer:
- tbjazic
- Date:
- Fri Dec 04 17:38:02 2015 +0000
- Revision:
- 1:22043b67c31c
- Parent:
- 0:ebee649c5b1b
- Child:
- 2:f86b1e3609b3
A class HCSR04 implemented within the main.cpp file.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tbjazic | 1:22043b67c31c | 1 | /** Revision 1: Building the class */ |
| tbjazic | 0:ebee649c5b1b | 2 | |
| tbjazic | 0:ebee649c5b1b | 3 | #include "mbed.h" |
| tbjazic | 0:ebee649c5b1b | 4 | |
| tbjazic | 0:ebee649c5b1b | 5 | Serial pc(USBTX, USBRX); // communication with terminal |
| tbjazic | 0:ebee649c5b1b | 6 | |
| tbjazic | 1:22043b67c31c | 7 | class HCSR04 { |
| tbjazic | 1:22043b67c31c | 8 | |
| tbjazic | 1:22043b67c31c | 9 | public: |
| tbjazic | 1:22043b67c31c | 10 | |
| tbjazic | 1:22043b67c31c | 11 | /** Receives two PinName variables. |
| tbjazic | 1:22043b67c31c | 12 | * @param echoPin mbed pin to which the echo signal is connected to |
| tbjazic | 1:22043b67c31c | 13 | * @param triggerPin mbed pin to which the trigger signal is connected to |
| tbjazic | 1:22043b67c31c | 14 | */ |
| tbjazic | 1:22043b67c31c | 15 | HCSR04(PinName echoPin, PinName triggerPin); |
| tbjazic | 1:22043b67c31c | 16 | |
| tbjazic | 1:22043b67c31c | 17 | /** Calculates the distance in cm, with the calculation time of 25 ms. |
| tbjazic | 1:22043b67c31c | 18 | * @returns distance of the measuring object in cm. |
| tbjazic | 1:22043b67c31c | 19 | */ |
| tbjazic | 1:22043b67c31c | 20 | float getDistance_cm(); |
| tbjazic | 1:22043b67c31c | 21 | |
| tbjazic | 1:22043b67c31c | 22 | private: |
| tbjazic | 1:22043b67c31c | 23 | |
| tbjazic | 1:22043b67c31c | 24 | InterruptIn echo; // echo pin |
| tbjazic | 1:22043b67c31c | 25 | DigitalOut trigger; // trigger pin |
| tbjazic | 1:22043b67c31c | 26 | Timer timer; // echo pulsewidth measurement |
| tbjazic | 1:22043b67c31c | 27 | float distance; // store the distance in cm |
| tbjazic | 1:22043b67c31c | 28 | |
| tbjazic | 1:22043b67c31c | 29 | /** Start the timer. */ |
| tbjazic | 1:22043b67c31c | 30 | void startTimer(); |
| tbjazic | 1:22043b67c31c | 31 | |
| tbjazic | 1:22043b67c31c | 32 | /** Stop the timer. */ |
| tbjazic | 1:22043b67c31c | 33 | void stopTimer(); |
| tbjazic | 1:22043b67c31c | 34 | |
| tbjazic | 1:22043b67c31c | 35 | /** Initialization. */ |
| tbjazic | 1:22043b67c31c | 36 | void init(); |
| tbjazic | 1:22043b67c31c | 37 | |
| tbjazic | 1:22043b67c31c | 38 | /** Start the measurement. */ |
| tbjazic | 1:22043b67c31c | 39 | void startMeasurement(); |
| tbjazic | 1:22043b67c31c | 40 | }; |
| tbjazic | 1:22043b67c31c | 41 | |
| tbjazic | 1:22043b67c31c | 42 | |
| tbjazic | 1:22043b67c31c | 43 | HCSR04::HCSR04(PinName echoPin, PinName triggerPin) : echo(echoPin), trigger(triggerPin) { |
| tbjazic | 1:22043b67c31c | 44 | init(); |
| tbjazic | 1:22043b67c31c | 45 | } |
| tbjazic | 1:22043b67c31c | 46 | |
| tbjazic | 1:22043b67c31c | 47 | void HCSR04::init() { |
| tbjazic | 1:22043b67c31c | 48 | /** configure the rising edge to start the timer */ |
| tbjazic | 1:22043b67c31c | 49 | echo.rise(this, &HCSR04::startTimer); |
| tbjazic | 1:22043b67c31c | 50 | |
| tbjazic | 1:22043b67c31c | 51 | /** configure the falling edge to stop the timer */ |
| tbjazic | 1:22043b67c31c | 52 | echo.fall(this, &HCSR04::stopTimer); |
| tbjazic | 1:22043b67c31c | 53 | |
| tbjazic | 1:22043b67c31c | 54 | distance = -1; // initial distance |
| tbjazic | 1:22043b67c31c | 55 | } |
| tbjazic | 1:22043b67c31c | 56 | |
| tbjazic | 1:22043b67c31c | 57 | void HCSR04::startTimer() { |
| tbjazic | 0:ebee649c5b1b | 58 | timer.start(); // start the timer |
| tbjazic | 0:ebee649c5b1b | 59 | } |
| tbjazic | 0:ebee649c5b1b | 60 | |
| tbjazic | 1:22043b67c31c | 61 | void HCSR04::stopTimer() { |
| tbjazic | 0:ebee649c5b1b | 62 | timer.stop(); // stop the timer |
| tbjazic | 0:ebee649c5b1b | 63 | } |
| tbjazic | 0:ebee649c5b1b | 64 | |
| tbjazic | 1:22043b67c31c | 65 | void HCSR04::startMeasurement() { |
| tbjazic | 1:22043b67c31c | 66 | /** Start the measurement by sending the 10us trigger pulse. */ |
| tbjazic | 1:22043b67c31c | 67 | trigger = 1; |
| tbjazic | 1:22043b67c31c | 68 | wait_us(10); |
| tbjazic | 1:22043b67c31c | 69 | trigger = 0; |
| tbjazic | 1:22043b67c31c | 70 | |
| tbjazic | 1:22043b67c31c | 71 | /** Wait for the sensor to finish measurement (generate rise and fall interrupts). |
| tbjazic | 1:22043b67c31c | 72 | * Minimum wait time is determined by maximum measurement distance of 400 cm. |
| tbjazic | 1:22043b67c31c | 73 | * t_min = 400 * 58 = 23200 us = 23.2 ms */ |
| tbjazic | 1:22043b67c31c | 74 | wait_ms(25); |
| tbjazic | 1:22043b67c31c | 75 | |
| tbjazic | 1:22043b67c31c | 76 | /** calculate the distance in cm */ |
| tbjazic | 1:22043b67c31c | 77 | distance = timer.read() * 1e6 / 58; |
| tbjazic | 1:22043b67c31c | 78 | timer.reset(); // reset the timer to 0 after storing the distance |
| tbjazic | 1:22043b67c31c | 79 | } |
| tbjazic | 1:22043b67c31c | 80 | |
| tbjazic | 1:22043b67c31c | 81 | float HCSR04::getDistance_cm() { |
| tbjazic | 1:22043b67c31c | 82 | startMeasurement(); |
| tbjazic | 1:22043b67c31c | 83 | return distance; |
| tbjazic | 1:22043b67c31c | 84 | } |
| tbjazic | 1:22043b67c31c | 85 | |
| tbjazic | 0:ebee649c5b1b | 86 | int main() { |
| tbjazic | 1:22043b67c31c | 87 | HCSR04 sensor(p5, p7); // instantiate the sensor object |
| tbjazic | 0:ebee649c5b1b | 88 | while(1) { |
| tbjazic | 1:22043b67c31c | 89 | /** Print the result in cm to the terminal with 1 decimal place |
| tbjazic | 0:ebee649c5b1b | 90 | * (number 5 after % means that total of 5 digits will be reserved |
| tbjazic | 0:ebee649c5b1b | 91 | * for printing the number, including the dot and one decimal place). */ |
| tbjazic | 1:22043b67c31c | 92 | pc.printf("Distance: %5.1f cm\r", sensor.getDistance_cm()); |
| tbjazic | 1:22043b67c31c | 93 | wait_ms(1000-25); |
| tbjazic | 0:ebee649c5b1b | 94 | } |
| tbjazic | 0:ebee649c5b1b | 95 | } |
