Scale program broekn running 100 times a second

I'm currently having a problem trying to get my code to calibrate correctly and to be able to stop after 15 seconds any help would be amazing.


#include <Q2HX711.h>

const byte hx711_data_pin = 3;
const byte hx711_clock_pin = 4;

float y1 = 15.0; // calibrated mass to be added
long x1 = 0L;
long x0 = 0L;
float avg_size = 1.0; // amount of averages for each mass measurement (also speeds up sensor data intake by making the amount smaller)

Q2HX711 hx711(hx711_data_pin, hx711_clock_pin); // prep hx711

void setup() {
  Serial.begin(9600); // prepare serial port
  delay(1000); // allow load cell and hx711 to settle

  // Tare procedure
  for (int ii = 0; ii < int(avg_size); ii++) {
    delay(10);
    x0 += hx711.read();
  }
  x0 /= long(avg_size);
  Serial.println("Add Calibrated Mass");

  // Calibration procedure (mass should be added equal to y1)
 int ii = 1;
  while(true){
    if (hx711.read()<x0+10000){
    } else {
      ii++;
      delay(2000);
      for (int jj=0;jj<int(avg_size);jj++){
        x1+=hx711.read();
      }
      x1/=long(avg_size);
      break;
    }
  }
  Serial.println("Calibration Complete");
  Serial.println("Start");
}

void loop() {
  int count = 0;
  long reading = 0;
  count = count + 1;
  while (count < 1) {
    reading += hx711.read();
    
  }

  reading /= long(avg_size);

  if (count != 100) {
    // calculating mass based on calibration and linear fit
    float ratio_1 = (float)(reading - x0);
    float ratio_2 = (float)(x1 - x0);
    float ratio = ratio_1 / ratio_2;
    float mass = y1 * ratio;
    count = count + 1;
    Serial.print(count);
    Serial.print(" | Weight(lbs): ");
    Serial.println(mass);
    count++;
  }
  else{
    Serial.println("DONE");
  }

  delay(10); // Delay for 10 milliseconds (100 times per second)
}

What i'm trying to do is make a scale that takes measurements 100 times a second. so when i do the calibration it should just print out 15 for the 15 lbs, I'm using to do this.

moving int count = 0 to the top fixed the counter but the problem with the caibration is still bugged

I have it working now for the calibration, by switching a while loop to a for loop and moving count. my issue currently is that i can not get the output to can any faster that 100 readings in 9.5 seconds. I've tried changing the value of delay at the end of the code and just getting ride of it but nothing works.

#include <Q2HX711.h>
const byte hx711_data_pin = 3;
const byte hx711_clock_pin = 4;

float y1 = 15.0; // calibrated mass to be added
long x1 = 0L;
long x0 = 0L;
float avg_size = 1.0; // amount of averages for each mass measurement (also speeds up sensor data intake by making the amount smaller)
int count = 0;
Q2HX711 hx711(hx711_data_pin, hx711_clock_pin); // prep hx711

void setup() {
  Serial.begin(9600); // prepare serial port
  delay(1000); // allow load cell and hx711 to settle

  // Tare procedure
  for (int ii = 0; ii < int(avg_size); ii++) {
    delay(10);
    x0 += hx711.read();
  }
  x0 /= long(avg_size);
  Serial.println("Add Calibrated Mass");

  // Calibration procedure (mass should be added equal to y1)
 int ii = 1;
  while(true){
    if (hx711.read()<x0+10000){
    } else {
      ii++;
      delay(2000);
      for (int jj=0;jj<int(avg_size);jj++){
        x1+=hx711.read();
      }
      x1/=long(avg_size);
      break;
    }
  }
  Serial.println("Calibration Complete");
  Serial.println("Start");
}

void loop() {
  long reading = 0;
  for (int jj=0;jj<int(avg_size);jj++){
    reading+=hx711.read();
  }

  reading /= long(avg_size);

  if (count < 100) {
    // calculating mass based on calibration and linear fit
    float ratio_1 = (float)(reading - x0);
    float ratio_2 = (float)(x1 - x0);
    float ratio = ratio_1 / ratio_2;
    float mass = y1 * ratio;
    count = count + 1;
    Serial.print(count);
    Serial.print(" | Weight(lbs): ");
    Serial.println(mass);
  }
  else{
    Serial.println("DONE");
  }
  delay(10); // Delay for 10 milliseconds (100 times per second)  
}

Are you sure the HX711 supports that rate?

did not think about if it could handle that rate

just looked it up and it says 10 so that explains that

how would i write the power to pin 15?

without desoldering the chip

You have a zero-ohm resistor, jumper, cutable trace, or similar on the PWB so that you can change the logic level applied Pin 15 relatively easily.

What is the purpose of measuring so often?

A longer time ago I bought a real cheap HX711 / 5kg load-cell set for $10

and made some tests with this demo-code. This library has a calibrating function ready to use.

//
//    FILE: HX_kitchen_scale.ino
//  AUTHOR: Rob Tillaart
// PURPOSE: HX711 demo
//     URL: https://github.com/RobTillaart/HX711


#include "HX711.h"

HX711 scale;

//uint8_t dataPin = 6;
//uint8_t clockPin = 7;
uint8_t dataPin  = 19;//for esp32
uint8_t clockPin = 18;//for esp32

void setup() {
  Serial.begin(115200);
  Serial.println(__FILE__);
  Serial.print("LIBRARY VERSION: ");
  Serial.println(HX711_LIB_VERSION);
  Serial.println();

  scale.begin(dataPin, clockPin);

  Serial.print("UNITS: ");
  Serial.println(scale.get_units(10));

  Serial.println("\nEmpty the scale, press a key to continue");
  while(!Serial.available());
  
  while(Serial.available()) {
    Serial.read();
  }
  scale.tare();
  Serial.print("UNITS: ");
  Serial.println(scale.get_units(10));


  Serial.println("\nPut 1000 gram in the scale, press a key to continue");
  while(!Serial.available());
  while(Serial.available()) {
    Serial.read();
  }

  scale.calibrate_scale(1000, 5);
  Serial.print("UNITS: ");
  Serial.println(scale.get_units(10));

  Serial.println("\nScale is calibrated, press a key to continue");
  // Serial.println(scale.get_scale());
  // Serial.println(scale.get_offset());
  while(!Serial.available());
  while(Serial.available()) {
    Serial.read();
  }
}


void loop() {
  Serial.print("UNITS: ");
  Serial.println(scale.get_units(10));
  delay(250);
}

// -- END OF FILE --

I tested it with differents weights from a few grams up to 2 kg and the measured value was the same +- 1 gram to my kitchen-scale

I think i solved my problem some what i have it running at 40 sps now what's better.

my problem now is that my scale is not reading the inputs anymore but only printing the calibration weight, and I did not change the code

#include <Q2HX711.h>

const byte hx711_data_pin = 3;
const byte hx711_clock_pin = 4;

float y1 = 15.0; // calibrated mass to be added
long x1 = 0L;
long x0 = 0L;
float avg_size = 1.0; // amount of averages for each mass measurement (also speeds up sensor data intake by making the amount smaller)
int count = 0;
Q2HX711 hx711(hx711_data_pin, hx711_clock_pin); // prep hx711

void setup() {
  Serial.begin(9600); // prepare serial port
  delay(1000); // allow load cell and hx711 to settle

  // Tare procedure
  for (int ii = 0; ii < int(avg_size); ii++) {
    delay(10);
    x0 += hx711.read();
  }
  x0 /= long(avg_size);
  Serial.println("Add Calibrated Mass");

  // Calibration procedure (mass should be added equal to y1)
 int ii = 1;
  while(true){
    if (hx711.read()<x0+10000){
    } else {
      ii++;
      delay(2000);
      for (int jj=0;jj<int(avg_size);jj++){
        x1+=hx711.read();
      }
      x1/=long(avg_size);
      break;
    }
  }
  Serial.println("Calibration Complete");
  Serial.println("Start");
}

void loop() {
  long reading = 0;
  for (int jj=0;jj<int(avg_size);jj++){
    reading+=hx711.read();
  }

  reading /= long(avg_size);
  while (count < 501){
    if (count < 500) {
      // calculating mass based on calibration and linear fit
      float ratio_1 = (float)(reading - x0);
      float ratio_2 = (float)(x1 - x0);
      float ratio = ratio_1 / ratio_2;
      float mass = y1 * ratio;
      count = count + 1;
      Serial.print(count);
      Serial.print(" | Weight(lbs): ");
      Serial.println(mass);
    }
    else{
      Serial.println("DONE");
      count = count + 1;
      break;
    }
    delay(13); // Delay for 10 milliseconds (100 times per second)  
  }
}

had to move the while loop

i got it to work i'm testing force of model rockets with my little brother to see what we can use to make the bodies of the rocket with. we are gonna hand calculate everything but we want more data points to see the curve how the force

at least a hint for other users how you should not code it

sum up measurings

calculate average (which is a single number

repeat 500 times to use one single value
without reading 499 new values
to calculate something

#include <Q2HX711.h>

const byte hx711_data_pin = 3;
const byte hx711_clock_pin = 4;

float y1 = 15.0; // calibrated mass to be added
long x1 = 0L;
long x0 = 0L;
float avg_size = 1.0; // amount of averages for each mass measurement (also speeds up sensor data intake by making the amount smaller)
int count = 0;
Q2HX711 hx711(hx711_data_pin, hx711_clock_pin); // prep hx711

void setup() {
  Serial.begin(9600); // prepare serial port
  delay(1000); // allow load cell and hx711 to settle
  // Tare procedure
  for (int ii = 0; ii < int(avg_size); ii++) {
    delay(10);
    x0 += hx711.read();
  }
  x0 /= long(avg_size);
  Serial.println("Add Calibrated Mass");
  // Calibration procedure (mass should be added equal to y1)
 int ii = 1;
  while(true){
    if (hx711.read()<x0+10000){
    } else {
      ii++;
      delay(2000);
      for (int jj=0;jj<int(avg_size);jj++){
        x1+=hx711.read();
      }
      x1/=long(avg_size);
      break;
    }
  }
  Serial.println("Calibration Complete");
  Serial.println("Start");
}

void loop() {
  while (count < 201){
    long reading = 0;
    for (int jj=0;jj<int(avg_size);jj++){
      reading+=hx711.read();
    }
    reading /= long(avg_size);
    if (count < 200) {
      // calculating mass based on calibration and linear fit
      float ratio_1 = (float)(reading - x0);
      float ratio_2 = (float)(x1 - x0);
      float ratio = ratio_1 / ratio_2;
      float mass = y1 * ratio;
      count = count + 1;
      Serial.print(count);
      Serial.print(" | Weight(lbs): ");
      Serial.println(mass);
    }
    else{
      Serial.println("DONE");
      count = count + 1;
      break;
    }
    delay(13); // Delay for 10 milliseconds (100 times per second)  
  }
}

maybe it helps if you write a posting where you explain in pure everyday words by avoiding all math and avoiding code what you want to do

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