Accelstepper on conditional call

The start function not working when called from an if sentence, but works when called directly. Why, and is there a workaround?

void loop(){  
if(x==0){//remove these 2 lines and it works
    start();
   }//remove these 2 lines and it works
}

}
void start(){  
   myStepper.setMaxSpeed(200);
   myStepper.setAcceleration(50);
   myStepper.moveTo(140);
   myStepper.run();
}

Maybe 'x' is not zero?

Typically, myStepper.setMaxSpeed(200); and
myStepper.setAcceleration(50); would be in setup() and the 'myStepper.run()' would be in loop().

That would indicate that the condition is false.

Just a quick guess.

Should be unconditional…
I think it
ts the stepper ,maintenance’ function.

The meaning of this is you have to call

myStepper.run();

at a higher frequency than the next step must be created.
This is the price for non-blocking step-pulse creation
loop must run very fast.

The alternative is to use a blocking function for the step-pulse-creation
But as the name "blocking" says no other code can be executed as long as the step-pulse-creation is active = steppermotor is rotating.

The only way to have both non-blocking step-pulse-creation and "slow" running loop()
is to use a timerinterrupt for step-pulse-creation.

The MobaTools-Library does exactly this. Non-blocking step-pulse-creation "in the backround" through a timer-interrupt.

"set and forget" step-pulse-creation.

The function-calls in the MobaTools-library have different names than in the accellstepper-library. Once you have learned them it is easy to use.

So you have to decide between investing time for
re-writing your code beeing non-blocking again and having a fast running loop
or
investing time to learn the function-calls of the MobaTools-Library and never again think about
"will my loop() still be fast enough to call often enough myStepper.run(); ?????

best regards Stefan

I'm pretty sure it can be solved in an easy way.
To make a suggestion you would have to post a description of your project and the wanted functionality.

I'm serious: The description should have only normal words. No coding-terms

best regards Stefan

After reading Stefan L38´s comments and studying AccelStepper on other forums I found an easy solution: In this case using a blocking command is no problem.
Info on AccelSteppers commands is scarce, but in this case:

https://www.airspayce.com/mikem/arduino/AccelStepper/Blocking_8pde-example.html

does the trick - placing only this command in the function:

myStepper.runToNewPosition(175);

and the other settings in the void loop.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.