Mutila: Mouse's Utilities for Arduino
Oft-used utilities: debouncing buttons, averaging samples, and so on.
Public Member Functions | Static Public Attributes | List of all members
SonicRanger Class Reference

#include <SonicRanger.h>

Inheritance diagram for SonicRanger:
Inheritance graph

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
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ SonicRanger()

SonicRanger::SonicRanger ( const uint8_t  trigPin,
const uint8_t  echoPin 
)

Constructor.

Parameters
trigPinthe arduino pin conneced to the TRIG pin of the HC-SR04 device.
echoPinthe arduino pin conneced to the ECHO pin of the HC-SR04 device.

Definition at line 41 of file SonicRanger.cpp.

Member Function Documentation

◆ begin()

void SonicRanger::begin ( )

Initialization.

Typically called from setup().

Definition at line 49 of file SonicRanger.cpp.

Here is the caller graph for this function:

◆ getRange()

uint16_t SonicRanger::getRange ( )

Get range in cm.

Returns
the range of the nearest object to the HC-SR04 device in cm (approximate). If no object is in range, some the maximum range (200 cm) will be returned.

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.

Here is the caller graph for this function:

◆ setMaxRange()

void SonicRanger::setMaxRange ( uint16_t  cm)
inline

Set maximum range

Parameters
cmthe new maximum range in cm.

Definition at line 57 of file SonicRanger.h.

◆ setTimeoutMs()

void SonicRanger::setTimeoutMs ( uint16_t  ms)
inline

Set timeout in ms.

Parameters
msthe 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.


The documentation for this class was generated from the following files: