Having trouble coding LEDs to flash with a load cell and hx711 amplifier.

Hi, I’m a newbie to coding, but have been trying to learn and now feel like I’ve hit a wall with my development and knowledge.

I’ve been working on a Uni project and want the LED from pin 13 to flash when there is no load exerted on the cell, then stop when there is a load. The stopping is what I’m having trouble with.

So far I’ve using Bodge’s HX711 calibration and running code from GitHub, and added to it from my experiments with other sketches.

This is my code so far,

*/

#include "HX711.h"

#define calibration_factor -7050.0 //This value is obtained using the SparkFun_HX711_Calibration sketch

#define DOUT  3
#define CLK  2
#define LEDPin 13// ME - what I've added in myself.

HX711 scale(DOUT, CLK);

void setup() {

  Serial.begin(9600);
  Serial.println("HX711 scale demo");

  scale.set_scale(-9470.00); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale.tare();	//Assuming there is no weight on the scale at start up, reset the scale to 0

  Serial.println("Readings:");
  pinMode(LEDPin, OUTPUT);
}

void loop() {
  Serial.print("Reading: ");
  Serial.print(scale.get_units(), 1); //scale.get_units() returns a float
  Serial.print(" ml"); //You can change this to kg but you'll need to refactor the calibration_factor
  Serial.println();
  {
 
 }
  
if (scale.get_units(), 1 <= 0.10)
    Serial.println("empty"); //ME
  digitalWrite(13, HIGH);   // From the blink example
  delay(1000);              
  digitalWrite(13, LOW);    
  delay(1000);             

  if (scale.get_units(), 1 >= 0.10)   // ME - this is where I'm having trouble, won't stop flashing.
    Serial.println("Full");
  digitalWrite(13, LOW);
  
{

    delay(1000);
  }

}

I’ve been researching first, but any time I try to incorporate the word ‘else’ in it, the software doesn’t like it and fails to verify. I’ve thought about using switch case’s too as an alternative, tried that and I couldn’t get it to work :frowning: .

Any help would be much appreciated and hope I’ve posted correctly, thanks for your time.

any time I try to incorporate the word ‘else’ in it, the software doesn’t like it and fails to verify.

The obvious thing to do is to post the code that causes the problem and the whole error message(s) that you get when you verify it.

if (scale.get_units(), 1 <= 0.10)looks wrong to me …
Please explain in words what you think it does.

Hi Helibob, thanks for having a look at my problem.

This is the code and error if I pop an else in.

*/

#include "HX711.h"

#define calibration_factor -7050.0 //This value is obtained using the SparkFun_HX711_Calibration sketch

#define DOUT  3
#define CLK  2
#define LEDPin 13// ME

HX711 scale(DOUT, CLK);

void setup() {

  Serial.begin(9600);
  Serial.println("HX711 scale demo");

  scale.set_scale(-9470.00); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale.tare();	//Assuming there is no weight on the scale at start up, reset the scale to 0

  Serial.println("Readings:");
  pinMode(LEDPin, OUTPUT);
}

void loop() {
  Serial.print("Reading: ");
  Serial.print(scale.get_units(), 1); //scale.get_units() returns a float
  Serial.print(" ml"); //You can change this to kg but you'll need to refactor the calibration_factor
  Serial.println();
  {
  }
  if (scale.get_units(), 1 <= 0.10)
    Serial.println("empty"); //ME
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second

  if else (scale.get_units(), 1 >= 0.10)
    Serial.println("Full");
  digitalWrite(13, LOW);
  {

    delay(1000);
  }
}

Error message :-

load_senors_test_11:61: error: expected ‘(’ before ‘else’

if else (scale.get_units(), 1 >= 0.10)

^

exit status 1
expected ‘(’ before ‘else’

Here’s another way I’ve seen it expressed, same with an error.

.........

void loop() {
  Serial.print("Reading: ");
  Serial.print(scale.get_units(), 1); //scale.get_units() returns a float
  Serial.print(" ml"); //You can change this to kg but you'll need to refactor the calibration_factor
  Serial.println();
  {
  
}
 
 if (scale.get_units(), 1 <= 0.10)
    Serial.println("empty"); //ME
  digitalWrite(13, HIGH);   
  delay(1000);              
  digitalWrite(13, LOW);    
  delay(1000);              

}else{                         //  Here
  
if (scale.get_units(), 1 >= 0.10)
    Serial.println("Full");
  digitalWrite(13, LOW);
  
{

    delay(1000);
  }
}

Error being :-

}else{

^

exit status 1
expected unqualified-id before ‘else’

The second question about this -

if (scale.get_units(), 1 <= 0.10)

My understanding of it is that this bit of code,

(scale.get_units(), 1) ,

is the statement that is containing the current value of weight, so it is this statement that I need to compare being less than 0.10 ml to get the LED to flash.

  Serial.println();
  {
  }

What are the useless curly braces for?

  if (scale.get_units(), 1 <= 0.10)

What do you think that comma is doing? It almost certainly isn’t. But fixing the code is going to depend on what you (mistakenly) think you are doing.

  if else (scale.get_units(), 1 >= 0.10)

There is an else if statement. There is NOT an if else statement.

My understanding of it is that this bit of code,

That is NOT a “bit of code”. That was the parentheses and argument list to a function call. Copying that bit of code and expecting to use it AS A SINGLE VALUE is like copying the complete argument list for analogWrite() and expecting to use it as a pin number.

Learn what that function was doing, and you’ll understand that the complete argument list is nonsense when pasted somewhere else (unless it is pasted as an argument list again).

if (scale.get_units(), 1 <= 0.10)Waiting with bated breath for an explanation of what the OP thinks this does.

@Desperate_Dan : Rather than wasting everyone's time reporting a post to moderation, why don't you answer the question, so we can work out what it is you want to do?

Ok,

Pual S - Curly Braces - it is one of the variations that seem to verify in the software, I've been looking at many examples of coding on the forum and elsewhere and this set out has worked before.

The most frustrating thing is the number of variations of the same outcome there seems to be, just following the reference pages and examples this site offers doesn't seem to always be the working case in reality.

Also the comma - I have no idea, it is in the original code written by someone else for this load cell sketch. I don't fully understand why that is there either, as commas aren't in the Arduino reference pages.

Helibob - Just to be clear I thoughtvI answered your question in my previous post to you:-

"My understanding of it is that this bit of code,

(scale.get_units(), 1) ,

is the statement that is containing the current value of weight, so it is this statement that I need to compare being less than 0.10 ml to get the LED to flash."

Further in thinking - the units are being stored in the () part and are then being compared to the integer 1, that symbolises 'true' I believe?

Out of interest what's an OP?

I am now wondering that I need to somehow store the value from "(scale.get_units(), 1)" to then compare it after for the LED part of the code? Is that right?

Looking at the HX711 library I see

float HX711::get_units(byte times) {
 return get_value(times) / SCALE;
}

so the call to it should be

scale.get_units(1)

Are you sure that you copied the original code correctly ?

The other thing you can do is follow the example(s) given in my YouTube videos #31 & #32 in which I create a Coffee Cup Coaster (with Load Cell and HX711) - at least then you will get some working code from which to branch out on.

Easy to use and run and once you have completed the simple demo it will undoubtedly be blindingly obvious how to do what you want with it.

URL in the footer of this post. Enjoy!

Thanks Helibob, that has sorted out the problem of the serial monitor displaying 'Full' all the time. It now knows the difference between 'empty' and 'full'.

Ralf - Thank you so much for your videos, what I need in my project is very similar. Working from the same source, the spark fun example. So at least I know I've been going down the correct lines of research. I am working through your code and what I've achieved already, so I try and gain a better understanding on my work.

Once again thanks to both of you for your help so far.

I'll let you know how I get on (and if I need more help).