Simple stepper movements

5,144 views
Skip to first unread message

laurynas...@gmail.com

unread,
Apr 16, 2013, 11:58:37 AM4/16/13
to accels...@googlegroups.com
Hello,

 I am beginner. So I started to experiment with arduino and accelstepper library and just want to make simple movements with stepper motor (28BYJ-48). I want  Stepper to move 500 positions forward, than back to 0, then forward to 300. I started trying to make stepper move 500 forward with this code:

void setup()
{  
  // Change these to suit your stepper if you want
  stepper.setMaxSpeed(2000);
  stepper.setAcceleration(20);
  
}
void loop()
{
stepper.moveTo(500);
    stepper.run();

This is fine. Stepper rotates to 500 and stops. I add another part of code and this working fine. stepper rotates back to 0:

   stepper.setMaxSpeed(2000);
  stepper.setAcceleration(20);
    stepper.moveTo(0);
    stepper.run();

But when I add third part of code the stepper is not working as supposed to. It will rotate 500, then stops and start rotate very slowly. What is happening? I just added the similar part of code, only with other numbers.  There is the whole code:

void setup()
{  
  // Change these to suit your stepper if you want
  stepper.setMaxSpeed(2000);
  stepper.setAcceleration(20);
   
}

void loop()
{
stepper.moveTo(500);
    stepper.run();
    
      stepper.setMaxSpeed(2000);
  stepper.setAcceleration(20);
    stepper.moveTo(0);
    stepper.run();
    
          stepper.setMaxSpeed(2000);
  stepper.setAcceleration(20);
    stepper.moveTo(300);
    stepper.run();

Thank you for your help.

Sandy Noble

unread,
Apr 16, 2013, 12:55:32 PM4/16/13
to accels...@googlegroups.com
Hi, run() itself will only ever take one step, so it needs to be in its own loop to make much sense:

void loop()
{
  stepper.moveTo(500);  // set target forward 500 steps
  while (stepper.distanceToGo() != 0) 
  {  stepper.run(); }

  stepper.moveTo(0);  // set target back at start position
  while (stepper.distanceToGo() != 0) 
  {  stepper.run(); }

  stepper.moveTo(300); // set target forward 300 steps
  while (stepper.distanceToGo() != 0) 
  {  stepper.run(); }
}

So the above will move 

forward 500 steps, 
backwards 500 steps, 
forward 300 steps,

forward 200 steps
backwards 500 steps
forward 300 steps

and on and on

sandy



--
You received this message because you are subscribed to the Google Groups "accelstepper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to accelstepper...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Sandy Noble

laurynas...@gmail.com

unread,
Apr 16, 2013, 2:23:48 PM4/16/13
to accels...@googlegroups.com

laurynas...@gmail.com

unread,
Apr 16, 2013, 2:25:16 PM4/16/13
to accels...@googlegroups.com
Thank you for quick response. It works now. But  know I want to add another stepper and move them simultaneously in the same order.. When I add part code with other stepper, the code runs first stepper, and than another stepper (code below).  How to run both at the same time?

void loop()
{

  stepper.moveTo(500);  // set target forward 500 steps
  while (stepper.distanceToGo() != 0) 
  {  stepper.run(); }
  
  stepper2.moveTo(500);  // set target forward 500 steps
  while (stepper2.distanceToGo() != 0) 
  {  stepper2.run(); }
}
;

Sandy Noble

unread,
Apr 16, 2013, 3:43:10 PM4/16/13
to accels...@googlegroups.com
The examples that Mike packages along with Accelstepper show this stuff fairly well, but this: 

void loop()
{

  stepper.moveTo(500);  // set target forward 500 steps
  stepper2.moveTo(500);  // set target forward 500 steps
  while (stepper.distanceToGo() != 0 || stepper2.distanceToGo() != 0) 
  {
    stepper.run();
    stepper2.run(); 
  }
}

Will do it.  Cycling through the run loop as long as either stepper still have a distance to go.

sn

laurynas...@gmail.com

unread,
Apr 17, 2013, 11:33:25 AM4/17/13
to accels...@googlegroups.com
That is great! Thank you.

Jose Chacon

unread,
Mar 27, 2018, 1:41:11 PM3/27/18
to accelstepper
thanks Sandy
Reply all
Reply to author
Forward
0 new messages