non-blocking motor move for arduino

2549 views
Skip to first unread message

Ronneil Camara

unread,
Apr 6, 2015, 4:50:54 PM4/6/15
to accels...@googlegroups.com
Hi folks,

I have a very simple menu where I can press up and down button to select a menu, just like a dropdown menu. It's inside the loop() function.

I added the code below inside loop(). Now, I could hardly access the menu.

    stepper.runToNewPosition(0);
    stepper.runToNewPosition(1500);
    stepper.runToNewPosition(1300);
    stepper.runToNewPosition(1700);

How can we use AccelStepper functions so that it doesn't affect the menu kinda like in asynchronous way?

Thanks!

Neil

gregor christandl

unread,
Apr 6, 2015, 4:53:49 PM4/6/15
to accels...@googlegroups.com

runToNewPosition() blocks until it is finished. Instead, set a new target once with move() or moveTo(), then call run() in the loop (run() does not block).

Cheers,

--
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/d/optout.

Sandy Noble

unread,
Apr 6, 2015, 4:56:13 PM4/6/15
to accels...@googlegroups.com
Hello Neil, did you read the docs
(http://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html)
that describe how to use Accelstepper?

Have a look at the accelstepper examples that show how to set targets
with move(..) and then run().

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/d/optout.



--
Sandy Noble

Ronneil Camara

unread,
Apr 6, 2015, 5:06:16 PM4/6/15
to accels...@googlegroups.com
Thanks folks. I will do both, the code and the docs.

Thanks again!

Ronneil Camara

unread,
Apr 6, 2015, 5:16:16 PM4/6/15
to accels...@googlegroups.com
Ok, I tried this and it worked but everytime I would press my up or down button(to change menus), the motor would stop. Once I release the button, the motor will continue moving again.

// If at the end of travel go to the other end
if (stepper.distanceToGo() == 0)
stepper.moveTo(-stepper.currentPosition());
stepper.run();

Ronneil Camara

unread,
Apr 6, 2015, 5:24:02 PM4/6/15
to accels...@googlegroups.com

I was wrong. I made a very simple code(see below). A0 is one of the pins where my button is connected. When I press the button, it doesn't stop the motor. It continuously run smoothly. Now I need to hunt which code is causing the motor to stop. Maybe it stops when I display a text when I change menus

    reading = digitalRead(A0);

Sandy Noble

unread,
Apr 6, 2015, 5:31:13 PM4/6/15
to accels...@googlegroups.com
Writing to LCDs is usually pretty slow, and it also blocks, so will
prevent .run() from being called.

Accelstepper relies on having .run() called as often as possible, and
at least as regularly as a motor step is required. Ie for very slow
movements, you might not notice the pause.

Ronneil Camara

unread,
Apr 6, 2015, 5:35:11 PM4/6/15
to accels...@googlegroups.com
I found the culprit.  The delay(200) is what makes the motor stops. I added delay(200) on my real menu so that I can slow down the incrementing/decrementing of a value that is being displayed on the screen while I press up/down buttons. I'm having a hard time setting a specific value since value increments/decrements extremely fast.


    reading = digitalRead(A0);
    if (reading == HIGH) {
        delay(200);

Ronneil Camara

unread,
Apr 6, 2015, 5:36:37 PM4/6/15
to accels...@googlegroups.com
I'm using vacuum field display and guess what, when I took out the delay(200), the issue went away. I just need now to figure out how to slow down the incrementing/decrementing of value as I mentioned on my previous post.

gregor christandl

unread,
Apr 6, 2015, 6:18:29 PM4/6/15
to accels...@googlegroups.com

Only increment/decrement a value when a specific time has passed since the last increment/decrement.

If (reading == HIGH && millis() - prev_update_time >= 200)
{
    prev_update_time = millis();

    //change the value
}

Reply all
Reply to author
Forward
0 new messages