Skip to main content
Editing Step 7 —

Step Type:

Drag to rearrange

This loop averages the calculated distances together to make one average distance between the sensor and the object. It is another way to clean up the data and make it more organized. A loop means that it does the action continuously, so it will constantly average the numbers to get clean data.

distance1= getdistance(trigPin, echoPin); //function to get distance using the variables, trigPin and echoPin

oldvalue-distance1; //this makes the first distance recorded the old value when averaging

for(int average=0; average<10; average++) //this loop means that it will record the distance and average the old and new distances together 10 times and then reset and start all over

{distance1=getdistance(trigPin, echoPin); newvalue=distance1; //after the next value is recorded, that distance is viewed as the second value when used for averaging

oldvalue=(oldvalue+newvalue)/2; //this is the formula to average numbers together and it equals the old value because the average is added to a new value afterwards to make another average (a cycle) and so on

Your contributions are licensed under the open source Creative Commons license.