Stepper Motor Variable Position/Speed Control

Good evening/morning everyone,
I have a stepper motor attached to a ball screw so when the motor rotates the ball screw moves forward and backward in certain distances and times. Normally if I would want the ball screw to move in a constant distance over time I would translate that to the number of pulses that the motor should rotate to move the ball screw that distance. But I would like the ball screw to move in a number of different distances that change over time, so I would have a lot of different numbers of pulses to input to the motor. How do I do that? and lastly, how do I control the speed aka. the Pulses per second aka. frequency of the motor? I would LOVE for you to help write my code.
Thank You,
Omar.

For the business of motor speed have a look at this Simple Stepper Code - especially the second, non-blocking example. The first example is really only suitable for simple tests.

It's not clear from your Post how many different distances you want to be able to choose from or how you would select the appropriate one. It will be easier to give useful advice when we know those things.

...R
Stepper Motor Basics

Many Thanks to you good sir for taking the time for replying. I will look up the codes you provided. As for the matter of the linear distances I want my ball screw to do, I have an excel sheet of Linear distances with time and I want the ball screw to do those linear distances. So I converted these distances over time to the corresponding pulses. for the sake of example let's say I have 30 different values of the corresponding pulses. How do I input these values to the stepper motor?

OmarAhmedAbdulAzziz:
for the sake of example let's say I have 30 different values of the corresponding pulses. How do I input these values to the stepper motor?

You have not explained how that data is intended to be used. For example, will a user press a button to select a particular one of the 30 selections of steps?

If you know the step values when you are writing the program they can probably be stored in an array.

Be aware, also that 30 would be no problem but 300 may be and 3000 almost certainly would be. Be sure you are giving us a realistic number.

Another thought - is it possible to derive the number of steps from a calculation?

It would be very much easier to give useful advice if you tell us what you are actually trying to create.

...R

Dear Robin2,
The Ball Screw I’m attaching to the stepper motor is the driving part of a machining robot I’m creating so, In order for my robot to be in a certain point in space the ball screw need to go a certain distance in a certian time right? You could just translate that to pulses and input it to the stepper motor and you’re done. Splendid. But what if I want my robot to go over a number of points in space not just one. Think of it as a path for the robot. That’s why I created an excel sheet for the distances the ball screw should go over time and translated that to the number of pulses needed. So the point is, I need to input all the values of the pulses in an array so they would all be executed in order accordingly. How do I create this array and how do I input it the motor. Also consider that each element in the array needs to be executed by the motor in a second. For example, the first element in my pulses array is 5000 pulses and the second is 7500 pulses. I need those 5000 pulses to be executed in the first second. The 7500 in the second after that. And pardon me for not giving the exact number of values I want to store in my array I’m currently working on it maybe you would just help me with the concept.

I mentioned earlier that your text is hard to absorb because it is all one big paragraph. See how much easier it is to read when there is a little white space added to separate the different elements.

The Ball Screw I'm attaching to the stepper motor is the driving part of a machining robot I'm creating so, In order for my robot to be in a certain point in space the ball screw need to go a certain distance in a certian time right?

You could just translate that to pulses and input it to the stepper motor and you're done. Splendid.

But what if I want my robot to go over a number of points in space not just one. Think of it as a path for the robot. That's why I created an excel sheet for the distances the ball screw should go over time and translated that to the number of pulses needed.

So the point is, I need to input all the values of the pulses in an array so they would all be executed in order accordingly. How do I create this array and how do I input it the motor.

Also consider that each element in the array needs to be executed by the motor in a second. For example, the first element in my pulses array is 5000 pulses and the second is 7500 pulses. I need those 5000 pulses to be executed in the first second. The 7500 in the second after that.

And pardon me for not giving the exact number of values I want to store in my array I'm currently working on it maybe you would just help me with the concept.

...R

You can certainly put values into an array and draw them out one by one to be used. As you don’t want to use each value instantly it is probably best not to use a FOR loop. Instead I would have a variable that acts as an index to the array and I would increment that after every move - something like this

// at the top of the program
int moveList[5] = {123, 5613, 476, 12773, 3456};
byte listIndex = 0;
maxMoves = 5;

// in loop()
if (listLindex < maxMoves) {
   stepsForNextMove = moveList[0];
   // code to implement the move
   listIndex ++;
}

Your suggestion that the move must be completed in one second causes me some concern. You must ensure that the number of steps is capable of being implemented within 1 second. Assuming you are satisfied that the number of steps can be implemented in a second then a simple way to figure out the interval between steps is by dividing 1 million by the number of steps. That will give you the number of microseconds between steps.

However I can’t imagine why it is necessary to divide up the steps into 1 second intervals. I would have thought the important thing is the total distance to move to get the tool to wherever it needs to go. If the 1 second time interval has something to do with coordinating the movements of this motor with something else then I strongly suspect that is the wrong way to go about it.

Which brings me back to the last paragraph of my Reply #3

…R

I'd like to apologize for stuffing up the whole thing in one long paragraph.

And also many thanks to you for helping me know how to create and code the array.

The whole thing of dividing the steps into 1second intervals is my idea of speed control of the stepper motor.

I have this idea in my head that stepped motor speed is directly proportional to frequency and frequency is the amount of steps per second.

That's why I would like for each number of steps to be executed by the motor in 1 second.

Is this even valid? And if so how can I code it?

OmarAhmedAbdulAzziz:
The whole thing of dividing the steps into 1second intervals is my idea of speed control of the stepper motor.

I have this idea in my head that stepped motor speed is directly proportional to frequency and frequency is the amount of steps per second.

The speed of a stepper motor is indeed set by the number of steps per second.

However I get the impression you are telling us about HOW you think something should be implemented without telling us WHAT needs to be implemented. This is a classic XY problem.

I have a very strong feeling that we will probably recommend a different approach which may well be a great deal simpler if only you would describe the requirement without regard to how it might be implemented.

Back (again) to my request at the end of Reply #3

...R

If you are really interested in using your machine for milling, look at GRBL. It's all programmed for you.

Paul

Paul mentioned GRBL which can take care of a lot of the headaches for you if you want to use ballscrews (or any screws) for absolute positioning.

There are other actual libraries out there too which can also help you take away a lot of the programming needed and make it simpler if you don’t want to use it for machining.
Robin covered only a small subset of the many libraries and methods out there.

ballscrewbob:
Robin covered only a small subset of the many libraries and methods out there.

Because he was / is hoping the OP would / will clarify what he is actually trying to create.

...R