How to call .step() using randomized steps and mills() instead of delay()


I am a senior university student and have been working on a capstone project using a stepper motor to turn a block quarter and half turns. I am using a NEMA 17 bipolar stepper motor (200 steps per revolution) with the arduino rev3 motor shield. The stepper turns a 4 sided rectangular block. A constant power supply runs 12V to the motor through the shield and 5V to power the arduino uno (when not plugged into a usb port). I have learned quite a bit over the semester and have had success in using the delay function to get the stepper to move from face to face consistently and accurately. I can get this to work successfully, but using the delay function is cumbersome and will not translate well when performing hundreds of randomized turns.

300-600 turns need to occur with roughly 8 seconds in between turns: 6 seconds static, 2 sec window for movement. The movement is far quicker than that (~250 ms for a full rotation) but still must occur within that window.

The final prototype will have 2 arduinos, 2 motor shields, and 2 motors synced to physically move to a set position in an xy plane (hold 6 seconds), physically move out to another set position in that xy plane, rotate to a given face within a 2 second window, and physically move back into that original position in that xy plane.

I want to simplify the code as much as possible so there aren't 600-1200 lines in the void loop and utilize the mills() function to read the elapsed time instead of using the delay function as a timer between turns.

I want to also simultaneously provide feedback for the elapsed time and corresponding block face in the serial monitor.
I have been able to do this, but once again, the code is manually entered at the moment for EVERY GIVEN ROTATION.

The set [-50,0,50,100] encompasses the shortest paths for the block to turn from any face to any other.

I want to call the .step() function and use the numbers within this set to create movement, without having to manually code EVERY SINGLE TURN. I also want to be able to randomize the order in which the numbers within the set are called within the .step() function to allow for unique trials. I have been unsuccessful in learning how to do so and am having issues with arrays and utilizing the mills() function as a timer. I tried the blinkwithoutdelay example and sort of understand it, but not completely.

I want to make the code for this project as simple (short) as possible within reason and writing out nearly a thousand lines of code any time the order of rotations change is just not feasible.

Any help would be appreciated, especially towards resources to aid in my understanding.

I can share any schematics, code, and diagrams upon request.

However, I will not be able to share TOO many specifics due to the nature of work being done with this project.

If you don't post your program it is just a teeny bit difficult to know what is wrong with it. :slight_smile:

Have a look at the second example in this Simple Stepper Code - it uses millis() and micro() for timing.

Also the demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.