Problem learning microstep program

Hi Guys,

I’m learning stepper motor with microstep driver program.

Here is my program.

#include <AccelStepper.h>

AccelStepper stepper(AccelStepper::DRIVER, 2, 3); // 2-PUL,3-DIR

void setup()
{
stepper.setAcceleration(10000);
stepper.setCurrentPosition(0);
stepper.setMaxSpeed(40000);
}
long current_position;
void loop()
{
current_position = stepper.currentPosition();
if(current_position==0){
delay(1000);
stepper.moveTo(-1000);
}
if (current_position==-1000){
delay(1000);
stepper.moveTo(-2000);
}
else if (current_position==-2000){
delay(1000);
stepper.moveTo(0);
}
stepper.run();
}

Well my question is, how can i run the stepper from 0 to -1000 and then to -2000. Also from -2000 to 0.
If i write the program like that, before the -2000 get to 0, and reach -1000, it will go back to -2000.
Anyway, i hope somebody can help me.
Thanks. :smiley:

Rather than checking if(current_position==0){ etc. you first need to check if the current position has reached its destination. Something like this

if (current_position == destination) {

   if (current_position == 0) {
      delay(1000);
      destination = -1000;
      stepper.moveTo(destination);
   }

  if (current_position == -1000) {
      delay(1000);
      destination = -2000;
      stepper.moveTo(destination);
   }

  // etc

}

…R

Hi Mr.Robin,

Thanks for your reply.
Anyway how should i declare the destination?
Can i use this?

#include <AccelStepper.h>
// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, 2, 3); // 13-PUL,12-DIR

void setup()
{
stepper.setAcceleration(30000);
stepper.setCurrentPosition(0);
stepper.setMaxSpeed(10000);
}
long current_position;
long destination;
void loop()
{
if (current_position == destination) {

if (current_position == 0) {
destination = -1000;
delay(500);
stepper.moveTo(destination);
}

if (current_position == -1000) {
destination = -2000;
delay(500);
stepper.moveTo(destination);
}
if (current_position == -2000) {
destination = -3000;
delay(500);
stepper.moveTo(destination);
}
}
stepper.run();
}

But, the stepper motor run just once, from 0 to -3000.
the 0 to -1000, and -1000 to -2000 didn’t work.

Thanks!

But, the stepper motor run just once, from 0 to -3000.
the 0 to -1000, and -1000 to -2000 didn't work.

Are you sure? How do you know?
Maybe it would be clearer if you make the delay()s longer

If you want the sequence to repeat you need a test for current_position == -3000

...R