Mutila: Mouse's Utilities for Arduino
Oft-used utilities: debouncing buttons, averaging samples, and so on.
|
#include <SonicRanger.h>
Public Member Functions | |
SonicRanger (const uint8_t trigPin, const uint8_t echoPin) | |
void | begin () |
uint16_t | getRange () |
void | setMaxRange (uint16_t cm) |
void | setTimeoutMs (uint16_t ms) |
Static Public Attributes | |
static const uint16_t | DefaultMaxRangeCm = 200 |
static const uint8_t | DefaultTimeoutMs = 15 |
HC-SR04 Ultrasonic ranger device
SonicRanger provides a simple wrapper around the calculation necessary to determine the range of an object using a HC-SR04 ultrasonic range finder.
Definition at line 17 of file SonicRanger.h.
SonicRanger::SonicRanger | ( | const uint8_t | trigPin, |
const uint8_t | echoPin | ||
) |
Constructor.
trigPin | the arduino pin conneced to the TRIG pin of the HC-SR04 device. |
echoPin | the arduino pin conneced to the ECHO pin of the HC-SR04 device. |
Definition at line 41 of file SonicRanger.cpp.
void SonicRanger::begin | ( | ) |
Initialization.
Typically called from setup().
Definition at line 49 of file SonicRanger.cpp.
uint16_t SonicRanger::getRange | ( | ) |
Get range in cm.
NOTE: execution time depends on the range to an object. Of no object is in range, the ranging will time out after 15ms. The maximum range and timeout values can be set using setMaxRange() and setTimeoutMs().
Range=10cm, execution time ~1 ms Range=50cm, execution time ~4 ms Range=100cm, execution time ~7 ms Range=150cm, execution time ~10 ms Range=>200cm, execution time ~15 ms
Definition at line 55 of file SonicRanger.cpp.
|
inline |
Set maximum range
cm | the new maximum range in cm. |
Definition at line 57 of file SonicRanger.h.
|
inline |
Set timeout in ms.
ms | the new timeout in milliseconds. |
Note that you need to allow about 7 ms for every 100 cm of range you wish to be able to measure. Typically HC-SR04 units can only reliably measure ranges of up a couple of meters.
Definition at line 68 of file SonicRanger.h.