Hello!
I am having a problem with my code lagging when it is calculating the next step for a stepper motor.
My program runs by starting with a reading from a load cell using an HX711 amplifier. While reading a value greater than -0.25, the program runs smoothly. As soon as the value goes below -0.25 and uses the first and second else if starting in line 48, the program starts to lag.
If I comment out lines 48 to 55, the readings are very smooth and consistent. I think the problem is somewhere with the calculation of the next step in these lines.
I have also tried to force the scale.get_units to be an integer before calculation, but there was no difference. I also tried an stm32 nucleo f446re to see if a faster board would help, but it yielded the same result.
I have been troubleshooting, but have not found success. Any help would be greatly appreciated.
Thank you!
// Complete Code for Stepper Motor Control with Arduino and Force Sensor
#include "HX711.h"
HX711 scale;
#include "Stepper.h"
#define Steps 200
Stepper stepper(Steps, 7, 6); // 6 Blue 7 Red
#define DAT 2
#define CLK 3
#define calibration_factor -19450.0 // determined through hx711 cal. code
int Step = 0;
int prev = 0;
int pos = 0;
int newstep = 0;
void setup() {
Serial.begin(9600);
stepper.setSpeed(750L);
scale.begin(DAT, CLK);
scale.set_scale(calibration_factor);
scale.tare();
Serial.println("Readings: ");
}
void loop() {
Serial.print("Reading: ");
Serial.print(scale.get_units(), 2);
Serial.print(" lbs");
Serial.println();
if (scale.get_units() > -0.25)
{
Step = 0;
}
else if (scale.get_units() > -6.3)
{
Step = scale.get_units()*334;
}
else if (scale.get_units()<= -6.3)
{
Step = -6.3 * 334;
}
Serial.print("Step");
Serial.print(Step);
Serial.println();
newstep = Step - prev;
stepper.step(newstep);
pos = newstep + prev;
prev = pos;
Serial.print("ns");
Serial.print(newstep);
Serial.print("pos");
Serial.print(pos);
}
2 posts - 2 participants