Homing two stepper motors simultaneously with Accelstepper

Dear forum visitors,

I am working on a project where I have two stepper motors (NEMA 17 1.33 kg.cm) and two limit switches connected to my cnc shield v3(with two drv8825 drivers on it). It is controlled by the arduino UNO.
I am finding it difficult to get the two steppers to home at the same time.
The code I have so far looks like this:

#include "AccelStepper.h" 
// Library created by Mike McCauley at http://www.airspayce.com/mikem/arduino/AccelStepper/

// AccelStepper Setup
AccelStepper stepperX(1, 2, 5);   // driver, step, direction
AccelStepper stepperY(1, 3, 6);   // driver, step, direction  

// Define the Pins used
#define home_switch_stepperX 9 // Pin 9 connected to Home Switch (MicroSwitch)
#define home_switch_stepperY 10 // Pin 9 connected to Home Switch (MicroSwitch)

// Stepper Travel Variables
long initial_homing_stepperX=-1;  // Used to Home StepperX at startup
long initial_homing_stepperY=-1;  // Used to Home StepperY at startup
const byte enablePin = 8;
long posFirst_stepperX = 2000;
long posSecond_stepperX = 5000;
long posThird_stepperX = 10000;


void setup() {
   Serial.begin(9600);  // Start the Serial monitor with speed of 9600 Bauds
   
   pinMode(enablePin, OUTPUT);  //enables stepper pin
   digitalWrite(enablePin, LOW);   
   
   pinMode(home_switch_stepperX, INPUT_PULLUP);
   pinMode(home_switch_stepperY, INPUT_PULLUP);
   
   delay(5);  // Wait for EasyDriver wake up

   //  Set Max Speed and Acceleration of each Steppers at startup for homing
  stepperX.setMaxSpeed(1000);      // Set Max Speed of Stepper X(Slower to get better accuracy)
  stepperX.setAcceleration(1000);  // Set Acceleration of Stepper X
  stepperY.setMaxSpeed(1000);      // Set Max Speed of Stepper Y (Slower to get better accuracy)
  stepperY.setAcceleration(1000);  // Set Acceleration of Stepper Y

// Start Homing procedure of Stepper Motor at startup

  Serial.print("Steppers are Homing . . . . . . . . . . . ");

  while (digitalRead(home_switch_stepperX)||(digitalRead(home_switch_stepperY))) {  // Make the Stepper move CCW until the switch is activated   
    stepperX.moveTo(initial_homing_stepperX);  // Set the position to move to
    stepperY.moveTo(initial_homing_stepperY);
    initial_homing_stepperX--;  // Decrease by 1 for next move if needed
    initial_homing_stepperY++;
    stepperX.run();  // Start moving the stepper
    stepperY.run(); 
    delay(5);
}

  stepperX.setCurrentPosition(0);  // Set the current position as zero for now
  stepperX.setMaxSpeed(5000.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepperX.setAcceleration(9000.0);  // Set Acceleration of Stepper
  initial_homing_stepperX=1;

  stepperY.setCurrentPosition(0);  // Set the current position as zero for now
  stepperY.setMaxSpeed(5000.0);      // Set Max Speed of Stepper (Slower to get better accuracy)
  stepperY.setAcceleration(9000.0);  // Set Acceleration of Stepper
  initial_homing_stepperY=1;

  while (!digitalRead(home_switch_stepperX) || (!digitalRead(home_switch_stepperY))) { // Make the Stepper move CW until the switch is deactivated
    stepperX.moveTo(initial_homing_stepperX); 
    stepperY.moveTo(initial_homing_stepperY);   
    stepperX.run();
    stepperY.run();
    initial_homing_stepperX++;
    initial_homing_stepperY--;
    delay(5);
  }
  
  stepperX.setCurrentPosition(0);
  Serial.println("Homing Stepper X Completed");
  Serial.println("");
  stepperX.setMaxSpeed(5000.0);      // Set Max Speed of Stepper X (Faster for regular movements)
  stepperX.setAcceleration(9000.0);  // Set Acceleration of Stepper X
  
  stepperY.setCurrentPosition(0);
  Serial.println("Homing Stepper Y Completed");
  Serial.println("");
  stepperY.setMaxSpeed(5000.0);      // Set Max Speed of Stepper Y (Faster for regular movements)
  stepperY.setAcceleration(9000.0);  // Set Acceleration of Stepper Y


}

void loop() {
  delay(1000);
  stepperX.runToNewPosition(posSecond_stepperX);
  delay(2000);
  stepperY.runToNewPosition(-5000);
  delay(2000);
  stepperY.runToNewPosition(0);
  delay(2000);
  
  stepperX.runToNewPosition(posFirst_stepperX);
  delay(2000);
  stepperX.runToNewPosition(posThird_stepperX);
  delay(2000);
  stepperX.runToNewPosition(0);
  delay(5000);

This results in the steppers homing, but they do not stop homing until BOTH limit switches have been activated.
I am sure it has something to do with one of these lines but I cannot figure out how to fix it.

while (digitalRead(home_switch_stepperX)||(digitalRead(home_switch_stepperY))) {
while (!digitalRead(home_switch_stepperX) && (!digitalRead(home_switch_stepperY))) {

I hope I have made my problem clear enough.

Sorry, but I don't understand the problem.

You say "but they do not stop homing until BOTH limit switches have been activated". What is it that you want to happen?

If this was my project I think I would approach the homing differently by getting the motors to move one step at a time at intervals decided by me and test the switches between each step. Then when a limit switch is triggered I could stop the steps for that motor. However I would expect the program to remain in the homing state until both motors had reached their home positions.

...R
Stepper Motor Basics
Simple Stepper Code

  while (digitalRead(home_switch_stepperX)||(digitalRead(home_switch_stepperY))) {  // Make the Stepper move CCW until the switch is activated   
    stepperX.moveTo(initial_homing_stepperX);  // Set the position to move to
    stepperY.moveTo(initial_homing_stepperY);
    initial_homing_stepperX--;  // Decrease by 1 for next move if needed
    initial_homing_stepperY++;
    stepperX.run();  // Start moving the stepper
    stepperY.run();
    delay(5);
}

That delay() is just completely wrong.

Most CNC machines home in one direction at a time. Why are you trying to home both at the same time?

MrGls:
Dear forum visitors,

I am working on a project where I have two stepper motors (NEMA 17 1.33 kg.cm) and two limit switches connected to my cnc shield v3(with two drv8825 drivers on it). It is controlled by the arduino UNO.
I am finding it difficult to get the two steppers to home at the same time.

2 questions:

Could you define what you mean by " at the same time"?

Are you looking for a single core solution or a multi core solution for an "at the same time"?

Hello everyone,

Excuse me for the late reply. I have been very busy lately.

Robin2:
You say "but they do not stop homing until BOTH limit switches have been activated". What is it that you want to happen?

I would like that stepper X would stop homing when the end stop is activated. Same goes for stepper Y.
Now they only stop when both endstops are activated. That means that if stepper X is home and stepper Y not, the homing still continues of stepper X. Resulting in stepper X to force itself into a wall.

In the attachment you find a picture of my setup.

The horizontal axis is the Stepper X. The vertical is Stepper Y.

Another thing I seem unable to figure out is that my homing does not respond to my acceleration code. In other words, it does not accelerate while homing. That means I am unable to control the speed and acceleration of the steppers when they are homing.

Excuse me for asking potentially simple questions, but I am still quite new to programming and I don't know the correct commands. Basically I look for other codes that might seem to work, then I evaluate, adapt and test them until I run into a problem that I do not understand.

Try changing this

  while (digitalRead(home_switch_stepperX)||(digitalRead(home_switch_stepperY))) {  // Make the Stepper move CCW until the switch is activated   
    stepperX.moveTo(initial_homing_stepperX);  // Set the position to move to
    stepperY.moveTo(initial_homing_stepperY);
    initial_homing_stepperX--;  // Decrease by 1 for next move if needed
    initial_homing_stepperY++;
    stepperX.run();  // Start moving the stepper
    stepperY.run(); 
    delay(5);
}

to something like this pseudo code

bool homingComplete = false;
bool xHome = false;
bool yHome = falsel
while(homingComplete == false) {
   if (digitalRead(home_switch_stepperX) {
       stepperX.moveTo(initial_homing_stepperX);  // Set the position to move to
       initial_homing_stepperX--;
       stepperX.run(); 
   }
   else {
       xHome = true;
   }
   // likewise for stepper Y
   
   if (xHome == true and yHome == true) {
      homingComplete = true;
   }
}

...R

@Robin2

Thank you for your suggestion. It is a bit of work to get my head around it but I am managing.
The first part of homing works now. I just have to adapt the rest of the code to have it entirely working. After the homing I can just execute all the commands necessary for my goal.
Thanks again and I will keep you posted.

PaulS:
That delay() is just completely wrong.

You are right. Removing the delay there made my stepper accelerate during homing.

PaulS:
Most CNC machines home in one direction at a time. Why are you trying to home both at the same time?

Because my machine is not a CNC machine. It is more of a pick and place robot.