Go Down

Topic: Problem with AccelStepper Library (Read 926 times) previous topic - next topic

derferdige

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:
Code: [Select]

#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 http://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html#aa4a6bdf99f698284faaeb5542b0b7514 and the library code but I cannot find my mistake...
I would be glad if somebody could help me.

chrisLomo

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:
Code: [Select]

#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, 2);
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();
    }
  }
}

MarkT

Much better, use acceleration, so use setMaxSpeed, setAcceleration, moveTo and run
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up