How to resolve 'scale' not declared

I have used the following code from this website:Load Cell Amplifier HX711 Breakout Hookup Guide - SparkFun Learn

int motorPin = 3; //motor transistor is connected to pin 3
void setup() {
  pinMode(motorPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("HX711 scale demo");

  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale(calibration_factor); //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:");
}

void loop() {
  Serial.print("Reading: ");
  Serial.print(scale.get_units(), 1); //scale.get_units() returns a float
  Serial.print(" lbs"); //You can change this to kg but you'll need to refactor the calibration_factor
  Serial.println();
  if (scale.get_units()>100) {Serial.print(">100")};
  if (scale.get_units()>100) {
  digitalWrite(motorPin, HIGH); //vibrate
  delay(20000);  // delay one second
  digitalWrite(motorPin, LOW);  //stop vibrating
  delay(1000); //wait one second  
  };
  
  delay(100);
}

After I tried to upload my code to my Arduino nano it said 'scale' not declared. What have I done wrong and how do I fix my code?

At least this line from the link

HX711 scale;

is missing. You may have failed to cut and paste all the code.

a7

Where would I put the lineHX711 scale;

Also missing is the line that #includes the library and some variable declarations,

#include "HX711.h"

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

#define DOUT  3
#define CLK  2

HX711 scale;

Before you run the code for measurement you should run the code provided in the tutorial to obtain the calibration factor.

what does #define DOUT 3 #define CLK 2 mean?

"#define DOUT 3" creates a symbol named DOUT which is equal to 3.

Thank you for all the help. But I have encountered another issue. The error says

expected ';' before '}' token in the line- if (scale.get_units()>100) {Serial.print(">100")};

int motorPin = 3; //motor transistor is connected to pin 3
#include "HX711.h"

#define LOADCELL_DOUT_PIN 3
#define LOADCELL_SCK_PIN 2

HX711 scale;

float calibration_factor = -1620; //-7050 worked for my 440lb max scale setup
void setup() { 
  pinMode(motorPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("HX711 scale demo");

  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale(-1620); //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:");
}

void loop() {
  Serial.print("Reading: ");
  Serial.print(scale.get_units(), 1); //scale.get_units() returns a float
  Serial.print(" lbs"); //You can change this to kg but you'll need to refactor the calibration_factor
  Serial.println();
  if (scale.get_units()>100) {Serial.print(">100")};
  if (scale.get_units()>100) {
  digitalWrite(motorPin, HIGH); //vibrate
  delay(1000);  // delay one second
  digitalWrite(motorPin, LOW);  //stop vibrating
  delay(1000); //wait one second  
  };
  
  delay(100);
}

How do I fix this issue?

The semi colon to end the Serial print statement is in the wrong place.
Should be:

if (scale.get_units()>100) {Serial.print(">100");}

But this is much better, I think.

if (scale.get_units()>100)
{
    Serial.print(">100");
}

I do not like to put more than one statement per line. I do like to put { and } on their own lines and indent the code properly. Makes reading and following the code easier.

Thank you so much! I fixed my code!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.