Challenge

Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter

PROGRAM:

#include <iostream>

#include <vector>

#include <chrono>

#include <thread>

#include <numeric> // For std::accumulate

using namespace std;

// Simulated function to represent the time it takes for the echo to return (in microseconds).

long simulateUltrasonicPulse() {

// Simulating a delay (in microseconds) for the ultrasonic pulse to travel and return

// In a real scenario, this would come from pulseIn() or similar functions.

return rand() % 10000 + 5000; // Random value between 5000 and 15000 microseconds

}

// Function to calculate the distance from the echo pulse duration

double calculateDistance(long duration) {

const double SPEED_OF_SOUND = 0.0344; // Speed of sound in cm/µs

return (duration * SPEED_OF_SOUND) / 2; // Return distance in cm

}

// Function to compute the moving average

double movingAverage(const vector<double>& data) {

double sum = accumulate(data.begin(), data.end(), 0.0);

return sum / data.size();

}

int main() {

const int WINDOW_SIZE = 5; // Size of the moving average window

vector<double> distanceData(WINDOW_SIZE, 0.0); // Circular buffer to store distances

int currentIndex = 0;

long duration;

double distance, avgDistance;

// Seed for random number generator

srand(time(0));

// Main loop to simulate continuous measurement

while (true) {

// Simulate reading the ultrasonic sensor pulse duration

duration = simulateUltrasonicPulse();

// Calculate the distance from the pulse duration

distance = calculateDistance(duration);

// Update the distance array (circular buffer)

distanceData[currentIndex] = distance;

// Calculate the moving average of the last 'n' measurements

avgDistance = movingAverage(distanceData);

// Print the raw and smoothed distances

cout << "Distance: " << distance << " cm, Moving Average: " << avgDistance << " cm" << endl;

// Move to the next index (circular buffer)

currentIndex = (currentIndex + 1) % WINDOW_SIZE;

// Sleep for a short time before taking the next measurement

std::this_thread::sleep_for(std::chrono::milliseconds(500));

}

return 0;

}

OUTPUT:

Distance: 216.565 cm, Moving Average: 43.313 cm

Distance: 116.203 cm, Moving Average: 66.5537 cm

Distance: 140.214 cm, Moving Average: 94.5966 cm

Distance: 197.25 cm, Moving Average: 134.046 cm

Distance: 256.469 cm, Moving Average: 185.34 cm

Distance: 94.6172 cm, Moving Average: 160.951 cm

Distance: 249.538 cm, Moving Average: 187.618 cm

Distance: 237.429 cm, Moving Average: 207.06 cm

Distance: 156.193 cm, Moving Average: 198.849 cm

Distance: 102.134 cm, Moving Average: 167.982 cm

Distance: 166.909 cm, Moving Average: 182.44 cm

Distance: 163.228 cm, Moving Average: 165.178 cm

Distance: 137.153 cm, Moving Average: 145.123 cm

Distance: 153.734 cm, Moving Average: 144.631 cm

Distance: 143.517 cm, Moving Average: 152.908 cm

Distance: 256.779 cm, Moving Average: 170.882 cm

Distance: 87.7028 cm, Moving Average: 155.777 cm

Distance: 149.743 cm, Moving Average: 158.295 cm

Distance: 186.517 cm, Moving Average: 164.852 cm

Distance: 140.042 cm, Moving Average: 164.157 cm

Distance: 111.336 cm, Moving Average: 135.068 cm

Distance: 207.638 cm, Moving Average: 159.055 cm

Distance: 114.088 cm, Moving Average: 151.924 cm

Distance: 229.121 cm, Moving Average: 160.445 cm

Distance: 156.52 cm, Moving Average: 163.741 cm

___________________________________________________________________________________


Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.