It might be a hardware problem. I tried using interrupts, but that didn’t work either. Now, I have found a solution that is not elegant but works most of the time. However, even with this solution, the motor sometimes stops before the sensor can even be triggered. I suspect that the sensor might be sending a faulty signal, or there might be a slight error in the sensor signal due to the wiring. Therefore, I tried adding a time delay in the condition of the while loop to bypass a potential faulty signal from the sensor.
I am also attaching my complete code in case someone can spot the error directly or find a solution for themselves.
#include <AccelStepper.h>
// Define stepper motor connections and motor interface type
#define motorInterfaceType 1
// Define the stepper motor control pins
#define X_STEP_PIN 60
#define X_DIR_PIN 61
#define X_ENABLE_PIN 56
#define Y_STEP_PIN 46
#define Y_DIR_PIN 48
#define Y_ENABLE_PIN 62
// Initialize the stepper library
AccelStepper stepperX(motorInterfaceType, X_STEP_PIN, X_DIR_PIN);
AccelStepper stepperY(motorInterfaceType, Y_STEP_PIN, Y_DIR_PIN);
// Define brake pins for X and Y motors
#define X_BRAKE_PIN 45
#define Y_BRAKE_PIN 10
// Stepper Motor
const int steps_per_rev = 200;
const int pitch = 4;
// Define optical sensor (endstop) pins
#define Y_HOME_PIN 19
#define X_HOME_PIN 2
// Cooling Fan
const int pin_fan1 = 7;
void setup() {
// Set the maximum speed and acceleration:
stepperX.setMaxSpeed(1000);
stepperX.setAcceleration(500);
stepperY.setMaxSpeed(1000);
stepperY.setAcceleration(500);
// Disable the motor
pinMode(X_ENABLE_PIN, OUTPUT);
digitalWrite(X_ENABLE_PIN, LOW);
pinMode(Y_ENABLE_PIN, OUTPUT);
digitalWrite(Y_ENABLE_PIN, LOW);
// Configure endstop and brake pins
pinMode(X_HOME_PIN, INPUT_PULLUP);
pinMode(Y_HOME_PIN, INPUT_PULLUP);
pinMode(X_BRAKE_PIN, OUTPUT);
pinMode(Y_BRAKE_PIN, OUTPUT);
// Engage brakes initially
digitalWrite(X_BRAKE_PIN, LOW);
digitalWrite(Y_BRAKE_PIN, LOW);
// Cooling Fan
pinMode(pin_fan1, OUTPUT);
analogWrite(pin_fan1, 180);
// Start serial communication
Serial.begin(9600);
}
void loop() {
// Check if there is any serial input
if (Serial.available() > 0) {
// Read the incoming string
String command = Serial.readStringUntil('\n');
// Check if the command is "home"
if (command.equalsIgnoreCase("home")) {
Serial.println("Homing begin");
home();
}
// Process X motor commands
else if (command.startsWith("x")) {
double distance = command.substring(1).toDouble();
double steps = distanceToSteps(distance);
Serial.println("Move X " + String(distance) + " mm or " + String(steps) + " steps");
moveMotor(stepperX, X_BRAKE_PIN, steps);
}
// Process Y motor commands
else if (command.startsWith("y")) {
double distance = command.substring(1).toDouble();
double steps = distanceToSteps(distance);
Serial.println("Move Y " + String(distance) + " mm or " + String(steps) + " steps");
moveMotor(stepperY, Y_BRAKE_PIN, steps);
} else {
Serial.println("Invalid command");
}
}
}
// Convert distance to steps
double distanceToSteps(double distance) {
return distance * steps_per_rev * 32 / pitch;
}
// Move motor with braking control
void moveMotor(AccelStepper &stepper, int brakePin, double steps) {
stepper.move(steps);
digitalWrite(brakePin, HIGH);
delay(100);
stepper.runToPosition();
delay(100);
digitalWrite(brakePin, LOW);
}
// Homing Process
void home() {
// Homing X axis
homeAxis(stepperX, X_BRAKE_PIN, X_HOME_PIN, 1000, -500);
// Homing Y axis
homeAxis(stepperY, Y_BRAKE_PIN, Y_HOME_PIN, -1000, 500);
// Give feedback when homing complete
Serial.println("Homing complete");
}
// Homing individual axis
void homeAxis(AccelStepper &stepper, int brakePin, int homePin, int speed, int backoffSteps) {
stepper.setCurrentPosition(0);
digitalWrite(brakePin, HIGH); // disable Brakes
delay(100);
stepper.setSpeed(speed); // Set speed and direction
unsigned long startTime = millis();
while (millis() - startTime < 100 || !digitalRead(homePin)) { // Wait until sensor triggers
stepper.runSpeed();
delay(1); // Will not run without delay!
// Check if interrupt was triggered
if (digitalRead(homePin) == HIGH) {
Serial.println("Sensor activated");
break;
}
}
stepper.stop();
stepper.setCurrentPosition(0);
stepper.runToNewPosition(backoffSteps); // Move steps back
stepper.setCurrentPosition(0); // Set Position to zero
digitalWrite(brakePin, LOW); // Engage Brakes
}