Controling Load cell and Motor nema at the same time

Good evening,

I am working on a small project that consists of pressing a grape (by means of an endless screw mechanism that is driven by a stepper motor) against a load cell (I am using the HX711 amplifier)
During the whole process, I need to read the value of the load cell continuously until the grape explodes, to later draw a curve of the behavior of the grape.

I have used the PROTOTHREADS library to simulate a “Parallel” operation of the stepper motor turning, while logging the load cell data every 10 milliseconds.

I leave you the code that I have made:

#include <pt.h>
#include "HX711.h"
#include <AccelStepper.h>

AccelStepper stepper_motor(1, 10, 9);

struct pt hilo1;
struct pt hilo2;
struct pt hilo3;

HX711 scale(6, 5); //HX711 scale(6, 5)
float calibration_factor = -443;

void setup() {
  Serial.begin(9600);
  Serial.println("HX711 weighing");
  scale.set_scale(calibration_factor);
  scale.tare();
  Serial.println("Lecturas:");

  stepper_motor.setSpeed(3200);
  stepper_motor.setAcceleration(800);
  stepper_motor.setMaxSpeed(6400);

  PT_INIT(&hilo1);
  PT_INIT(&hilo2);
}

void loop() {
  lectura(&hilo1);
  bajar(&hilo2);

}
void lectura(struct pt *pt){
    PT_BEGIN(pt);
   
    float units;
    float ounces;

    static long t = 0;

  do {
    Serial.print("Leyendo:");
    units = scale.get_units(),10;
    if (units < 0)
    {
      units = 0.00;
    }
    ounces = units * 0.035274;
    Serial.print(units);
    Serial.println(" gramos");
    t = millis();
    PT_WAIT_WHILE(pt, (millis()-t)<10);
   } while(true);
  PT_END(pt);
}
void bajar(struct pt *pt){
    PT_BEGIN(pt);

   static long t = 0;
  //do {
    stepper_motor.moveTo(-4200);
    stepper_motor.run();
    t = millis();
    PT_WAIT_WHILE(pt, (millis()-t)<1);
   //} while(true);
  PT_END(pt);
}

I get them to work in parallel, but the operation of the stepper motor is not as smooth as it should be (it makes a microstop for each step it does), in fact when I only run the motor, it works correctly for me.
I don’t know how I can solve this problem.
I appreciate any help or suggestions. Thanks a lot.

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