Number of step in using stepper.h library

Dear all,
I am facing an issue while sending an input in Arduino.
I am using an ArduinoUno board + L298N motor driver to control a QSH 4218-51-10-049 stepper motor using LabVIEW (the stepper motor is working in a optical delay line, a linear stage).
That's my arduino code:

#include <Stepper.h>
const long stepsPerRevolution = 200; // Change this value based on your stepper motor
const long distancePerRevolution = 2; // mm

// Define the motor connections
#define motorPin1 8
#define motorPin2 9
#define motorPin3 10
#define motorPin4 11

// Define parameters
long distance = 0; 
long steps = 0;

// Create a new instance of the Stepper class
Stepper myStepper(stepsPerRevolution, motorPin1, motorPin2, motorPin3, motorPin4);

void setup() {
  // Set the speed of the stepper motor (RPM)
  myStepper.setSpeed(80);

  // Initialize the serial communication for debugging
  Serial.begin(9600);
}

void loop() {
  // Check if there is any incoming data from LabVIEW
  while (Serial.available() > 0) {
    String receivedString = Serial.readString();
    long distance = receivedString.toFloat(); // Read the desired distance from LabVIEW
    // Calculate the number of steps required to move the desired distance
    long steps = distance / distancePerRevolution * stepsPerRevolution;
    // Rotate the stepper motor
    // steps < 32768
    if (steps > 0 && abs(steps) < 32768) {        // Moving motor clockwise for 'distance' mm (front to end stage)
      myStepper.step(steps);
    } else if (steps < 0 && abs(steps) < 32768) { // Moving motor counter-clockwise for 'distance' mm (end to front stage)
      myStepper.step(steps);
      // steps > 32767 
    } else if (steps > 0 && abs(steps) > 32767) { // Moving motor clockwise for 'distance' mm (front to end stage)
      myStepper.step(-steps);
    } else if (steps < 0 && abs(steps) > 32767) { // Moving motor counter-clockwise for 'distance' mm (end to front stage)
      myStepper.step(-steps);
      //No movement
    } else {
    }
  }
}

Initially I was facing a problem while loading a number of "steps" larger than 32767: somehow the direction of the movement was reversed. I figured out that the "int" values can assume at maximum that value of 32767 and for larger numbers was not happy, the sign was reversed and the number slightly different. I did manage to fix this issue as shown in the code even if I believe there is a smarter way to do it..... also because now I am getting another problem related to this and I hope someone can help.
I think that the int "steps" are a default setting in the stepper.h library, so even when I load a bigger number with the long format, the number of steps performed by the motor are less than what it should be.
For instance, if I say steps = 35000 the motor rotate only an amount of steps of 30000 ca.

Can someone explain me how to fix it?
Thanks for help,
Edoardo

There is. Use an unsigned integer variable of a type large enough to hold the maximum value required. Even an unsigned int would be better

Thanks! where should I place it?

Use a better stepper library, that can cope with more than 32767 steps - e.g. AccelStepper or my MobaTools library

1 Like

The problem is, that the stepper.h lib uses an int as parameter for the .step method. So the lib itself is limited.

1 Like

Oh dear. What a silly thing for the library to do !

I am trying to use the AccelStepper library (only because there are more documentation and examples online acailable and I am not that familiar with arduino...)

#include <AccelStepper.h>
const long stepsPerRevolution = 200; // Change this value based on your stepper motor
const long distancePerRevolution = 2; // mm

// Define the motor connections
#define motorPin1 8
#define motorPin2 9
#define motorPin3 10
#define motorPin4 11

// Define parameters
long distance = 0; 
long steps = 0;

// Create a new instance of the Stepper class
AccelStepper myStepper(AccelStepper::FULL4WIRE, motorPin1, motorPin2, motorPin3, motorPin4);

void setup() {
  // Set the speed of the stepper motor (RPM)
  myStepper.setMaxSpeed(80);
  // Set the speed of the stepper motor (RPM)
  myStepper.setAcceleration(1);

  // Initialize the serial communication for debugging
  Serial.begin(9600);
}

void loop() {
  // Check if there is any incoming data from LabVIEW
  while (Serial.available() > 0) {
    String receivedString = Serial.readString();
    long distance = receivedString.toFloat(); // Read the desired distance from LabVIEW
    // Calculate the number of steps required to move the desired distance
    long steps = distance / distancePerRevolution * stepsPerRevolution;
    // Rotate the stepper motor
    // steps < 32768
    if (steps > 0) {        // Moving motor clockwise for 'distance' mm (front to end stage)
      myStepper.move(steps);
      myStepper.run();
    } else if (steps < 0) { // Moving motor counter-clockwise for 'distance' mm (end to front stage)
      myStepper.move(steps);
      myStepper.run();
      //No movement
    } else {
    }
    Serial.println(distance);
    Serial.println(steps);
  }
}

That's the code I rewrote but unfortunately is not working... Can you help me to see where I am mistaking?

This command creates at most one step, and you must call it in a loop frequently and fast to make the stepper move. To make it work like your first attemped with stepper.h you can use myStepper.runToPosition(); This is a blocking call just like the .step method of stepper.h, and returns when the stepper has reached the target position.

Your motor will run very slow with these values. Accelstepper doesn't define the RPM but the steps / sec. With 80 RPM and 200 Step/rev you must set it to 267 steps/sec to reach the same speed. And you should set the acceleration to a higher value.

Thanks a lot!! now it works!!
Last thing is that if I insert a value for distance equal to 0.01 it round to 0...how can I import the full value?

You defined distance as long, so it cannot hold fractional values. Define it as float.

1 Like

Thanks a lot!! now it works perfectly!

You're welcome

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