Need help with stepper motor profile with Accelstepper.h

Hi guys,

This is my first time posting here. I currently have a project where we need to make a profile for a stepper motor with accel, constant speed, decel, and a delay.

After some research, i found that the accelstepper library has the acceleration and deceleration function.

Below is the profile that i am required to make.

Below is the code i used

#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::FULL4WIRE, 8, 9, 10, 11);
//no of step to move to achieve 40step aka 20ms

void setup()
{  
  //setup accel speed 20steps/sec,accel: 5steps/sec. Accel/decel should be 4s (20/5)
  stepper.setMaxSpeed(20); 
  stepper.setAcceleration(5);
}



void loop()
{
  if (stepper.distanceToGo() == 0)
  {
    //delay 3s here for the idle
       delay(3000);  
// set the distance to move 120steps = 80steps for accel+decel, 40steps constant speed. 40steps = 2 secs.
       stepper.move(120);
  }
          stepper.run();

 }

The distance is calculated with D=V*V/2A *V= max speed, A=Acceleration.

From the code, one cycle should be around 10second (exclude idle). When i try to use a stop watch, it is around 9+ seconds.

i am using LED to check the timing of the profile now

I would like to know if i am coding it correctly? or the stop watch was not timed properly?

The AccelStepper library bases its acceleration profile on the number of steps and the acceleration rate, not on time. And it is designed to accelerate from 0 to the max speed and then decelerate back to 0 within the number of steps in a single movement.

You may be able to figure out a number of steps and an acceleration rate that has the by-product of producing the profile you require.

Alternatively it is not difficult to write your own acceleration code. The code in this link should give you a starting point.

...R

Hi Robin,

Thanks for the reply.

I have tried to understand your code in the other thread. It seems to be a little too advance for my level. I am quite new to this actually.

From your code, seems like there is only 2 pins used. 1 for direction and 1 for steps only.

Can you provide some simple guide for me?

lcs1987:
Can you provide some simple guide for me?

Sorry. Just realized I forgot to attach these links to my earlier Reply.

Stepper Motor Basics
Simple Stepper Code

...R