code execute loadcell value fast before getting intended weight

Hello guys, I want if function to run when weight is > than 10kg, loadcell reading is slow that when greater that 10kg weight is put on the scale if function becomes false (loadcell reading starts from lower numbers).

my purpoose is to reset scale to zero when weight < 5 is put on scale
Thanks for your time

if (weight > 10 )
{ Serial.print(" weight :\t");
   Serial.println(weight, 3);
}

if (weight < 5 )
{ 
   scale.tare();
}

Maybe you should wait until the scale reads the same value for some time before you use the value. Compare each reading to the previous reading. If it is different, note the time and don't act on the reading. If the noted time is more than a few seconds in the past, use the current value.

johnwasser:
Maybe you should wait until the scale reads the same value for some time before you use the value. Compare each reading to the previous reading. If it is different, note the time and don't act on the reading. If the noted time is more than a few seconds in the past, use the current value.

thanks johnwasser for your time, what kind of variables can I store the readings so that I can compare them?

eddy1234:
thanks johnwasser for your time, what kind of variables can I store the readings so that I can compare them?

The same type as your 'weight' variable? You didn't include all of your sketch so I don't know what type that is.

johnwasser:
The same type as your ‘weight’ variable? You didn’t include all of your sketch so I don’t know what type that is.

Here is my whole code, Thanks

#include "HX711.h"

HX711 scale;

void setup() {
  Serial.begin(9600);
  Serial.println("HX711 Demo");
  Serial.println("Initializing the scale");
  // parameter "gain" is ommited; the default value 128 is used by the library
  // HX711.DOUT  - pin #A1
  // HX711.PD_SCK - pin #A0
  scale.begin(4, 5);

  Serial.print("Raw ave(20): \t\t");
  Serial.println(scale.read_average(20));   // print the average of 20 readings from the ADC

  // Scale factor:
  // 1Kg cell: 2020 for reading in gms2226.51
  // 50kg cells: 19150 for reading in kg
  scale.set_scale(2226.f);                      // this value is obtained by calibrating the scale with known    weights; see the README for details
  scale.tare();               // reset the scale to 0

  Serial.println("\nAfter setting up the scale:");

  Serial.print("Raw: \t\t\t");
  Serial.println(scale.read());                 // print a raw reading from the ADC

  Serial.print("Raw ave(20): \t\t");
  Serial.println(scale.read_average(20));       // print the average of 20 readings from the ADC

  Serial.print("Raw ave(5) - tare: \t");
  Serial.println(scale.get_value(5));   // print the average of 5 readings from the ADC minus the tare weight, set with tare()

  Serial.print("Calibrated ave(5): \t");
  Serial.println(scale.get_units(5), 1);        // print the average of 5 readings from the ADC minus tare weight, divided
  // by the SCALE parameter set with set_scale
  Serial.println("\nReadings:");
}

void loop() {
  int  i, n, T;
  unsigned long  k, s;
  unsigned long  y = 5000;
  double val, weight, sum, sumsq, mean, g1;
  n = 5;

  i = sum = sumsq = 0;
  while (i < n) {
    val = ((scale.read() - scale.get_offset()) / scale.get_scale());;
    sum += val;
    sumsq += val * val;
    i++;
  }

  mean = sum / n;

  if (mean > 1 )
  {
    delay(5000);
    weight = mean;
  }

  if ( weight < 5 )
  {
    scale.tare();
  }
  Serial.print(" mean :\t");
  Serial.println(weight, 3);

  if ( weight > 10)
  {
    Serial.print(" weight :\t");
    Serial.print(weight, 3); Serial.println("\t");
    delay(500);
    
  }
  

}

I think this is close to what you want:

#include "HX711.h"
HX711 scale;


void setup()
{
  Serial.begin(9600);
  Serial.println("HX711 Demo");
  Serial.println("Initializing the scale");
  // parameter "gain" is ommited; the default value 128 is used by the library
  // HX711.DOUT  - pin 4
  // HX711.PD_SCK - pin 5
  scale.begin(4, 5);


  Serial.print("Raw ave(20): \t\t");
  Serial.println(scale.read_average(20));   // print the average of 20 readings from the ADC


  // Scale factor:
  // 1Kg cell: 2020 for reading in gms2226.51
  // 50kg cells: 19150 for reading in kg
  scale.set_scale(2226.0);    // this value is obtained by calibrating the scale with known    weights; see the README for details
  scale.tare();               // reset the scale to 0


  Serial.println("\nAfter setting up the scale:");


  Serial.print("Raw: \t\t\t");
  Serial.println(scale.read());                 // print a raw reading from the ADC


  Serial.print("Raw ave(20): \t\t");
  Serial.println(scale.read_average(20));       // print the average of 20 readings from the ADC


  Serial.print("Raw ave(5) - tare: \t");
  Serial.println(scale.get_value(5));   // print the average of 5 readings from the ADC minus the tare weight, set with tare()


  Serial.print("Calibrated ave(5): \t");
  Serial.println(scale.get_units(5), 1);        // print the average of 5 readings from the ADC minus tare weight, divided
  // by the SCALE parameter set with set_scale
  Serial.println("\nReadings:");
}


// Average five readings
double getWeight()
{
  double sum = 0.0;
  for (int i = 0; i < 5; i++)
  {
    sum +=  ((scale.read() - scale.get_offset()) / scale.get_scale());
  }
  return sum / 5.0;
}


void loop()
{
  unsigned long currentMillis = millis();
  double weight = getWeight();


  static unsigned long lastWeightChangeMillis = 0;
  static double containerWeight;
  static double previousWeight;


   // NOTE: This comparison might be too subject to noise.  If the weight never stabilizes, change the 
   // comparison to something like:
   // (weight < previousWeight-0.01 || weight > previousWeight + 0.01)
   // Adjust the constant until the weight settles reliably in the range.
  if (weight != previousWeight)  // NOTE: This comparison 
  {
    lastWeightChangeMillis = millis();
    previousWeight = weight;
  }


  // If weight has not changed in the last three seconds
  if (currentMillis - lastWeightChangeMillis > 3000)
  {
    if (weight < 5)
    {
      containerWeight = weight;
    }
    else
    {
      Serial.print(" Weight:\t");
      Serial.println(weight - containerWeight, 3);
    }
    delay(500);
  }
}

thanks a lot pal, this code is giving me hope it’s working, I’m still running more tests