if(distance<40) { if(TCNT1>156 && p<6) //0.01s when used with 1024 prescaler { sv[p]=distance; p++; TCNT1=0; //resetting the timer and incrementing array position } else if((sv[1]<40 && sv[1]!=0) && (sv[2]<40 && sv[2]!=0) && (sv[3]<40 && sv[3]!=0) && (sv[4]<40 && sv[4]!=0) && (sv[5]<40 && sv[5]!=0)) { n=1; //indicating distance is lesser than 40 TCNT1=0; sv[0]=0; sv[1]=0; sv[2]=0; sv[3]=0; sv[4]=0; sv[5]=0; //reseting the array and timer p=0; } if(p==6) p=0; }
I am using this code to remove random errors given by a long distance sensor. Although this code does work i want to ensure if its actually checking the errors or is it coming to be true at random?
I check whether the distance has been lesser than a specified value for atleast 5 times, checked every 0.01 seconds. If it is, I assume that the value is actually lesser than the specified value.
The last condition is not needed when using only this code, but has been added due to certain other parts of the entire code.