# How to control stepper motors simultaneously with acceleration

Hey guys,
I have been working on code to make a camera slider with a pan and tilt head for a while now. I am done except for the actual movement of my 3 stepper motors. I want to have them to all start moving at the same time.

I can not use the MultiStepper library for this as I need the motors to accelerate and decelerate to minimise shake of my camera. I have tried and googled many things but I simply can't get it to work how I want it to. j

Just a note I don't care if the motors finish at different times just as long as they start and move together and not have one start then finish and then the 2nd start and finish and so on. All I need is a code where each stepper moves a certain amount of steps with acceleration and start at the same time.

I would post the code I have now but the part that I am stuck with will only make sense if you see the rest of the code which is several hundred lines long.

Many Thanks,
Brennen

It's not very difficult to write your own acceleration code if you are using stepper drivers that take step and direction signals. This Simple acceleration code should give you some ideas. I have used it with 3 motors on a small CNC lathe.

IIRC I did the acceleration calcs for the motor with the most steps in a move and then slaved the steps for the other motors from that.

...R

Robin2:
It’s not very difficult to write your own acceleration code if you are using stepper drivers that take step and direction signals. This Simple acceleration code should give you some ideas. I have used it with 3 motors on a small CNC lathe.

IIRC I did the acceleration calcs for the motor with the most steps in a move and then slaved the steps for the other motors from that.

…R

I kinda understand that code I just have one question.

What is ‘numAccelSteps’? Do I just leave it as a 100 or do I have to assign it some sort of value according to my code and also what is ‘byte direction = 1;’ for?

Thanks
Brennen

You can set numAccelSteps to any value you choose. It is the number of steps over which the change from 0 to full speed takes place - and from full-speed to 0.

The direction variable just controls the direction of the motor.

...R

This code works fine by itself but as sson as I put it in my slider project, when it is activated the motor will only do a single step for some reason.

BrenCar12:
This code works fine by itself but as sson as I put it in my slider project, when it is activated the motor will only do a single step for some reason.

How can I comment without seeing the complete program? The devil is in the detail.

...R

Robin2:
How can I comment without seeing the complete program? The devil is in the detail.

...R

Yes I was always going to show you the code i just have to simply tidy it up first.

Robin2:
How can I comment without seeing the complete program? The devil is in the detail.

...R

Is there another way to show you my code other than copying and pasting it using the code inserter on the forum as the code is too long to be copied in like that. Can I send/attach the file or can I share a link somehow?

You can add your .ino file as an attachment. If you click "Preview" at the bottom of the Quick Reply editor window it brings you to the full editor where you can add attachments.

...R

Thanks. The code is attached there below.

Motorised_Camera_Slider.zip (4.55 KB)

You have attached a ZIP file and I don't like opening strange ZIP files.

...R

Sorry I thought u would prefer it that way here is the .ino file.

Motorised_Camera_Slider.ino (24.1 KB)

It's a very long program so it is hard to know what parts are critical.

In this piece

``````    delay((seconds - interval) * 1000); //motorStopDuration
movexStepper();
``````

there is a delay() (I have no idea for how long) which probably does not sit well with the use of millis() in the piece you have taken from my code.

...R

Robin2:
It's a very long program so it is hard to know what parts are critical.

In this piece

``````    delay((seconds - interval) * 1000); //motorStopDuration
``````

movexStepper();

``````

there is a delay() (I have no idea for how long) which probably does not sit well with the use of millis() in the piece you have taken from my code.

...R
``````

That delay is can range from 1 to 30 seconds
And it is necessary to have those 1 to 30 seconds as that is the time it has to wait for the photo to be taken before it can move. I can rearrange it so that that delay comes after the motor movement if that helps?

BrenCar12:
I can rearrange it so that that delay comes after the motor movement if that helps?

I suspect that would be essential rather than just helpful. The way it is now it is interposed between each step.

...R

So if I just put that delay after the moving code is done it should be fine then right? It won't matter that that whole part is in a for loop until all photos are taken?

BrenCar12:
So if I just put that delay after the moving code is done it should be fine then right? It won't matter that that whole part is in a for loop until all photos are taken?

The Arduino system is great for learning-by-doing.

I don't like long-running FOR or WHILE loops. And by "long running" I mean anything that takes longer than a few millisecs. Have a look at how the code is organized in Several Things at a Time

Note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete a task in one call. And there may be dozens of calls to a function before it is actually time for it to do anything.

...R

It does the same thing whether the delay is before or after as because it is in a for loop it does it over so the delay seems like it is before the next movement of the motor. Is there any way to modify that motor code so that the Arduino can't move on to the next process 'aka' the delay until it is fully completed turning the motor? I just thought of something but would using the millis() function in a way like a delay help as it will not block/stop the motor code from running like the delay does as it is a non blocking function. And when I mean use like a delay in mean have the motor code within the millis() function so that it can complete the full motor turning and have the millis() function long enough to add on that delay needed between each motor movement?

BrenCar12:
It does the same thing whether the delay is before or after as because it is in a for loop

I thought you meant that you would put the delay after the FOR loop

...R

Robin2:
I thought you meant that you would put the delay after the FOR loop

...R

No it still has to be in the for loop. Will using millis() help or no.