Hey guys,
I'm working on a project in which a load cell takes measurements while a stepper motor rotates at a constant speed up to a certain distance. I'm having a few problems with this project. Firstly, I've used the HX711 library and AccelStepper, but it seems that the runPositionTo() function doesn't allow anything else like reading a load cell. Next, based on this thread: https://forum.arduino.cc/t/stepper-motor-running-while-collecting-load-cell-data/630540,
I ran the stepper with a pulse delay and it worked fine until I used millis() and the load cell; The motor finally worked and the arduino read the load cell between steps. However, even if I decrease the microstep, it seems that the load cell reading time adds too much time and the motor isn't smooth. I haven't made any code to calculate the read time, that's probably my next step.
Here's the code:
#include "HX711.h"
HX711 scale;
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 3;
const int stepPin = 9;
const int dirPin = 8;
const int enPin = 10;
bool condition = true;
bool b=false;
float pas = 0.0079375; //mmm/step
float sT = 200; //step/revolution
float microst = 800; //pulse/rev from the driver
float distance = 2; //distance to acheive in mm
float s_distance=round(distance*microst/(sT*pas));
//Vitesse quasi-statique de mm/min a step/s
float speed = 5; //speed in mm/min
float step_speed = speed*microst/(sT*pas*60); //speed in step/s
unsigned long curMillis;
long prevStepMillis;
int pulseWidthMicros = 20; // microseconds
unsigned long millisBetweenSteps = round(1/step_speed*1000); // milliseconds
int step = 0; //number of step
unsigned long deltat = 0;
void setup() {
Serial.begin(115200);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
digitalWrite(enPin, HIGH);
//Serial.println("HX711 Demo");
// Serial.println("Initializing the scale");
// Initialize library with data output pin, clock input pin and gain factor.
// Channel selection is made by passing the appropriate gain:
// - With a gain factor of 64 or 128, channel A is selected
// - With a gain factor of 32, channel B is selected
// By omitting the gain factor parameter, the library
// default "128" (Channel A) is used here.
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
Serial.print("Before setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight (not set yet)
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight (not set) divided
// by the SCALE parameter (not set yet)
scale.set_scale(41533.f); // this value is obtained by calibrating the scale with known weights; see the README for details
scale.tare(); // reset the scale to 0
Serial.print("After setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average: \t\t");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t");
Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare()
Serial.print("get units: \t\t");
Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight, divided
// by the SCALE parameter set with set_scale
}
void loop() {
curMillis = millis();
if (step < s_distance && condition) {
digitalWrite(enPin, LOW);
digitalWrite(dirPin, HIGH);
digitalWrite(stepPin, HIGH);
delayMicroseconds(pulseWidthMicros);
digitalWrite(stepPin, LOW);
prevStepMillis = curMillis;
condition=false;
}else if(step >= s_distance){
digitalWrite(enPin, HIGH);
digitalWrite(enPin, HIGH);
Serial.println("Restart in 5 sec");
delay(5000);
step=0;
}
if(condition==false){
if(deltat<millisBetweenSteps){
deltat=curMillis - prevStepMillis;
Serial.println(scale.get_units(1), 2);
}
else if(deltat>=millisBetweenSteps){
step=step+1;
Serial.print("Nb of step ");
Serial.println(step);
deltat=0;
condition=true;
}
}
}
The code runs at a speed of 5mm/min and to a distance of 2mm. It repeats it with a 5 secondes pause between each cycle. When I comment Serial.println(scale.get_units(1), 2); the motor runs smoothly at the right speed and distance. Also, I use a Arduino uno minima R4.
Has anyone had the same problem and found a solution? I'm still a beginner in arduino programming so any help would be greatly appreciated. Also, I've seen that the HX711 rate can be changed from 10Hz to 80Hz but I haven't tried it.
Thank you in advance!