Move to a defined position. (stepper motor)

Hello dear users

I would be glad to be adviced on some programming questions please :

I would try to move a stepper motor to a defined position.

for example :

if analog voltage is "x" move to step 800
if analog voltage is "y" move to step 200

I understand that I need to home the motor with a limit switch first but cannot figure out the code to move according the previous position to the next one ...
Hope my english will be understandable enough to go further !
Thanks
Philippe

if you use the standard Stepper library, after you "homed" your system you need to remember where you are and issue the request to move based on this.

assume you have 3 postions: home, pos800, pos200

if you are home and voltage is x, then step +800 and remember your are at pos800
if you are home and voltage is y, then step +200 and remember you are at pos200
if you are at pos800 and voltage is x, do nothing
if you are at pos800 and voltage is y, then step -600 and remember you are at pos200
if you are at pos200 and voltage is x, then step +600 and remember your are at pos800
if you are at pos200 and voltage is y, then do nothing

if you are using AccelStepper then after you homed you can register this position being 0 with setCurrentPosition() and the moveTo() function call takes an absolute position so you don't really need to remember where you are. just do moveTo(800) or moveTo(200) based on the voltage

In one of the AccelStepper examples, they introduce the concept of ‘current’ and ‘target’ positions.

From there it’s easy…

In loop(), you compare the two, and initiate a move to cancel out the difference.

You use your step counter and/or encoders / limit switches to keep track of the current position,

To perform any motion, all you do is set a new target, the control loop() takes care of the rest.

Optionally you can adjust the speed if you desire, or you can let the acceleration profile look after that as well.

If you don’t want to use AccelStepper, you can do the magic yourself, but it’s a pretty good library.

Thanks JML

sounds good ! I'll try the moveTo() command.

I don't have space enough in my project to install a motor with its encoder.

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