AccelStepper instant stop on limit switch

Hi there,

This is my first post - I hope someone out there can help!

I am using the accelstepper library to move a motor from position 0 to 180 degrees (position 200 in my code) and back again. Under normal use the program will just loop back and forth between these 2 numbers.

I have 2 limit switches on my project which are absolute limits for the moving part i.e. i need an immediate stop, not a quick stop as is in the library examples.
Ideally I want to keep the absolute positions unchanged as well. So far I have got this to work....


This dead stops the motor fine. I do however want to then run it back to the other side again so I write afterwards;


This also works fine as long as limit switch is not pressed during the acceleration/deceleration phase. i.e. If I press the limit with the motor at position 100 (in the middle) the motor immediately stops and then starts going back the other way. If I press the limit switch at position 10, the motor slows down under the acceleration figure that was set.

Anyone any ideas why? or is there another way of doing this?


Sounds like a software design issue, so you'll need to post your code if you want any constructive advice.

Sorry the code was too long to post - I have attached it for anyone that can help. Im not sure how much of it is needed to decipher my problem.


Stepper_motor_test.ino (9.34 KB)

That code looks quite convoluted, with global flags all over the place. Most of the flags seem to be used just for display updates and can be ignored, but that still leaves a surprising amount of complexity.

I understand you have two limit switches and the motor is intended to stop each time it hits a limit.

When you say that the motor behaves differently when you operate the switch manually when it is moving at a steady speed versus when it is accelerating/decelerating, do you mean that you operate the switch that the motor is heading towards? That's the only one that seems to make sense, but actually both limit switches are active all the time so either of them might have an effect.

Does it make any difference which way the motor is traveling? I notice that there is an extra call to in the 'right' switch handling, which might make this scenario behave differently.

It looks to me as if the limit switches will try to stop the motor but the code around lines 306 .. 320 will cause the motor to start movement in the reverse direction when the stepper reaches its target position. I can't figure out how this is supposed to relate to the limit switches - do you intend the motor to stop when it reaches the target position, or when it hits the limit switches, or both? I'm struggling to see how this could work at all, let alone well enough that your only problem is a bit of deceleration.

Finally, not related to this problem but your code to make data flash up on the display doesn't handle timing at all well. Look at the 'blink without delay' to see the recommended way to do that.