1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| #include <HX711.h>//https://github.com/bogde/HX711
const long LOADCELL_OFFSET = 340884;
const float LOADCELL_DIVIDER = 262.5F;
const int numberOfReadings = 10;
const int doutPin = A1;
const int sckPin = A0;
HX711 scale;
float weight = 0;
float initWeight = 0;
void setup() {
Serial.begin(1200);
Serial.println(F("Initialize System"));
scale.begin(doutPin, sckPin);
scale.set_scale(); //remove scale divider
scale.tare(); // reset the scale to 0
//find
Serial.println("Put a known wait and send command");
while (Serial.available() == 0) {}
Serial.print("Compute average on 10...");
Serial.println(scale.get_units(10), 1); //repeat with two different weight to find DIVIDER and OFFSET
//WEIGHT= (val-OFFSET)/DIVIDER
scale.set_scale(LOADCELL_DIVIDER);
scale.tare();
//scale.set_offset(LOADCELL_OFFSET);
delay(200);
initWeight = scale.get_units(numberOfReadings * 10), 10;
}
void loop() {
readScale();
}
void readScale() { /* function readScale */
//// Read button states from keypad
if (scale.is_ready()) {
weight = scale.get_units(numberOfReadings), 10;
Serial.print("weight : ");
Serial.println(weight);
} else {
Serial.println("HX711 not found.");
}
} |
Partager