Stepper motor - self learning software :-)

Hi to all.

I got one tricky situation where i have to control back/forward stepper movement, based on some “marker”. It’s just a test that should prove that this is possible to make and that it can be adopted in our future project.

Simply, action that stepper should take is based on repeat speed of this marker.
Main goal is to avoid this marker but the problem starts when marker begins to speed up.
Best example is car. When you start it it moves slowly and in some time it starts to speed up.
This is the same situation but it can be separated into two options.

If marker == true && repeat time < 2 per second

  • move forward
  • delay some time
  • move backward to starting position.

If marker == true && repeat time > 2 per second

  • move forward
  • move backward to starting position.
  • avoid marker


  • moving stepper forward / backward ( even without delay ) would take some time and i will not be able to avoid marker after the whole process speeds up… So the only way to continue with this test is to use option 2. It’s not important when this second option accours. Important is that, in this time, it should avoid the marker.

As test am using magnetic switch which is hooked up to moving track and i can simulate option 1 but i dont know how to “create” option two because in some time track speed we increase so much that i can not avoid the marker…OK. when this happens, we have completed the test…

Suggestions ?

Regards !

This is the example code that am using:

#include <Stepper.h>

const int stepsPerRevolution = 600;  
const int trigger = 2;         // trigger
int       bState = 0;          // pushbutton status
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  pinMode(trigger, INPUT_PULLUP);


void loop() {
   bState = digitalRead(trigger);

  if (bState == LOW) { 



Before offering some silly advice (or at least irrelevant) perhaps you can explain more clearly what you want to achieve.

You say this is a test of a technique - it would probably help if you tell us what the eventual project will be. Then maybe the test will make more sense.

My first thought is that it is impossible to avoid something that is moving towards the motor faster than the motor can move away from it.

But then I say to myself, a motor cannot move out of the way of anything - it just sits on the table and spin its output shaft.


:slight_smile: :slight_smile: . Conclusion is correct but that also means i didnt explain correctly :slight_smile:
Now that i read it again, i noticed i missed the main part.

If the second option is active, system must try to avoid marker. The key word is try.
I think that the main thing in some time interval, because if the system can recognize some pattern and detect that next marker will come in some defined time, then software can calculate option2 execution time.

What I mean is that the value of stepper speed, should be adjusted automatically based on marker speed . The reason for this is to produce system that can be highly efficient ( in this conditions ).
Track speed will be different all the time, so system should adopt to this situation.

If the repeat time of the marker is so fast that sw can not execute option 2, system should just wait for next opportunity.

From my point of view, the only way to execute option 2 is to try to read out some marker repeat time interval and based on that, calculate if the option2 can be executed.

I have tested the fastest spin for this test stepper and that is 4 full turns in second.


Now that i read it again, i noticed i missed the main part.

You certainly missed the main part of my Reply #1 - a request for a description of the eventual project that you plan to create.

And the secondary point (which, I admit, may have been a bit obscure) what does the test project look like - how does it work?


You certainly missed the main part of my Reply #1 - a request for a description of the eventual project that you plan to create.
*** I am not alone in this project so i can not explain each detail. Sorry about that.
Look at this as onto some modular system that can be used in industry and robotics.

In fact, it looks super simple but when i try it real life then i get bunch of problems.
Like i wrote, for now am using one track which is moving at different speed.
On this track is connected magnet that i use as marker / switch.

btw. at the time, this is only test that i can use in real time situation.

Maybe the best explanation is that i have to keep this stepper motor "alive" as much as possible :-).
Tell me, if you know fastest turn speed of stepper motor, would it be then possible to execute option two ?.

Because, all comes down to marker speed.
So if the marker speed is to fast i dont have to trigger option 2, i can just wait, but if program detects that marker speed has been decreased and that there is an way to execute forward/backward motion in this time interval, then option two should be running.

I was thinking to set some value that would represent fastest execution time of option two.
Let's say that this is 1,5 sec.

So if time between markers is bigger then 1,5 sec --> run option 2.


You certainly missed the main part of my Reply #1 - a request for a description of the eventual project that you plan to create.
*** I am not alone in this project so i can not explain each detail. Sorry about that.

That leads me to think this is a commercial project for which you are seeking free advice - which IMHO would be very unfair.

If it is a commercial project then get professional advice.

If it is an OpenSource project then tell us all about it.


i hope that it will be commercial project in some time (year or so ) and that's why i can not explain in detail. For now it's just and idea that i want to test. Pure "garage" project.

Anyways, if you want/can help, let me know.

Regards !

I am by no means a programmer but I am interested in your problem. I think you are missing a few elements that need to be considered before defining programming variables. I agree with others here that the description of what you are attempting to accomplish is a little vague. But I think I got the idea using your car analogy. But there are gaps.

Anytime a mobile object requires friction to accelerate and decelerate, these physical actions require datasets relative to the actions desired. That is, acceleration rate, constant speed and deceleration rates. Once these are known, the programming elements could be developed with some parameters. Stepper motors confined and mechanically attached, operate using different constraints.

A good example is today's current sonar / radar technology being used on today's self driving cars and trucks now being tested on open roads. Parameters such as braking velocities measured in feet per second based on weight, physical forces, grip, etc, are compiled into a large dataset that are queried millions of times per second.

If I understand your requirement correctly, you want to maintain distance separation, regardless of how slow or fast the target marker is moving between itself and your device. Is this a correct assumption? If true, you will have to correct for latency and friction variables, that take into account acceleration, deceleration and steady state modes of operation.

OP, have you read about PID algorithms?

One of their very purposes is to aggregate inputs vs outputs over time - to reduce or eliminate overshoot.

From reading all your posts, you may be making this sound a lot harder than it really is. PID processing is used in everything from electronic toasters to climate control - and autonomous vehicles.