Stepper motors changing direction during homing function?

So I'm attempting to home two stepper motors at the same time because they both control a single axis of movement on a large test rig I have. To home them, I'm running a loop until limit switches are detected high on both sides, then backing them away from the switches after so they're not triggered. It seems to run just fine for around 5 seconds, then it switches direction, and repeats this every 5 seconds. Wondering if there is something I am doing wrong, or a way I can code this with interrupts instead?

void initHoming(){ //homes the machine for the first time
  while (!homed){
    if (digitalRead(leftLim) && !leftHome){
    if (!digitalRead(leftLim) && !leftHome){
    if (digitalRead(rightLim) && !rightHome){
    if (!digitalRead(rightLim) && !rightHome){
    if (leftHome && rightHome){
      while ((abs(left.distanceToGo()) > 0) && (abs(right.distanceToGo()) > 0)) {;;

Just don't. Even commercial equipment do not do this.

Two stepper motors are each controlling one lead screw, and they need to be in sync to home. You mention not doing this, but I was also hoping you could propose an alternative?

Before going any farther with this discussion, you need to supply a drawing of what you have and a very good explanation for two stepper motors connected together.

I've attached a sketch of the rig. It's about 6'x6', so both sides are using lead screws to move a central bar up and down. stepper motors are mounted at the bottom of each side rail coupled to the lead screws, they're not show in the image.

Moving the steppers in sync is obvious. Will the horisontal bar carry loads of some kind?
What about an angle checking device mounted on the horisontal bar that can serve as a guard and pull an alarm if not levelled? If one steppers stalls and the other stepper runs thing might quickly get nasty.

The horizontal bar will have sensors across it, and yes, I'm implementing tilt sensors that will sit in the middle of the bar to interrupt it if they misalign.

Good. Will that horisontal guard tell whether the tilt is left or right?
Somehow You new a way to recover.....
Then You only need to select proper dimensions for screws, stepper torque, .......

I'll use two different ball tilt sensors on separate interrupt pins so yes. Then I can just tell the code to move back the amount of steps necessary to trigger the tilt sensor in the first place.

If you want two or more steppers to be in absolute sync, then you need to use a single controller and connect them all to that controller. For two steppers and one to turn the opposite, wire that one opposite to the first.

I raise a finger of warning. Do You have experience of interrupt handlers and real time programming?
Take this scenario: Your lib is running the motors, and an interrupt occurs (and does what?). When the interrupt routine is finished the lib keeps on doing whatever it was doing because the lib knows nothing about any abnormalities.

I don't have a ton of experience, but understand how to use digital pins to interrupts. I plan to let the tilt sensors go off, then re-adjust, then rehome the unit to start over the procedure correctly.

You don't convince me... Interrupt is not a GOTO, making the execution start all over somewhere. Drop the interrupts. Check the tilt sensor after every stepping pulse.

We can tell. Your interrupt code will still have to set a boolean or other type flag which you will still have to check before each and every step your code will be taken.

Hi Paul, but, if I have only one controller and one of the two motors is blocked by something, the other will still move, so we have to move them independently to realign them, so we need two controllers. If the pulse signal is the same for both controllers, why won't they move in sync?

The only doubt I have is if the controller, at the very first step, knows which coil to activate to go in the right direction, otherwise the motor may move one step in the opposite direction.

Have you solved the problem? I'm doing the same but moving horizontally. At the moment I have only a single home switch hoping that both motors will go always in sync. I'll let you know.

Did you set the current limit to TWICE that required for a single stepper?

Hi Paul, maybe I was not clear. I want to use two motor drivers with the same arduino pin and, only in case, with two different pins for alignment, why you say that is not possible?

Strange design to use hope. The usual strategy is to use a SINGLE stepper motor and use a toothed belt and toothed pulleys to connect the two lead screws. Then you know for sure they both run in sync.

There are two sprockets 6 meters far so I connected a motor to each sprocket. There is the same configuration on a similar machine but with servo motors ... more expensive.