I'm a bit stuck with some code that I'm busy with. I'm trying to write a conversion protocol to convert units of measurement. This is for an ultrasonic range finder.
As such I have implemented a case switch to select the unit of measurement. Inside the case switch I'm also adjusting a conversion factor to the appropriate value.
then a big menu structure and inside the menu structure I call my Units_of_Measurement function. In this function the conversion factory is then changed to the appropriate value.
Back in the main void loop the newly applied multiplication factor is applied to the measured value. It is also applied to the empty and the full calibration as it effects these values as well.
Problem is that when I multiply my Empty and my Full calibrations with my conversion factor, it results in a zero value for both and I don't understand why? Any advise?
I've also tried forcing it to a float with the following statements:
Have you tried putting some debug print statements in, to check the values before you do the multiplications?
case 6:
Unit_of_measurement = "ft";
Unit_conversion_factor = 0.00328084;
break;
}
// Make sure Serial.begin() has been called in setup()
Serial.print("Empty_Cal: ");
Serial.println(Empty_Cal);
// Same for Full_Cal and Unit_conversion_factor
Empty_Cal = Empty_Cal * Unit_conversion_factor;
Full_Cal = Full_Cal * Unit_conversion_factor;
Don't apply the conversion factor to the calibration numbers in the Units_of_Measurement function. Each time you call this function your calibration numbers get smaller. Apply the conversation factor to the calibration numbers in the expression where the measured value is calculated. Like this...
I'm using a GLCD so I can see the values before and after. Before it is good, it is only after I change the conversion factor that the values goes haywire.
@ DavidOConnor
Hey David
Thanks for the input, this seems to have solved it. I've changed the statement to:
But this only converts the Empty_Cal value and I need to convert the Full_Cal value as well as I'm using the Measured_Value and the Full_Cal value to derive a percentage value as well to draw a percentage bar-graph representation as well.
I can't convert the full value in the main loop as then it will loop constantly and get smaller and smaller as you've mentioned. But where I have it, it is outside of the main loop and is only run when I access the units of measurement function. Run once since it is a case switch statement.
I believe that I'm only going through the above piece of code once, when I access the switch case as mentioned. But maybe I'm wrong, if so, any ideas how I can execute this statement only once after a change of unit of measurement have been applied? Or is there maybe something else that I'm missing.
I'm using a GLCD so I can see the values before and after. Before it is good, it is only after I change the conversion factor that the values goes haywire.
Sounds more like a problem in what you are displaying or the way you are doing it.
But this only converts the Empty_Cal value and I need to convert the Full_Cal value as well as I'm using the Measured_Value and the Full_Cal value to derive a percentage value as well to draw a percentage bar-graph representation as well.
Apply the conversion factor in the expression that calculates the percentage value, rather than trying to change the FullCal value.
Thanks for all the inputs. I've solved the issue of runing the conversion factor on the empty and full calibrations only once with the following piece of code:
But yes, as dlloyd said, I'll have to declare a few more floats otherwise I'm manipulating my actual measurement, i.e. when I want to go from mm to cm and then to m then the calculation is stuffed. I'll post the code once I'm done.