Adding limit switches

Am constructing a linear actuator to drive a carriage along a rail using a stepper motor and toothed belt.

I intend using an Arduino UNO R3 and an Easy Driver board.

I'm 95% there with the code, thanks to Brian Schmalz. See the last diagram on the page here.

Where I'm stuck is adding two limit switches to the code.

Any assistance would be greatly appreciated.


Each limit switch should be checked before and during moving, cancelling motion in the relevant direction. So if going "forward" you constantly check the forward limit switch and cancel motion when it activates - it also prevents you starting a forward motion (but allows reverse motion).

Vice versa for the other limit. Since you are driving a stepper you can implement soft limits as well, for once you've encountered one limit switch you know your absolute position and can then enable software limiting should you want to avoid hitting the hard stops each time.

Any chance someone could write some code for me, I'm a complete novice and struggling.


A state machine approach might help. Have a look at the attached state diagram.

Generally state machines are a very productive approach to these types of problems. There is a good paper on state machines here but the coding section is a bit over complicated. A much more simple approach is using my state machine library where you have to write 1 state function for each state so your code very closely mirrors the state diagram

limit switches state diagram.png