AVERAGE READING of the drag and lift forces

Hi guys.. hope you can help me this one.. Since the reading of my load cell fluctuates due to the vibration and other factors, I need to find its average. By the way i use a millligrams unit.

#include "HX711.h"

// HX711 circuit wiring
const int DF_DOUT = 2; // DOUT for drag
const int DF_SCK = 4; // SCK for Drag

const int LF_DOUT = 11; // DOUT for Lift
const int LF_SCK = 12; // SCK for Lift

//HX711 constructor:
HX711 DFORCE;
HX711 LFORCE;

void setup() {
Serial.begin(38400);
Serial.println();
Serial.println("Starting Set Up...");
DFORCE.begin(DF_DOUT, DF_SCK);
DFORCE.set_scale();
DFORCE.tare();

LFORCE.begin(LF_DOUT, LF_SCK);
LFORCE.set_scale();
LFORCE.tare();

}

void loop() {
long DragOrigRead = DFORCE.read ();
long Drag2ndRead;
long Drag1stRead;

long LiftOrigRead = LFORCE.read ();
long Lift2ndRead;
long Lift1stRead;

Serial.println();
Serial.println();
Serial.println("Start The Fan And Set A Desired Velocity");

Drag1stRead = (-000.524* LiftOrigRead) -127008.2+14000;

Lift1stRead = (-000.525* DragOrigRead) +37008.2+52000;

Serial.print("Drag Force Reading Is:");
Serial.print(Drag1stRead);

Serial.println();
Serial.print("Lift Force Reading Is:");
Serial.print(Lift1stRead);

delay(10000);

}

So, sum a number of readings, and divide the sum by the number of readings.

If you need a moving average, there are libraries for that, or you can write a simple circular buffer, so you subtract the oldest reading and add the newest to a running total.

thank you for the response sir..by the way hows my code gonna look like??..can you show me sir?

you can also try leaky integration

avg += (reading - avg) * K; // K < 1

hows gonna my code look like?

You need to decide how you want your software to perform.

how and where to insert that line to my code?

Your code can be structured like this, using an enum statement and converting all your code into separated functions:

#include "HX711.h"

// Declare functions
enum MeasurementType {drag, lift};
long takeMeasurement(MeasurementType type);
long getAverage(MeasurementType type, int N_Measurement);

// HX711 circuit wiring
const int DF_DOUT = 2; // DOUT for drag
const int DF_SCK = 4; // SCK for Drag

const int LF_DOUT = 11; // DOUT for Lift
const int LF_SCK = 12; // SCK for Lift

//HX711 constructor:
HX711 DFORCE;
HX711 LFORCE;

void setup() {
  Serial.begin(38400);
  Serial.println();
  Serial.println("Starting Set Up…");
  DFORCE.begin(DF_DOUT, DF_SCK);
  DFORCE.set_scale();
  DFORCE.tare();

  LFORCE.begin(LF_DOUT, LF_SCK);
  LFORCE.set_scale();
  LFORCE.tare();

}

void loop() {
  long DragRead = getAverage(DFORCE, drag, 5);  // Get the drag average of 5 measurements
  long LiftRead = getAverage(LFORCE, lift, 5);  // Get the lift average of 5 measurements

  Serial.print("Drag Force Reading Is:");
  Serial.print(DragRead);

  Serial.println();
  Serial.print("Lift Force Reading Is:");
  Serial.print(LiftRead);

  // THE CODE YOU WANT HERE

  delay(10000);
}

long takeMeasurement(HX711 FORCE, MeasurementType type) {
  long measurement = 0;
  switch (type) {
    case drag:
      measurement = (-000.524 * FORCE.read ()) - 127008.2 + 14000;
      break;
    case lift:
      measurement = (-000.525 * FORCE.read ()) + 37008.2 + 52000;
      break;
  }
  return measurement;
}

long getAverage(HX711 FORCE, MeasurementType type, int N_Measurements) {
  long average = 0;
  for (int i = 0; i < N_Measurements; i++) {
    average += takeMeasurement(FORCE, type) / N_Measurements;
  }
  return average;
}

I hope this help you in your project.
Cheers!

1 Like

thank you very much for your help sir.God Bless​:heart::heart::heart:

I'd be inclined to use references for the sensor objects in the functions, then you wouldn't need the enums

1 Like

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