How can I better understand a library command e.g get_Units()

I'm trying to modify the stock code that reads a loadcell weight via a HX711.

I've got the HX711.h, the example code I have uses a line:

units = scale.get_units(), 10;

how do I find out more about the get_units command? (I would like to know what the 10 refers to but also for the future know how to look up thin information thanks)

1 Like

Aaaaargh_rduino:
I've got the HX711.h, the example code I have uses a line:

units = scale.get_units(), 10;

Better double check that. I definitely doesn't do what you think it does. Try printing the resultant value of 'units'.

how do I find out more about the get_units command? (I would like to know what the 10 refers to but also for the future know how to look up thin information thanks)

Look in HX711.h and HX711.cpp.

long read_average(byte times = 10); // Average 'times' raw readings
double get_value(byte times = 1); // return read_average(times) - OFFSET
float get_units(byte times = 1); // return get_value(times) / SCALE;
void tare(byte times = 10); // OFFSET = read_average(times);

So, get_units() returns (read_average(1) - OFFSET) / SCALE;

If you really want to understand that line, you should research the comma operator:
https://en.cppreference.com/w/cpp/language/operator_other

In the end, all you really need to know is that it's wrong.

units = scale.get_units(), 10;

Since it uses the comma operator, this is a way of saying:

scale.get_units();
units = 10;

Note that the value returned by "scale.get_units()" is thrown away.

I expect what they intended to write was:

units = scale.get_units(10);

This averages 10 raw readings, subtracts the current OFFSET, divides by the current SCALE factor and saves the result in 'units'.

John,

The code works (though I'm not sure how) - it reads the measurement scale value though i've got no idea at what rate or averaging etc

Since it uses the comma operator, this is a way of saying:
Code: [Select]
scale.get_units();
units = 10;

Note that the value returned by "scale.get_units()" is thrown away.

If the value were thrown away how is it reading the HX711 for the scale value?

gfvalvo:
Better double check that. I definitely doesn't do what you think it does. Try printing the resultant value of 'units'.
Look in HX711.h and HX711.cpp.

The whole code is below - it does work... not sure how though?

/*
Setup your scale and start the sketch WITHOUT a weight on the scale
Once readings are displayed place the weight on the scale
Press +/- or a/z to adjust the calibration_factor until the output readings match the known weight
Arduino pin 6 -> HX711 CLK
Arduino pin 5 -> HX711 DOUT
Arduino pin 5V -> HX711 VCC
Arduino pin GND -> HX711 GND
*/

#include "HX711.h"

HX711 scale(5, 6);

float calibration_factor = 2230; // this calibration factor is adjusted according to my load cell
float units;
float ounces;

void setup() {
Serial.begin(9600);
Serial.println("HX711 calibration sketch");
Serial.println("Remove all weight from scale");
Serial.println("After readings begin, place known weight on scale");
Serial.println("Press + or a to increase calibration factor");
Serial.println("Press - or z to decrease calibration factor");

scale.set_scale();
scale.tare(); //Reset the scale to 0

long zero_factor = scale.read_average(); //Get a baseline reading
Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
Serial.println(zero_factor);
}

void loop() {

scale.set_scale(calibration_factor); //Adjust to this calibration factor

Serial.print("Reading: ");
units = scale.get_units(), 10;
/*if (units < 0)
{
units = 0.00;
}
*/
ounces = units * 0.035274;
Serial.print(units);
Serial.print(" grams");
Serial.print(" calibration_factor: ");
Serial.print(calibration_factor);
Serial.println();

if(Serial.available())
{
char temp = Serial.read();
if(temp == '+' || temp == 'a')
calibration_factor += 1;
else if(temp == '-' || temp == 'z')
calibration_factor -= 1;
}
}

Assignment operators in C have higher precedence than comma.

TheMemberFormerlyKnownAsAWOL:
Assignment operators in C have higher precedence than comma.

That makes sense. So it's treated as:

units = scale.get_units();
10;

Sorry I think you guys are jumping a bit ahead of me,

What is the precedence in C - the language (in which case I definitely dont understand) or commas (which I might)

So what do we think the 10 does?

I opened hx711.h in notepad but could not find an explanation what for example the get scale command does, what syntax it needs etc.

What I'm trying to do is read from 2 scales quickly one after another.

I tried

HX711 scale(3,2);
scale.set_scale(calibration_factor1); //Adjust to this calibration factor
unit1 = scale.get_units(), 10;

HX711 scale(5,6);
scale.set_scale(calibration_factor1); //Adjust to this calibration factor
unit2 = scale.get_units(), 10;

but this won't compile as I cannot redeclare HX711??

I have used each section of the HX711 seperately, Iand it correctly reads from one sensor or the other (so the numbers must refer to pins I'm using for sensor connection) - but now I'm a bit stumped how to get readings from both scales in the same program??

Aaaaargh_rduino:
So what do we think the 10 does?

Nothing useful. Get rid of the ",10"

What I'm trying to do is read from 2 scales quickly one after another.

I tried

HX711 scale(3,2);
scale.set_scale(calibration_factor1); //Adjust to this calibration factor
unit1 = scale.get_units(), 10;

HX711 scale(5,6);
scale.set_scale(calibration_factor1); //Adjust to this calibration factor
unit2 = scale.get_units(), 10;

I you're going to have two object of the HX711 class, they have to have different names.

gfvalvo:
I you're going to have two object of the HX711 class, they have to have different names.

How would I do this? Copy and rename a HX11.h file - install in in the library and one sensor uses hx711.h and the other hx711v2.h?

Found a way from GIThub, it appears I can just define different names for scale...

HX711 scale1(3,2);
scale1.set_scale(calibration_factor1); //Adjust to this calibration factor
unit1 = scale1.get_units(), 10;

HX711 scale2(5,6);
scale2.set_scale(calibration_factor2); //Adjust to this calibration factor
unit2 = scale2.get_units(), 10;

The "Order of Precedence" is the order in which operations are done (what operation precedes another). For example, that is why "5 + 3 * 4" is 17 (5+12) and not 32 (8 * 4).

A "higher precedence" means that the '=' (assignment operator) is done before the ',' (comma operator). The line is treated as if you wrote:

(units = scale.get_units()) , 10;

The comma operator is almost never used. What is does is ignore the value to the left of the comma. About the only place I ever see the comma operator used is in a 'for' loop, for example:

  for (i=0, j= 255;  i < 128;  i++, j--)

This is a loop with i counting up from 0 to 127 and j counting down from 255. Unless the loop does something else strange with i or j you could get the same effect with:

  for (i=0; i < 128; i++)
  {
    j = 255 - i;
1 Like