CrossGuard
Loading...
Searching...
No Matches
Public Member Functions | List of all members
UltrasonicSensor Class Reference

Provides functionality for interfacing with an ultrasonic sensor for distance measurement. More...

#include <UltrasonicSensor.h>

Public Member Functions

 UltrasonicSensor (int triggerPin, int echoPin)
 Constructs an UltrasonicSensor with specified GPIO pins.
 
 ~UltrasonicSensor ()
 Destructs the UltrasonicSensor instance.
 
void initialize ()
 Initializes the sensor and prepares it for distance measurements.
 
float calculateDistance ()
 Calculates the distance to an object in front of the sensor.
 
bool isMotionDetected (float distanceThreshold=5.0f)
 Checks for motion detection based on changes in measured distance.
 

Detailed Description

Provides functionality for interfacing with an ultrasonic sensor for distance measurement.

This class handles initialization, distance measurement, and motion detection using an ultrasonic sensor connected to specified GPIO pins on a Raspberry Pi. It encapsulates details of sending pulses and measuring response times to calculate distances.

Constructor & Destructor Documentation

◆ UltrasonicSensor()

UltrasonicSensor::UltrasonicSensor ( int triggerPin,
int echoPin )

Constructs an UltrasonicSensor with specified GPIO pins.

Initializes an UltrasonicSensor and starts its initialization.

Parameters
triggerPinThe GPIO pin used to trigger the sensor.
echoPinThe GPIO pin used to receive the echo signal from the sensor.
triggerPinGPIO pin used to trigger the sensor.
echoPinGPIO pin used to receive the echo signal.

◆ ~UltrasonicSensor()

UltrasonicSensor::~UltrasonicSensor ( )

Destructs the UltrasonicSensor instance.

Cleans up GPIO resources.

Ensures proper shutdown of the GPIO functionality, primarily used on systems where GPIO pins require manual cleanup.

Member Function Documentation

◆ calculateDistance()

float UltrasonicSensor::calculateDistance ( )

Calculates the distance to an object in front of the sensor.

Measures the distance by timing how long it takes for an echo to return.

Returns
The measured distance in centimeters.

This method triggers the sensor, waits for the echo, and calculates the distance based on the time delay between sending the pulse and receiving the echo.

Returns
Distance to the nearest object in centimeters.

◆ initialize()

void UltrasonicSensor::initialize ( )

Initializes the sensor and prepares it for distance measurements.

Sets up the sensor hardware and prepares it for operation.

Sets the GPIO modes for the trigger and echo pins, and registers the echo reception callback.

Initializes the GPIO library, sets the GPIO modes for the pins, and registers the callback function for echo detection.

◆ isMotionDetected()

bool UltrasonicSensor::isMotionDetected ( float distanceThreshold = 5.0f)

Checks for motion detection based on changes in measured distance.

Detects motion based on changes in measured distances.

Parameters
distanceThresholdThe minimum change in distance to consider as motion (default is 5.0 cm).
Returns
True if motion is detected, false otherwise.

Measures the current distance and compares it to the previous measurement. If the change exceeds the specified threshold and is within a practical range, motion is considered detected.

Parameters
distanceThresholdMinimum distance change to qualify as motion.
Returns
True if motion is detected, otherwise false.

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