The whole construction of the project is as follows: Arduino Uno, Capacitive Sensor, JoyIt SBC-MotoDriver 2 and the QSH5718-56-28-126 stepper motor. The motor moves a metal plate, which is connected through a spindel, to a target position entered by the user. The plate should move 5 millimeters per revolution, so 1 mm for 40 steps. The motor is connected to the MotoDriver 2, which is connected to the Arduino. My problem is, that the more distanced the target position is, a bigger deviation from that target position occures. The range is from 0 to 1000 millimeters. The motor/ the plate is accurate at a target position of 100 millimeters. At 250 millimeters, the plate moves 1 millimeter too short, at 500 millimeters the deviation is 2 millimeters too too short and at 1000 millimeters the error is 3 millimeters too short. This is part of a project from someone else, which I have to optimize. So I tried to write a new code myself for controlling the Plate (which is the one above). For that, I tried to write the code in a way that person did. So I did not use the AccelStepper library as I have seen in every Stepper Motor guide now. My code is as follows:
int motorSpeed = 6;
int CalibrSensor = 5;
const in stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
unsigned int stepCount = 0;
int dirStep = 1; //backwards
int mdirStep = -1; //forward
const int stepsPerMillimeter = stepsPerRevolution / 5; //5 millimeters per 200 steps -> 1 millimeter per 40 steps
long int targetPosition = 0; //desired location in Steps, initiated as 0 for calibrating
long int stepsToMove = 0; //Steps required to reach target position
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(CalibrSensor, INPUT);
//Calibration of Testbench, set zero point/starting point
//CalibrSensor goes to LOW, when Plate is on zero point
//Plate moves backwards, until zero point is reached
while (digitalRead(KaliSensorIn) == HIGH && targetPosition == 0) {
myStepper.step(dirStep);
delay(motorSpeed);
stepCount--;
}
//Turn off current
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
stepCount = 0;
Serial.println("Testbench calibrated. Starting Point set.");
}
void loop() {
while (stepCount != targetPosition) { //loop, as long as targetPosition is not reached
stepsToMove = targetPosition - stepCount;
if (stepsToMove > 0) { //move forward, if targetPosition is in postive direction
myStepper.step(mdirStep);
delay(motorSpeed);
stepCount++;
} else { //move backward, if targetPosition is in postive direction
myStepper.step(dirStep);
delay(motorSpeed);
stepCount--;
}
}
Serial.print("Step count at current destination: "); //print current stepCount after reaching destination
Serial.println(stepCount);
Serial.println("Enter a new target position in mm: "); //ask for new target position in mm through Serial Monitor
while (Serial.available() == 0) { //wait for user input and do nothing
}
if (Serial.available() > 0) { //if user entered target position
targetPosition = Serial.parseInt(); //get target position as an integer
}
targetPosition = targetPosition * stepsPerMillimeter; //convert target position to steps needed for reaching position
Serial.print("New Target Position: "); //print new target position
Serial.println(targetPosition);
}
My guess is, that the motor looses steps with greater distance and thus the plate does not move far enough to the target position. But I can't seem to find the reason for that in my program, so it could be a hardware problem. The cooling body on the MotoDriver does get really hot, so that you can't touch it with bare hands. But the motor itself stays cool, so I wasn't sure if that was the problem. As I am not an expert at this, more of a beginner/intermediate, I was hoping for answers to the problem or possible solutions, recommendations or tips for the project. I would be more than happy for every form of help, if there is any more information needed I will provide it.