Arduino uno Loadcell "if statements error"

Hello, Good Evening
I am beginner in arduino programming,
I am working in a project of loadcell, In this project I am using if statements,
I use arduino uno board, 20kg Loadcell, Hx711, 16*2 lcd display, leds & breadboad Jumperwires
My code is

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#include <HX711.h>


#define DT A0
#define SCK A1
#define sw 9
#define LED 13

long sample=0;
float val=0;
long count=0;

unsigned long readCount(void)
{
unsigned long Count;
unsigned char i;
pinMode(DT, OUTPUT);
digitalWrite(DT,HIGH);
digitalWrite(SCK,LOW);
Count=0;
pinMode(DT, INPUT);
while(digitalRead(DT));
for (i=0;i<24;i++)
{
digitalWrite(SCK,HIGH);
Count=Count<<1;
digitalWrite(SCK,LOW);
if(digitalRead(DT))
Count++;
}
digitalWrite(SCK,HIGH);
Count=Count^0x800000;
digitalWrite(SCK,LOW);
return(Count);
}

void setup()
{
pinMode(SCK, OUTPUT);
pinMode(sw, INPUT_PULLUP);
lcd.begin(16, 2);
lcd.print(" Weight ");
lcd.setCursor(0,1);
lcd.print(" Measurement ");
delay(1000);
lcd.clear();
calibrate();

}

void loop()
{
count= readCount();
int w=(((count-sample)/val)-2*((count-sample)/val));
lcd.setCursor(0,0);
lcd.print("Measured Weight");
lcd.setCursor(0,1);
lcd.print(w);
lcd.print("g ");

if(digitalRead(sw)==0)
{
val=0;
sample=0;
w=0;
count=0;
calibrate();
}

{
 if (HX711.get_units() >= 1)
 {
   digitalWrite(13, HIGH);
    }
  else {
       digitalWrite(13, LOW);
      }
}

}

void calibrate()
{
lcd.clear();
lcd.print("Calibrating...");
lcd.setCursor(0,1);
lcd.print("Please Wait...");
for(int i=0;i<100;i++)
{
count=readCount();
sample+=count;
}
sample/=100;
lcd.clear();
lcd.print("Put 100g & wait");
count=0;
while(count<1000)
{
count=readCount();
count=sample-count;
}
lcd.clear();
lcd.print("Please Wait....");
delay(2000);
for(int i=0;i<100;i++)
{
count=readCount();
val+=sample-count;
}
val=val/100.0;
val=val/100.0; // put here your calibrating weight
lcd.clear();
}

When I compile it shows this error
error: expected primary-expression before '.' token

So, At the end when I compile it show error in this section:-

 if (HX711.get_units() >= 1)
 {
   digitalWrite(13, HIGH);
    }
  else {
       digitalWrite(13, LOW);
      }

Hope somebody helps me :slightly_smiling_face: :slightly_smiling_face:

Take a look at the library examples. They declare an HX711 object called scale. You will need one in your code too.

how can you explain me in depth

Here is the basic example from one of the Github repos:

#include "HX711.h"

// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 3;

HX711 scale;

void setup() {
  Serial.begin(57600);
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
}

void loop() {

  if (scale.is_ready()) {
    long reading = scale.read();
    Serial.print("HX711 reading: ");
    Serial.println(reading);
  } else {
    Serial.println("HX711 not found.");
  }

  delay(1000);
  
}

Try that on your scale before you attempt to fix what you have.

if i use scale in the place of hx711 it show this error

 if (scale.get_units() >= 1)
 {
   digitalWrite(13, HIGH);
    }
  else {
       digitalWrite(13, LOW);
      }

'scale' was not declared in this scope

i believe you need to define a variable of type HX711. HX711 is the class.

HX711 scale;

how ??
can you just correct my code in that if statement

Did you try running the example code? It looks like you’ll have to fix the pin numbers to match your wiring.

Hey can you please tell me how to declare scale in above code

Hello, Good Evening
I am beginner in arduino programming,
I am working in a project of loadcell, In this project I am using if statements,
I use arduino uno board, 20kg Loadcell, Hx711, 16*2 lcd display, leds & breadboad Jumperwires
My code is

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#include <HX711.h>


#define DT A0
#define SCK A1
#define sw 9
#define LED 13

long sample=0;
float val=0;
long count=0;

unsigned long readCount(void)
{
unsigned long Count;
unsigned char i;
pinMode(DT, OUTPUT);
digitalWrite(DT,HIGH);
digitalWrite(SCK,LOW);
Count=0;
pinMode(DT, INPUT);
while(digitalRead(DT));
for (i=0;i<24;i++)
{
digitalWrite(SCK,HIGH);
Count=Count<<1;
digitalWrite(SCK,LOW);
if(digitalRead(DT))
Count++;
}
digitalWrite(SCK,HIGH);
Count=Count^0x800000;
digitalWrite(SCK,LOW);
return(Count);
}

void setup()
{
pinMode(SCK, OUTPUT);
pinMode(sw, INPUT_PULLUP);
lcd.begin(16, 2);
lcd.print(" Weight ");
lcd.setCursor(0,1);
lcd.print(" Measurement ");
delay(1000);
lcd.clear();
calibrate();

}

void loop()
{
count= readCount();
int w=(((count-sample)/val)-2*((count-sample)/val));
lcd.setCursor(0,0);
lcd.print("Measured Weight");
lcd.setCursor(0,1);
lcd.print(w);
lcd.print("g ");

if(digitalRead(sw)==0)
{
val=0;
sample=0;
w=0;
count=0;
calibrate();
}

{
 if (HX711.get_units() >= 1)
 {
   digitalWrite(13, HIGH);
    }
  else {
       digitalWrite(13, LOW);
      }
}

}

void calibrate()
{
lcd.clear();
lcd.print("Calibrating...");
lcd.setCursor(0,1);
lcd.print("Please Wait...");
for(int i=0;i<100;i++)
{
count=readCount();
sample+=count;
}
sample/=100;
lcd.clear();
lcd.print("Put 100g & wait");
count=0;
while(count<1000)
{
count=readCount();
count=sample-count;
}
lcd.clear();
lcd.print("Please Wait....");
delay(2000);
for(int i=0;i<100;i++)
{
count=readCount();
val+=sample-count;
}
val=val/100.0;
val=val/100.0; // put here your calibrating weight
lcd.clear();
}

When I compile it shows this error
error: expected primary-expression before '.' token

So, At the end when I compile it show error in this section:-

 if (HX711.get_units() >= 1)
 {
   digitalWrite(13, HIGH);
    }
  else {
       digitalWrite(13, LOW);
      }

Hope somebody helps me :slightly_smiling_face: :slightly_smiling_face:

I would advise you to find any simple complete example of using the HX7111 library. Get it working.

At a glance, you have not declared an instance of the object the library uses to represent the load cell.

Here’s two lines from an example I found google easily

#include "HX711.h"

HX711 loadcell;  // <— makes a loadcell object named loadcell 

then to refer to that object, it would be like

loadcell.get_units();

I like to name objects so I know whose they are, BTW, so for me it would be

HX711 myLoadCell; // a loadcell object named myLoadCell

HTH

a7

Hey man! you are insane
It worked
I literally wasted 1 day and 3 hrs for this only
I dont know how to thank you
you are literaly insane
Thanks again for this help…

That’s what the GF tells me frequently. :wink:

a7

:rofl: :rofl: :rofl: :rofl: :rofl: :rofl: :rofl:

The HX711 library comes with six examples to show how to use it. You can find library examples in the IDE menu:

File->Examples->HX711->

I’ve merged your cross-posts @divyanshsingh.

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a suspension from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the “How to get the best out of this forum” guide you will find at the top of every forum category. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Hi,

A little helps in all of us…

Tom… :grinning: :+1: :coffee: :australia:

okay i understand