How to Arduino
View
Options
Preview new Authoring Experience
History
1
2
3
4
5
6
7
8
9
Introduction
Details
Guide Steps
Review
Quiz
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
« Previous
Next »
Your contributions are licensed under the open source Creative Commons license.
Next line
Previous line
Insert a new line below
Insert a new line above
Indent current line
Unindent current line
Delete current line
Navigate to next step.
Navigate to previous step.