Decimal Precision and Strain Gauges

Hi guys,

I think this is quite simple, but I am stuck and needed some assistance.

Currently I am using the HX711 library for a strain gauge.

https://www.sparkfun.com/products/13879

https://github.com/bogde/HX711

My problem is that Serial.println(scale.get_units(), 4); grabs the value with 4 decimal places. This works great.

But I need to store the scale.get_units value as a floating point digit. How would I store this value efficiently?

I tried Serial.available() and using the command value = scale.get_units(); but this only stores 2 decimal places.

Hopefully there is a way to grab what the serial buffer has in it or simply store the scale object value in a variable.

I can post a longer segment of code, but all the examples in the HX711 library never store the value to a variable.

-NB

But I need to store the scale.get_units value as a floating point digit. How would I store this value efficiently?

float x = scale.get_units(); ?

This does work, but only returns two decimal places.

While Serial.print(scale.get_units(),4); returns 4 places.

I need 4 decimal places.

Would fixed point math work?

The "4" in Serial.print(something,4) specifies the number of digits after the decimal point to display. You can change that.

The [u]precision[/u] of a floating point number is 6-7 digits total.

Nolebrain: This does work, but only returns two decimal places.

Don't be silly - if it "works" here Serial.println(scale.get_units(), 4);, then

float x = scale.get_units();
Serial.println(x, 4);

"works" just as well.

Okay AWOL good call!

I assumed it was getting cut short when storing it as a float value, but really it was just not displaying the full value when printing it.

Thank you so much!! :)

If you want all the precision there is to be had, use:

long read();

or

long read_average(byte times = 10);

and do all your math in 32-bit integers. But, you’ll have to take care of offset, tare, and conversion to real-world units yourself.