HX711 icon indicating copy to clipboard operation
HX711 copied to clipboard

taking out objects from scale changes tare?

Open MBENKI opened this issue 3 years ago • 1 comments

Hi everyone, i am having a big problem, with my scale, if i keep taking out and putting back the same item on the scale, thee weight goes into negative or a number way greater than 0 when i leave the scale empty for example after the calibration my sketch works fine, but if i take out the object several times from the scale, it shows -21g or -40g or even 150g or also 70g while it is empty. by the way i display directlty on a 240*320 dsplay that why i use the unction draw240() instead of a serial println I dont know why ?? could someone please help me understand here's the code ` #include <HX711.h> #include <printucg240.h>

const int LOADCELL_DOUT_PIN = 4;
const int LOADCELL_SCK_PIN = 5;
const int eepromCalibAddr = 10;
const int precisionScale = 30; // round of weight precision
float prevCalib; //= EEPROM.get(eepromCalibAddr, prevCalib);
int currentWeight = 0;
int instantWeight = 0;
int appog = 0;
int weight0;
int weight1;
int weight2;
//HX711 constructor:
HX711 LoadCell;

void setup() {
  Serial.begin(9600);
  while (!Serial) { ; }  // wait for serial port to connect. Needed for native USB port only
  LoadCell.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  calibrateNow();
  draw240("bil. pronta !", 3000);
}

void loop() {
  weight2 = weight1;
  weight1 = weight0;
  instantWeight = int(round(LoadCell.get_units(3)));
  weight0 = weightWithScalePrecision(instantWeight);
  // here verify diff. between the last and the new readings, if diff. bigger than 2*precision then proceed with reading the new current weight
  if (weight0 != currentWeight && appog == 0 && abs(currentWeight - weight0) > (precisionScale*2)) {
    draw240("sto pesando...", 0);
    appog = 1;
  }
  if (checkApproxWeight(weight0, weight1) == true && (checkApproxWeight(weight1, weight2)) == true && true && appog == 1) {
    if (currentWeight != weight0) { 
      if (weight0 < 0) weight0 = 0;
      currentWeight = weight0; 
    }    
    appog = 0;
    draw240(String(currentWeight) + " (" + String(instantWeight) + ")", 1500);
  }
  delay(100);
}

boolean checkApproxWeight(int w1, int w2) {
  return abs(w1 - w2) < (precisionScale*2);
}

void calibrateNow() {
  draw240("*******", 1000);
  draw240("inizio calib. :", 1500);
  draw240("svuotare piatto", 5000);
  LoadCell.set_scale();
  LoadCell.tare(); 
  //LoadCell.set_offset(LoadCell.read_average()); 
  draw240("offset:" + String(LoadCell.get_offset()), 5500);
  draw240("appoggiare 6,3kg", 5500);
  draw240("avg:" + String(LoadCell.read_average(10)), 5500); // raw weight
  prevCalib = LoadCell.get_units(10) / 6300;
  draw240("ult. val. EEPROM: ", 2000);
  draw240(String(prevCalib), 2000);
  //EEPROM.put(eepromCalibAddr, prevCalib);
  LoadCell.set_scale(prevCalib); 
}

int weightWithScalePrecision(int w) {
  float remainder = w % precisionScale;
  if (remainder <= (precisionScale/2)) {
    w -= int(remainder);    
  } else if (remainder > (precisionScale/2)){
    w += (precisionScale-int(remainder));
  }
  return w;
}`

MBENKI avatar May 21 '21 16:05 MBENKI

I have the same problem with repeatability, even in a range of ± 5g or so, so every load and unload I get different values.

boromyr avatar Dec 11 '21 21:12 boromyr