Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter
Q: In this project the implementation of code for “Measuring distance of an object using ultrasonic distance sensor (28015 Ping) and also smoothen the sensor data using moving average filter”.
A:
COMPONENTS:
<!-- [if !supportLists]-->1. <!--[endif]-->ARDUINO UNO
<!-- [if !supportLists]-->2. <!--[endif]-->ULTRASONIC DISTANCE SENSOR 28015 PING
<!-- [if !supportLists]-->3. <!--[endif]-->JUMPER WIRES
CONNECTIONS:
<!-- [if !supportLists]-->1. <!--[endif]-->SENSOR 5V PIN IS CONNECTED TO THE 5V OF ARDUINO
<!-- [if !supportLists]-->2. <!--[endif]-->SENSOR GND PIN IS CONNECTED TO THE GND OF ARDUINO
<!-- [if !supportLists]-->3. <!--[endif]-->SENSOR SIG PIN IS CONNECTED TO THE DIGITAL PIN 9
CODE:
#define WINDOW_SIZE 7
int inches = 0;
int cm = 0;
int INDEX = 0;
int VALUE = 0;
int SUM = 0;
int READINGS[WINDOW_SIZE] = {0}; // Initialize READINGS array
int AVERAGED = 0;
long readUltrasonicDistance(int triggerPin, int echoPin) {
pinMode(triggerPin, OUTPUT);
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
return pulseIn(echoPin, HIGH);
}
void setup() {
Serial.begin(9600);
}
void loop() {
cm = 0.01723 * readUltrasonicDistance(9, 9);
inches = cm / 2.54;
SUM = SUM - READINGS[INDEX];
VALUE = inches;
READINGS[INDEX] = VALUE;
SUM = SUM + VALUE;
INDEX = (INDEX + 1) % WINDOW_SIZE;
AVERAGED = SUM / WINDOW_SIZE;
Serial.print("The distance is: ");
Serial.println(inches);
Serial.print("Filtered data is: ");
Serial.println(AVERAGED * 100);
delay(100);
}
EXPLANATION:
- Global Variables:
- WINDOW_SIZE: This constant defines the number of readings to be averaged.
- inches and cm: Variables to store the distance measurement in inches and centimeters.
- INDEX: An index to keep track of the current position in the READINGS array.
- VALUE: Stores the most recent distance value.
- SUM: Keeps a running sum of the values in the READINGS array.
- READINGS: An array used to store the last WINDOW_SIZE distance measurements for averaging.
- AVERAGED: Stores the averaged distance value.
- readUltrasonicDistance Function:
- This function is used to trigger the ultrasonic sensor, measure the echo time, and calculate the distance in centimeters based on the measured time. It takes two arguments: triggerPin and echoPin, which are used to connect the trigger and echo pins of the sensor.
- setup Function:
- This function is called once at the start of the program. It initializes the serial communication with a baud rate of 9600, which is used for debugging and printing output to the serial monitor.
- loop Function:
- The loop function is where the main functionality of the program occurs, and it runs continuously.
- It first triggers the ultrasonic sensor to measure the distance in centimeters (cm) using the readUltrasonicDistance function.
- It then converts the distance to inches and stores it in the inches variable.
- It updates the running sum SUM by subtracting the oldest reading from the READINGS array and adding the new VALUE.
- It updates the READINGS array at the current INDEX with the most recent VALUE.
- It calculates the averaged distance value (AVERAGED) by dividing the SUM by WINDOW_SIZE.
- It prints the current distance in inches and the averaged distance (multiplied by 100) to the serial monitor.
- It then adds a delay of 100 milliseconds to control the rate at which readings are taken.