Challenge

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

#define size 5 //limit size of average values
const int pin =5; //taking pin 5 for operation
int duration; //duration variable for reading pulses
int a[size], b[size]; //a[] for taking inch values and b[] for taking centimeter values
int sum=0, total=0; //variables for storing added values
int count=0; //count for counting the loop iteration
int check = size; //for comparing the count value with check
void setup() //initialize function
{
Serial.begin(9600); //beginning serial monitor @9600
}
void loop() //function for changes in arduino
{
int inch, cm; //to store values for inches and centimeters
int value, average; //to store average values
int avg, val; //to store average values
pinMode(pin, OUTPUT); //setting pin as output
digitalWrite(pin, LOW); //making pin as low
delayMicroseconds(2); //delaying 2 ms
digitalWrite(pin, HIGH); //making pin as high(for sending the signal)
delayMicroseconds(5); //delaying for 5 ms
digitalWrite(pin, LOW); //making pin as low(for getting back the signals)
pinMode(pin, INPUT); //making pin as input after sending and receiving
duration = pulseIn(pin, HIGH); //storing read values in duration
inch = Toinch(duration); //storing values in which the duration is converted into inches by callinf function
cm = Tocm(duration);//storing values in which the duration is converted into cm by calling function
for(int i=0;i<size;i++) //iterating first loop for storing moving values
{
a[i] = inch; //values are stored in every step of iteration
value = inch; //storing that data in value
sum = sum+value; //adding all the values
}
for(int i=0;i<size;i++) //iterating second loop for storing moving values
{
b[i] = cm; //values are stored in every step of iteration
val = cm; //storing that data in value
total = total+ val; //adding all the values
}
Serial.print(value);//o/p statement
Serial.print(" inch");//o/p statement
Serial.print(", ");//o/p statement
Serial.print(cm);//o/p statement
Serial.print(" cm");//o/p statement
Serial.println();//o/p statement
count++; //incrementing count for counting iteration of loop
if(count%check==0) //checking if count == 5 or 10 and so on series...
{
average = sum/size; //for storing average value
Serial.print("Average values are: ");//o/p statement
Serial.print(average/5);//o/p statement
Serial.print(" & ");//o/p statement
sum = 0*sum; //making sum as zero to add the values again in next steps
avg = total/5;//for storing average value
Serial.print(avg/5);//o/p statement
total=0*total;//making total as zero to add the values again in next steps
Serial.println();//o/p statement
}
delay(2000); //delaying for 2 ms

}
int Toinch(int num) //called function for converting the duration into inches
{
return num/74/2; //return value for inches
}
int Tocm(int var)//called function for converting the duration into cm
{
return var/29/2;//return value for 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.