Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter
Aim:-
To Measuring the distance in terms of inches of an object using Ultrasonic Distance Sensor (28015 Ping) and also smoothen the sensor data using moving average filter in tinkercad platform.
Materials required :-
1. Ultrasonic Distance Sensor (28015 Ping)
2. Arduino Uno control
3. Breadboard
Code:-
int inches = 0;
int cm = 0;
long sum=0; // Initializing the all variables related to measuring distance
long avg;
int i;
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
void setup()
{
Serial.begin(9600);
}
void loop()
{
// measure the ping time in cm
cm = 0.01723 * readUltrasonicDistance(7, 7);
// convert to inches by dividing by 2.54
inches = (cm / 2.54);
for (i=0; i<5; i++)
{
sum=sum+inches;
avg = sum/5; // find out average for every 5 distance measurements
}
Serial.print(cm);
Serial.println("cm");
delay(1000); // Wait for 1000 millisecond(s)
Serial.print(inches);
Serial.print("inch");
delay(1000); // Wait for 1000 millisecond(s)
Serial.print("Avg:");
Serial.print(avg);
delay(1000); // Wait for1000 millisecond(s)
}