Accelstepper multiple movement commands

I am using Accelstepper lib to drive 2 stepper motors, I couldn't find an example of using multiple movement commands, so I tried myself.

This is what I came up with, and it is working perfectly, however I feel there must be an easier way to manage doing this! If anyone has some ways I could improve this it would be great.

I need to move to a few different positions so I am using a switch statement to cycle through the moveTo(); commands then breaking out to run the stepper.run(); to actually move the stepper. This needs to be run very fast.

Ideally I would like to put this into a function to be called in the main loop, but so far I could not get that to work.

Any comments on how I could improve would be greatly appreciated. Thanks!

if (runProduction == true) {

  if (clampStepper.distanceToGo() == 0) // Make sure there is not a move happening
    {

    switch (clampMotorStatus) // cycles through each command to change the position
    {
      case 1:
      clampStepper.setMaxSpeed(5000);
      clampStepper.setAcceleration(5000);
      clampStepper.moveTo(stepsPerMMClamp*50);
      clampMotorStatus ++;
      break;

    case 2:
      //clampStepper.setMaxSpeed(1000);
      //clampStepper.setAcceleration(1000);
      clampStepper.moveTo(stepsPerMMClamp*30);
      clampMotorStatus ++;
      break;

    case 3:
      //clampStepper.setMaxSpeed(1000);
      //clampStepper.setAcceleration(1000);
      clampStepper.moveTo(stepsPerMMClamp*40);
      clampMotorStatus ++;
      break;

    case 4:
      //clampStepper.setMaxSpeed(1000);
      //clampStepper.setAcceleration(1000);
      clampStepper.moveTo(stepsPerMMClamp*0);
      clampMotorStatus = 1;
      break;

      }
  }    

  clampStepper.run();  // Start moving the stepper

}

jackdavies:
I need to move to a few different positions so I am using a switch statement to cycle through the moveTo(); commands then breaking out to run the stepper.run(); to actually move the stepper. This needs to be run very fast.

Your code only moves one motor but you said there are two.

Why does it need to run very fast? And what needs to run fast - the motor, or the changes of position?

Your code does not test for whether the motor has reached the position.

If you want to put that piece of code in a function you just need to precede it with

void myFunction() {

and follow it with a closing }. Then, in loop() call it with myFunction();

(I assume you will choose a more meaningful name for the function)

Maybe have a look at Planning and Implementing a Program

...R

Hi Robin,

Thanks for your reply, I should have been more clear. The loop of calling the stepper.run(); needs to be very fast to keep generating pulses for the motor as I understand, so that needs to be constantly running. However I need to move a motor to a position and then change to another specific position. All while the loop is running to actually move the motor.

I know the syntax of writing a function, but basically I wanted to just call a function to do the motor commands, but with the needed loop of stepper.run(); I just got that to work by also moving stepper.run();inside the function.

Really my main question was is that a good method of updating the motor position using the switch case statement? Or is there a more elegant way that I am missing?

Cheers, Jack

jackdavies:
I just got that to work by also moving stepper.run();inside the function.

It would be more usual to have stepper.run() (for all the motors) in loop() so it gets called all the time.

Really my main question was is that a good method of updating the motor position using the switch case statement? Or is there a more elegant way that I am missing?

Your SWITCH/CASE system seems fine.

...R