Equivalent code using AccelStepper library

505 views
Skip to first unread message

Ronneil Camara

unread,
Nov 13, 2013, 12:59:29 AM11/13/13
to accels...@googlegroups.com
What would be the AccelStepper code equivalent to move stepper 25 steps only then delay 1000ms?

In Arduino's Stepper library, I would simply do 

# -------------------------------------------------
#include <Stepper.h>
Stepper myStepper(200, 8,9,10,11);

void setup() {
    myStepper.setSpeed(60);
}
void loop() {
    myStepper.step(25);
    delay(1000);
}
# -------------------------------------------------

Any help would be greatly appreciated.

Thanks,

Neil

Sandy Noble

unread,
Nov 13, 2013, 5:04:36 AM11/13/13
to accels...@googlegroups.com
AccelStepper myStepper(AccelStepper::FULL4WIRE, 8,9,10,11);

void setup() {
    myStepper.setMaxSpeed(60.0);
}
void loop() {
    myStepper.runToNewPosition(myStepper.currentPosition()+25);
    delay(1000);
}

....  alternatively, the more "accelsteppery" kind of way
void loop() {
    myStepper.move(25);
    while (myStepper.distanceToGo() != 0) {
        myStepper.run();
    }
    delay(1000);
}

.... I suppose an even more accelsteppery kind of way might be

void loop() {
     if (myStepper.distanceToGo() == 0) {
         delay(1000);
         myStepper.move(25);
     }
     myStepper.run();
}

I tend to think that the .run() method should be in the main loop when practical.  Don't know why, maybe it's stupid.

.... I suppose this:

void loop() {
     if (myStepper.run()) {
         delay(1000);
         myStepper.move(25);
     }
}

would work too - I don't feel good about doing essential work in an if evaluation, but it might save a couple of cycles maybe!



sn


--
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

onur olce

unread,
Nov 13, 2013, 5:40:52 AM11/13/13
to accels...@googlegroups.com
Sandy is an expert as you can see :)

Ronneil Camara

unread,
Nov 13, 2013, 12:01:41 PM11/13/13
to accels...@googlegroups.com
Does this look right?

void moveStepper(int steps, int delayMs) {
if (myStepper.distanceToGo() == 0) {

delay(delayMs);

myStepper.move(steps);

}
}
myStepper.run();

}

void loop() {
moveStepper(25, 1000);
}

Thanks Sandy!

Sandy Noble

unread,
Nov 13, 2013, 12:57:20 PM11/13/13
to accels...@googlegroups.com
Looks ok to me!

sn

Ronneil Camara

unread,
Nov 13, 2013, 1:11:13 PM11/13/13
to accels...@googlegroups.com
Great! I'll try it once I get home. :)

On Wednesday, November 13, 2013 11:57:20 AM UTC-6, Sandy Noble wrote:
Looks ok to me!

sn

Ronneil Camara

unread,
Nov 13, 2013, 8:18:06 PM11/13/13
to accels...@googlegroups.com


Hi Sandy,

Here is an update. The code is not moving the stepper motor.

This is the code I uploaded to my Arduino UNO.
#include <AccelStepper.h>

AccelStepper stepper(1,11,10);

void setup() {

        stepper.setMaxSpeed(100.0);
        stepper.setSpeed(60);

}

void loop() {
        moveStepper(200, 1000);
}

void moveStepper(int steps, int delayMs)
{
        if (stepper.distanceToGo() == 0) {
                delay(delayMs);
                stepper.move(steps);
        }
        stepper.run();
}


However, the code below is able to move my stepper motor properly.

#include <AccelStepper.h>

AccelStepper stepper(1,11,10);

void setup()
{  
   stepper.setMaxSpeed(500);
   stepper.setSpeed(60);
}

void loop()
{  
   stepper.runSpeed();
}


Ronneil Camara

unread,
Nov 13, 2013, 8:30:23 PM11/13/13
to accels...@googlegroups.com
Here is another update. I got it to work when I added stepper.setAcceleration(100);

However, is there a way to make it move at constant speed?

Ronneil Camara

unread,
Nov 13, 2013, 8:41:18 PM11/13/13
to accels...@googlegroups.com
It's me again. :)

I've been doing many experiments using different configuration. I noticed that when I use stepper.setAcceleration(), the stepper.setSpeed() doesn't get honored anymore. Am I right with my observation?

I also noticed that when I increase the value in stepper.setAcceleration(), the torque weakens. Am i correct?

Thanks,

Neil
Reply all
Reply to author
Forward
0 new messages