AccelStepper.h, motors will not move.

Hello, I am very new to Arduino. I am doing a project where I am moving two stepper motors NOT simultaneously (they take turns), where each moves a fixed distance, stops, and moves forward again until a max distance is reached, and then returns to the original position. I want to move at a constant speed, but I need to use Accelstepper library for a non-blocking movement, so that it can check for a limit switch to be pressed during the movement distance.

However, when I upload the script to the arduino, the motor will not move. It sounds like it’s buzzing, but nothing is happening. I’ve tried to change the script around, but the problem persists. And the motors do have enough power to move because another script I have moves the motors fine. Is there anything I’m doing wrong with the code I’ve written (I’ve taken the limit switch code and one motor out for simplicity)? And also, is it feasible to use the built in Stepper.h library instead to check for the switches during movement?

#include <Accelstepper.h>

int steps = 10000;

AccelStepper stepper1(1,3,4);

int X = 0;

int Xmax = steps*5;

void setup() {}

void loop() {
while (X < Xmax)
{
stepper1.move(steps);
stepper1.setSpeed(1000);
while (stepper1.distanceToGo() != 0)
{
stepper1.runSpeedToPosition();
}
delay(2000);
X = X + steps;
}

stepper1.move(-X);
stepper1.setSpeed(1000);
while (stepper1.distanceToGo() != 0)
{
stepper1.runSpeedToPosition();
}
X = 0;
}

Have you had one motor moving properly with any other code?

If not try this Simple Stepper Code which does not need any library. Get the motor working with simple code before trying anything more complex.

If you need more help then ...

  • post a link to the datasheet for your stepper motor
  • tell us what stepper motor driver you are using
  • tell us what stepper motor power supply you are using (volts and amps).

...R
Stepper Motor Basics

The motors have been running using a different script based on the built in Stepper Library, which is why I believe it's a programming issue. Can the power demand change using a different library?

And also, would it be feasible to use the Stepper library or the Basic code you wrote for checking for limit switches?

If your motors work with another program then that eliminates the doubt that was in my mind - eliminate potential problems one by one :slight_smile:

I suggest you get rid of all the WHILEs from your program. They are not necessary.

Try this and see what happens

void loop() {
    if (X < 2)
    {
        stepper1.move(steps);
        stepper1.setSpeed(1000);
        stepper1.runSpeedToPosition();
        delay(2000);
        X = X + 1;
    }
}

By the way loopCount would be a much more meaningful name than X.

Also, single character variable names can make debugging very difficult.

…R

Thank you for the code sample, I’ll try it when I get a chance. However, if I add a second motor, say, stepper2, and I want it to do the same thing as stepper1, but not simultaneously, where it only turns after stepper1 is finished, wouldn’t a loop like that force them to move at the same time?

void loop() {
if (loop1 < 2)
{
stepper1.move(steps);
stepper1.setSpeed(1000);
stepper1.runSpeedToPosition();
delay(2000);
loop1 = loop1 + 1;
}

if (loop2 < 2)
{
stepper2.move(steps);
stepper2.setSpeed(1000);
stepper2.runSpeedToPosition();
delay(2000);
loop2 = loop2 + 1;
}

}

mbr29:
Thank you for the code sample, I'll try it when I get a chance. However, if I add a second motor, say, stepper2, and I want it to do the same thing as stepper1, but not simultaneously, where it only turns after stepper1 is finished, wouldn't a loop like that force them to move at the same time?

First off, the purpose of my code was ONLY to ensure that you can get one motor to work with code that has some resemblance to your own code.

Second, the easiest way to answer many Arduino questions is to try something and see what happens. The Arduino system is great for learning by doing.

Third, there is a mistake in my code. I had (incorrectly) thought that runSpeedToPosition() would complete its move before moving on to the next line of code - i.e. block the Arduino until it finishes. However I don't think it does. The blocking functions are runToPosition() and runToNewPosition() and both of them use acceleration. Consequently you have two choices {A} replace runSpeedToPosition() with runToPosition() in the code in Reply #3 - in which case you will get the sequential behaviour that you want. OR {B} you need to use code something like this

char stepper1State = 'W'; // Paused Waiting Running Done
char stepper2State = 'P';



void loop() {
    if (stepper1State == 'W') {
        stepper1State = 'R';
        stepper1.move(steps);
        stepper1.setSpeed(1000);
    }
    else {
        if (stepper1.distanceToGo() ==  0) {
            stepper1State = 'D';
            stepper2State = 'W';
            delay(2000); // if you want a delay between movements
        }
    }
    
    if (stepper2State == 'W') {
        stepper2State = 'R';
        stepper1.move(steps);
        stepper1.setSpeed(1000);
    }
    else {
        if (stepper1.distanceToGo() ==  0)
            stepper2State = 'D';
            stepper1State = 'W'; // if you want the sequence to repeat
            delay(2000); // if you want a delay between movements
        }



    stepper1.runSpeedToPosition();
    stepper2.runSpeedToPosition();
}

...R

Normally you'd call stepper1.run() and stepper2.run() every time round loop, and
then call move() or moveTo() whenever you decide to start a movement, let loop() do the work,
no delay()'s, no whiles.