Smartcar Shield
Public Member Functions | List of all members
SR04 Class Reference

#include <SR04.hpp>

Inheritance diagram for SR04:
Inheritance graph
Collaboration diagram for SR04:
Collaboration graph

Public Member Functions

 SR04 (Runtime &runtime, uint8_t triggerPin, uint8_t echoPin, unsigned int maxDistance=smartcarlib::constants::sr04::kDefaultMaxDistance)
 Constructs an SR04 ultrasonic sensor. More...
 
unsigned int getDistance () override
 Gets the distance measured by the sensor in centimeters. More...
 
unsigned int getMedianDistance (uint8_t iterations=smartcarlib::constants::sr04::kDefaultIterations) override
 Gets the median distance from the specified number of measurements. More...
 
- Public Member Functions inherited from DistanceSensor
virtual ~DistanceSensor ()=default
 

Detailed Description

The SR04 (aka SRF05) is an inexpensive ultrasonic sensor controllable over two digital pins.

For a more advanced solution (faster readings, not using pulseIn, non-blocking measurements etc) please use the NewPing library.

Definition at line 29 of file SR04.hpp.

Constructor & Destructor Documentation

◆ SR04()

SR04::SR04 ( Runtime runtime,
uint8_t  triggerPin,
uint8_t  echoPin,
unsigned int  maxDistance = smartcarlib::constants::sr04::kDefaultMaxDistance 
)

Constructs an SR04 ultrasonic sensor.

Parameters
runtimeThe runtime environment you want to run the class for
triggerPinThe pin to produce the trigger signal
echoPinThe pin to receive the echo signal
maxDistanceThe maximum measurement distance in centimeters

Example:

unsigned short TRIGGER_PIN = 6;
unsigned short ECHO_PIN = 7;
ArduinoRuntime arduinoRuntime;
SR04 sr04(arduinoRuntime, TRIGGER_PIN, ECHO_PIN);

Definition at line 16 of file SR04.cpp.

Member Function Documentation

◆ getDistance()

unsigned int SR04::getDistance ( )
overridevirtual

Gets the distance measured by the sensor in centimeters.

Calling this might trigger a new measurement by the sensor.

Returns
0 if an error has occured, otherwise the measured distance in centimeters

Example:

unsigned int distance = sensor.getDistance();

Implements DistanceSensor.

Definition at line 40 of file SR04.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getMedianDistance()

unsigned int SR04::getMedianDistance ( uint8_t  iterations = smartcarlib::constants::sr04::kDefaultIterations)
overridevirtual

Gets the median distance from the specified number of measurements.

Parameters
iterationsNumber of measurements to conduct (at most kMaxMedianMeasurements)
Returns
The median of the conducted measurements or an error value if the number of iterations is 0 or larger than kMaxMedianMeasurements

Example:

// Get the median of `10` measurements
unsigned int distance = sensor.getMedianDistance(10);

Implements DistanceSensor.

Definition at line 61 of file SR04.cpp.

Here is the call graph for this function:

The documentation for this class was generated from the following files:
ArduinoRuntime
Definition: ArduinoRuntime.hpp:11
SR04
Definition: SR04.hpp:29