CrossGuard
|
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. | |
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.
UltrasonicSensor::UltrasonicSensor | ( | int | triggerPin, |
int | echoPin ) |
Constructs an UltrasonicSensor with specified GPIO pins.
Initializes an UltrasonicSensor and starts its initialization.
triggerPin | The GPIO pin used to trigger the sensor. |
echoPin | The GPIO pin used to receive the echo signal from the sensor. |
triggerPin | GPIO pin used to trigger the sensor. |
echoPin | GPIO pin used to receive the echo signal. |
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.
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.
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.
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.
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.
distanceThreshold | The minimum change in distance to consider as motion (default is 5.0 cm). |
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.
distanceThreshold | Minimum distance change to qualify as motion. |