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?
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);
}
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.