Using AccelStepper for 1 rotation and back, loops driving me loopy!

I have a marble machine door bell dinger which is designed to react to a button push (door bell outside).
I'm having real trouble working out how to get the loops working correctly with AccelStepper.

Im not completely new to arduino and have done a number of projects before, but this was a few years back! I'm resisting to ask for code i want to learn why I cant work this out! instead of just copy and pasting bits from all over the internet like most of my other projects!

I am running AccelStepper which my trial and error tells me needs to loop stepper.run which is what is throwing me.

I'd like a button press to.... (in sequence)

  1. turn on a light
  2. shuffle my servo left then right 5 times
    wait 5 seconds
  3. run stepper forwards x rotations, then back x rotations to the start,
    wait 5 sec
  4. turn off light
    then ready to read a button press again.
    While the loop is running I don't want the button press to in

For some reason I can't work out the loop structure.

If it matters I'm using, stepper.moveTo with different accel and speed for forward and reverse.

Any guidance on the structure i could use would be much appreciated

Many thanks
Neal

ive just come across the term 'blocking function'.
i think my lazy 'blocking' servo control code - move left...delay...move right...delay... mixed with stepper.run which is 'none blocking' is causing me issues?
maybe runToNewPosition() would work better.
(it would be nice to build it in a non-blocking way, but actually as i'm not asking for complex functionality, this may work ok? I'll try in the morning)

Another term that is very relevant for your project is "State Machine" (look it up). Basically is a fancy name for a simple concept of using one or more variables to keep track of progress through a project - for example

  • WAITING FOR PUSH
  • LIGHT ON
  • MOVING SERVO
  • MOVING STEPPER
  • LIGHT OFF
  • WAITING FOR PUSH

and then within (say ) the MOVING STEPPER state there could be another variable that keeps track of

  • AT REST
  • MOVING FORWARD
  • PAUSING
  • MOVING BACK
  • AT REST

The general idea is that, for example, in the WAITING state the code is checking for a button push and when it happens it changes the state from WAITING to LIGHT ON and the LIGHT ON action changes the state to MOVING SERVO etc. That way there is a nice orderly progression through the tasks.

It is also a good idea to have each action in a separate single purpose function as that allows each part to be tested separately. Have a look at Planning and Implementing a Program

...R

Unless that code is supposed to check for button presses or other inputs while doing the movements, there is no need for a state machine. Blocking functions will do just fine in this case, and are of course much simpler to implement.