Challenge

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

off line batch chennai

 PROGRAM:

//Initialize the variables

long duration,cm,inches;

void setup()

{

//Serial begining 
Serial.begin (9600);
}

void loop()
{
double x = Mov_Avg();
double inches = MicrosecondsToinches(x);
double cm = MicrosecondsTocm(x);
double avg = MicrosecondsTocm(x);
Serial.print("\n\nINCHES is : ");
Serial.print(inches);
Serial.print("\nCM is : ");
Serial.print(cm);


delay(1000);
}

//Transferring the signal
void US_TX()
{
pinMode(7,OUTPUT);
digitalWrite(7,LOW);
delayMicroseconds(2);
digitalWrite(7,HIGH);
delayMicroseconds(5);
digitalWrite(7,LOW);
delayMicroseconds(2);
}

//Receiving the signal

long US_RX()
{
pinMode(7,INPUT);
duration = pulseIn(7,HIGH);
return duration;

}

//Convert the microseconds  to inches
long MicrosecondsToinches(long duration)
{
inches = duration/73.35/2;
}

//Convert the microseconds to cm
long MicrosecondsTocm(long duration)
{
cm = duration/29/2;
}


long Mov_Avg()
{
long windowsize = 5;
long data[windowsize];

US_TX();
data[0] = US_RX();
US_TX();
data[1] = US_RX();
US_TX();
data[2] = US_RX();
US_TX();
data[3] = US_RX();
US_TX();
data[4] = US_RX();
float filter_value = (data[0]+data[1]+data[2]+data[3]+data[4]);
double Avg= filter_value/5;
return Avg;
}

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.