# Problem with AccelStepper Library

Hello,

I got a problem with the AccelStepper Library. I’m trying to write a simple sketch that moves a stepper motor to coordinates that are stored in an array. The sketch may not make sense but I need it to understand how to use the Library. The motor should move 10 steps forward and then 10 steps backward and so on, but it just keeps moving into one direction.

Here is my code:

``````#include <AccelStepper.h>
#include <Wire.h>

float v = 100;

// wrappers for the first motor!
void forwardstep1() {
myStepper1->onestep(FORWARD, DOUBLE);
}
void backwardstep1() {
myStepper1->onestep(BACKWARD, DOUBLE);
}

AccelStepper stepper1(forwardstep1, backwardstep1);

signed long path[4]={10, -10, 10, -10};

void setup()
{
AFMS.begin(); // Start the top shield
stepper1.setCurrentPosition(0);
}

void loop()
{
int i;
signed long x;
for(i=0; i<4; i++){
x = path[i];
stepper1.setSpeed(v);
stepper1.move(x);
while(stepper1.distanceToGo() != 0){
stepper1.runSpeed();
}
}
}
``````

I already checked the description of the library AccelStepper: AccelStepper Class Reference and the library code but I cannot find my mistake…
I would be glad if somebody could help me.

I believe your solution is simple. For some functions in the AccelStepper library it is necessary to indicate that the the speed is negative.

Here is what I would do:

``````#include <AccelStepper.h>
#include <Wire.h>

float v = 100;

// wrappers for the first motor!
void forwardstep1() {
myStepper1->onestep(FORWARD, DOUBLE);
}
void backwardstep1() {
myStepper1->onestep(BACKWARD, DOUBLE);
}

AccelStepper stepper1(forwardstep1, backwardstep1);

signed long path[4]={10, -10, 10, -10};
signed long pathspeed[4]={v, -v, v, -v};

void setup()
{
AFMS.begin(); // Start the top shield
stepper1.setCurrentPosition(0);
}

void loop()
{
int i;
signed long x;
for(i=0; i<4; i+[b]+){
x = path[i][/b];
v = pathspeed[i];
stepper1.setSpeed(v);
stepper1.move(x);
while(stepper1.distanceToGo() != 0){
stepper1.runSpeed();
}
}
}
``````

Much better, use acceleration, so use setMaxSpeed, setAcceleration, moveTo and run