Make Stepper Motor run continuously while ready load cell

I want the bipolar stepper motor to run continuoulsy while reading the load cell. Currently the motor spins then reads the load cell.

#include "HX711.h"

#define DT_PIN 12
#define SCK_PIN 11
#define STEP_PIN 5
#define DIR_PIN 2
#define EN_PIN 8

HX711 scale;

// Variable to store the offset
float offset = 0;

// Define the step delay (in microseconds) to achieve 2000 Hz frequency
unsigned long stepDelay = 250; // 250 microseconds for 2 kHz frequency
unsigned long lastStepTime = 0; // Variable to store the last step time

void setup() {
  // Setup serial communication for weight sensor
  Serial.begin(9600);
  
  // Initialize HX711 scale
  scale.begin(DT_PIN, SCK_PIN);
  scale.set_scale(454.0); // Set the scale to convert readings to pounds
  
  // Calibration: Read initial weight and set it as the offset
  Serial.println("Calibrating... Please wait.");
  delay(2000); // Wait for 2 seconds to stabilize
  offset = scale.get_units(10) / 2; // Take 10 readings and average them to get the offset
  Serial.print("Initial offset: ");
  Serial.println(offset);

  // Setup stepper motor pins
  pinMode(STEP_PIN, OUTPUT); 
  pinMode(DIR_PIN, OUTPUT);
  pinMode(EN_PIN, OUTPUT);
  digitalWrite(EN_PIN, LOW); // Enable the motor
}

void loop() {
  handleMotorControl();
  handleWeightMeasurement();
}

void handleMotorControl() {
  static unsigned long lastStepTime = 0; // To track the last step time
  static bool stepState = LOW; // To track the step pin state

  // Check if it's time to toggle the step pin
  if (micros() - lastStepTime >= stepDelay) {
    lastStepTime = micros(); // Update the last step time
    
    // Toggle the step pin state
    stepState = !stepState;
    digitalWrite(STEP_PIN, stepState);
  }
}

void handleWeightMeasurement() {
  // Get the average value of 10 readings from the scale
  float weight = scale.get_units(10) / 2;

  // Subtract the offset from the current weight and invert the sign
  float adjustedWeight = -(weight - offset);

  // Print the adjusted weight to the serial monitor
  Serial.print("Weight: ");
  Serial.print(adjustedWeight);
  Serial.println(" lbs");
}

Your motor stepping looks right.

Does this have a "settling" period?

Try

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