I have a conversion routine that takes a measurement, and converts it to a part number. This works wonderfully. The requirement has come up that the powers that be want three measurements taken, and the average of the three gets converted to a part number.
I have a button that's get pushed each time a measurement needs to be taken. Push it once, and the current measurement (in mm) gets saved to a variable. Push it again, and the current measurement (which may have changed) gets saved in a second variable. Push it a third time, and the third measurement is saved in a third variable, the three variables are averaged, and the resulting averaged measurement is submitted for conversion.
Push the button a fourth time and the variables are cleared and the process starts again.
The conversion routine gets entered anew for many times a second, as the user is manipulating a gear in a micrometer, and decides when to "set" a given measurement as one of the official "samples". So the part numbers being sent back have to continue regardless of if the button was pushed or not.
here is the working conversion routine:
int convert() {
mm = (float)num/1000;
float formula = round((((maxmm-(range/2.000))-mm)/jump));
int f;
f = (int)formula;
int lindy = maxl-f;
return lindy;