Limit switches not changing the direction of stepper motors

You can try reading a state change of the limit switches by comparing the current reading to a previous one. When the switch goes from closed to open, make the reversal.