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>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
#include <Adafruit_MCP23017.h>
Adafruit_MotorShield AFMS(0x60); // Default address, no jumpers
Adafruit_StepperMotor *myStepper1 = AFMS.getStepper(200, 1);
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.