Sample frequency optimization, with VNH5019 and HX711

Hi!

I’ve created a miniature tensile test device with a linear actuator and a load cell. I’ve used the VNH5019 motor shield and HX711 load cell amplifier to use these with my Arduino Uno. I recently added the calibration factor in (for the load cell) and it seems that the frequency of readings have been cut in half. Could someone quickly look through my code and see where there is room to optimize it? I’m a bit of a beginner on the programming side of things, so any input would be greatly appreciated!

The actual measurement starts at the last “else if” when button4 is pressed. The other buttons are for jogging the linear actuator rod to get it into position.

#include "DualVNH5019MotorShield.h"  //Motor driver library
#include "HX711.h"  //Load cell library

DualVNH5019MotorShield md;

HX711 scale(5, 7);

const int button1Pin = 3;     //Set position button
const int button2Pin = 10;    //Jog extend
const int button3Pin = 11;    //Jog retract
const int button4Pin = 12;    //Run Test
const int ledPin =  13;      // the number of the LED pin

const int x0 = 280;
const int testSpeed = 60;

// variables will change:
int potPin = A3;
int button1State = 0; 
int button2State = 0;
int button3State = 0;
int button4State = 0;

float calibration_factor = -49640;

void stopIfFault(){  //Stops motor in event of fault
  if (md.getM1Fault()){
    Serial.println("M1 fault");
    while(1);
  }
}

void setup() {
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
  pinMode(button3Pin, INPUT);
  pinMode(button4Pin, INPUT);
  Serial.begin(115200);
  md.init();  //initialize motor
  
  scale.set_scale();                      // this value is obtained by calibrating the scale with known weights; see the README for details
  scale.tare();				        // reset the scale to 0

}

void loop() {
  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);
  button3State = digitalRead(button3Pin);
  button4State = digitalRead(button4Pin);
  
  scale.set_scale(calibration_factor); //Set CAL factor
  
  //Button 1 sets the rod to the correct guage length
  if (button1State == HIGH) {
   if (analogRead(potPin) > x0){
    do{
      md.setM1Speed(400);
      stopIfFault();
      //Serial.println("Extending");
      float disp = analogRead(potPin)/1023.00*55.00;
      //Serial.println("Displacement = ");
      Serial.println(disp); 
      Serial.println(analogRead(potPin));
    } while (analogRead(potPin) > x0-100);
    do{
      md.setM1Speed(-50);
      stopIfFault();
      //Serial.println("Extending");
      float disp = analogRead(potPin)/1023.00*55.00;
      //Serial.println("Displacement = ");
      Serial.println(disp); 
      Serial.println(analogRead(potPin));  
    } while (analogRead(potPin) < x0);
    md.setM1Speed(0);
    }
   else if (analogRead(potPin) < x0) {
    do {
      md.setM1Speed(-50);
      stopIfFault();
      //Serial.println("Extending");
      float disp = analogRead(potPin)/1023.00*55.00;
      //Serial.println("Displacement = ");
      Serial.println(disp); 
      Serial.println(analogRead(potPin));  
    } while (analogRead(potPin) < x0);
    md.setM1Speed(0);
    }  
   else {
    Serial.println("At position");
   }
 }
  
  //Buttons 2 & 3 allow the rod to jog slightly back and forth to adjust incase of misalignment
  else if (button2State == HIGH) {
    md.setM1Speed(50);
    stopIfFault();
    float disp = analogRead(potPin)/1023.00*55.00;
    Serial.println(disp); 
    Serial.println(analogRead(potPin));
  }
  else if (button3State == HIGH) {
    md.setM1Speed(-50);
    stopIfFault();
    float disp = analogRead(potPin)/1023.00*55.00;
    Serial.println(disp); 
    Serial.println(analogRead(potPin));
  }
  else if (button4State == HIGH) {
    Serial.println("Time Displacement Load");
    do {
      md.setM1Speed(-testSpeed);
      stopIfFault();
      float disp = analogRead(potPin)/1023.00*55.00;
      float w = scale.get_units();
      Serial.print(millis());
      Serial.print(" ");
      Serial.print(disp); 
      //Serial.print(analogRead(potPin));  //Raw location data output
      Serial.print(" ");
      Serial.println(w, 4);
      int wi = w*100;
      //Serial.println(wi);
      if (analogRead(potPin) > x0+100  && w*100 < 5){
        break;
      }
    } while (analogRead(potPin) < x0+400);
  }
  else {
    md.setM1Speed(0);
  }
}

On a quick glance, it looks like the frequency of the readings is dependent on the period of the loop() iteration because you appear not to have any explicit timing control which would ensure that the readings are taken at say every x microseconds. That means that the more stuff you put in the loop (including Serial.print() etc.), the lower the frequency of the readings.

Incidentally, you should use code tags and not quote when posting code here to prevent your code being treated as formatting instructions for the web page.

@6v6gt Thanks, slimming down my code in the loop() helped with the reading frequency.

And thanks, I updated my post to include code tags.