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.