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 PRO1 by
HCSR04 Class Reference
A distance measurement class using ultrasonic sensor HC-SR04. More...
#include <HCSR04.h>
Public Member Functions | |
HCSR04 (PinName echoPin, PinName triggerPin) | |
Receives two PinName variables. | |
float | getDistance_cm () |
Calculates the distance in cm, with the calculation time of approximatelly 23.7 ms. | |
float | getDistance_mm () |
Calculates the distance in mm, with the calculation time of approximatelly 23.7 ms. | |
void | setRanges (float minRange, float maxRange) |
Sets the minimum and maximum ranges between the factory values of 2 cm and 400 cm. | |
float | getMinRange () |
Retreives the minimum sensor range set by the user. | |
float | getMaxRange () |
Retreives the maximum sensor range set by the user. |
Detailed Description
A distance measurement class using ultrasonic sensor HC-SR04.
Example of use:
#include "mbed.h" #include "HCSR04.h" Serial pc(USBTX, USBRX); int main() { HCSR04 sensor(p5, p7); sensor.setRanges(10, 110); pc.printf("Min. range = %g cm\n\rMax. range = %g cm\n\r", sensor.getMinRange(), sensor.getMaxRange()); while(1) { pc.printf("Distance: %5.1f mm\r", sensor.getDistance_mm()); wait_ms(500); } }
Definition at line 25 of file HCSR04.h.
Constructor & Destructor Documentation
HCSR04 | ( | PinName | echoPin, |
PinName | triggerPin | ||
) |
Receives two PinName variables.
- Parameters:
-
echoPin mbed pin to which the echo signal is connected to triggerPin mbed pin to which the trigger signal is connected to
Definition at line 4 of file HCSR04.cpp.
Member Function Documentation
float getDistance_cm | ( | ) |
Calculates the distance in cm, with the calculation time of approximatelly 23.7 ms.
- Returns:
- distance of the measuring object in cm.
Definition at line 42 of file HCSR04.cpp.
float getDistance_mm | ( | ) |
Calculates the distance in mm, with the calculation time of approximatelly 23.7 ms.
- Returns:
- distance of the measuring object in mm.
Definition at line 47 of file HCSR04.cpp.
float getMaxRange | ( | ) |
Retreives the maximum sensor range set by the user.
- Returns:
- the maximum sensor range set by the user in cm.
Definition at line 65 of file HCSR04.cpp.
float getMinRange | ( | ) |
Retreives the minimum sensor range set by the user.
- Returns:
- the minimum sensor range set by the user in cm.
Definition at line 61 of file HCSR04.cpp.
void setRanges | ( | float | minRange, |
float | maxRange | ||
) |
Sets the minimum and maximum ranges between the factory values of 2 cm and 400 cm.
- Parameters:
-
minRange Minimum range in cm. Must be between 2 cm and maxRange. maxRange Maximum range in cm. Must be between minRange and 400 cm.
Definition at line 52 of file HCSR04.cpp.
Generated on Tue Jul 12 2022 22:02:46 by
