Challenge

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

// C++ code
//
/*Distance= (travel time/2) * speed of sound
Where speed of sound around 340m per second.
So distance travel in one microsecond is
=340 / 1000000
= 0.00034m/microsecond
=0.00034 X 100 cm/microsecond
=0.034cm / microsecond
Distance= (travel time/2) * speed of sound
=we divide it by 2
=hence 0.017 cm/microsecond
Now input receive by ultrasonic sensor will have time duration in microsecond
If we multiply by this number we directly will get distance in cm. to convert it in inch again value will be diveide by 2.54cm.
*/


#include<LiquidCrystal.h>
//library for Liquid Crystal

#define WINDOW_SIZE 5
//window size for filter

int trigger;
//for sending data pin 7 to assigning variable name trigger

int echopin;
//same pin variable name is echopin

float CM;
//

float INCH ;
//

float inch;

float average=0;
float sum;
int index;

int reading[WINDOW_SIZE];

LiquidCrystal LCD(12,11,5,4,3,2);
void setup()
{
//LiquudCrystal LCD(RS,E,D4,D5,D6,D7);

LCD.begin(16,2);
// filter initializing

LCD.setCursor(0,0);

LCD.print(">>DISTANCE<<");
//LCD intial set um for distance measurement
LCD.setCursor(0,1);
LCD.print("MEASUREMENT");
delay(2000);
LCD.clear();
Serial.begin(9600);

}

void loop()
{
sum=sum-reading[index];
CM = 0.01723 * readUltrasonicDistance(7, 7);
//calling the function to send receive microseconds

reading[index]=CM;
//index will change from 0 to 5

sum=sum+reading[index];
index=(index+1)%WINDOW_SIZE;
//to increment index

average=sum/WINDOW_SIZE;
//taking average of the receive value

INCH = (CM/ 2.54);
inch=(average/2.54);
// converting it in inch

delay(10);

/* LCD.setCursor(0,0);
LCD.print(">>DISTANCE<<");
LCD.setCursor(0,1);
LCD.print(average);
LCD.setCursor(5,1);
LCD.print("CM");
delay(1000);
LCD.clear();*/

// diplaying on LCD measureement
LCD.print(">>DISTANCE<<");
LCD.setCursor(2,1);
LCD.print(inch);
LCD.setCursor(8,1);
LCD.print("INCH");
delay(200);
LCD.clear();

Serial.print(INCH);
Serial.print(" inch witout filter \n");
Serial.print(inch);
Serial.print(" inch with average filter, \n");

/* Serial.print(CM);
Serial.println("cm");
Serial.print(average);
Serial.print("filter cm, ");
delay(100); // Wait for 100 millisecond(s)*/
}

//called function
long readUltrasonicDistance(int trigger, int echopin)
{

pinMode(trigger,OUTPUT);
//initial is pin sevn is high

digitalWrite(trigger,LOW);
delay(2);
//sending low pulse initially

digitalWrite(trigger,HIGH);
delay(10);
//pulse will remain high for 10milis

digitalWrite(trigger,LOW);
//again low so it will get pulse of 10millis

pinMode(echopin,INPUT);

return pulseIn(echopin, HIGH);
//pulseIn function checks status of echopin is it goes from low to high or not returns the value
}

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.