Here's the thing: when you're using AccelStepper, you need to make sure you're telling the motor what to do step by step. Right now, it seems like you might be missing some logic after your motor hits the switch.
In your Home()
function, you've got a check for the limit switch status. When the limit switch is hit (LOW), you tell the motor to stop, set a new position, but then you immediately tell it to move to position 500 and run there. If the motor is already past the switch, it might not act as you expect.
You might want to add more control after the limit switch is triggered. Here's a simpler idea of what you might do:
- Move towards the switch.
- When the switch is hit, stop and back up a little.
- Then go to your desired position from the backed up spot.
Your code needs to handle all these steps. Right now, after the switch is triggered, you set the current position to 0, but then you move to position 500 immediately without backing up first.
Try something like this instead:
After the limit switch is triggered (when Limit_Switch_Stat == LOW
), do these:
- Stop the motor.
- Set the current position to 0 to recalibrate the position.
- Move the motor a small distance away from the switch to give it some space (for example,
stepper.move(-50);
).
- Then tell it to go to the new desired position (
stepper.moveTo(some_position);
).
And remember, every time you want the motor to move, you have to call stepper.run()
or stepper.runSpeed()
inside your loop to actually process the motor steps. The AccelStepper library needs this repeated call to step the motor.
In your existing code, after detecting the switch is LOW, you can add a step to move backwards, like this:
if (Limit_Switch_Stat == LOW) {
stepper.stop(); // Stop the motor
delay(50); // Short delay to ensure the stop completes
stepper.setCurrentPosition(0); // Reset position to 0 to consider this the home position
stepper.move(-50); // Move a small amount back from the switch
while (stepper.currentPosition() != -50) {
stepper.run(); // Run until the motor moves back by 50 steps
}
stepper.setMaxSpeed(400); // Set a new max speed
stepper.setAcceleration(400); // Set a new acceleration
stepper.moveTo(500); // Set the target position to 500
while (stepper.currentPosition() != 500) {
stepper.run(); // Run until the motor reaches position 500
}
stepper.setCurrentPosition(0); // Reset the position again if needed
stepper.stop(); // Stop the motor
}
Also, check your loop function, which is currently empty. You need to continuously check if the motor needs to step to reach its target position:
void loop() {
// This is where you would usually call stepper.run() or
// stepper.runSpeed() to actually move the stepper
}
Depending on your setup, you might want to handle the home position logic within the loop()
instead of the setup()
function, as setup()
only runs once when the Arduino starts up.
Just to mention, be cautious when using exit;
in your Home()
function. The exit
command is not really proper in the Arduino environment and doesn't do what it does in other programming environments. If you want to exit a loop or stop a function early, you should use return;
instead of exit;
. So, in your Home()
function, when the limit switch is HIGH, and you want to stop the homing process, you should use return;
to exit the function.
Let's clean up the Home()
function and integrate it into the main loop. Here’s an example of how you can modify your code:
void loop() {
// Only run the home function if we're not already at the home position.
if (digitalRead(Limit_Switch) == HIGH) {
Home();
} else {
// Once homed, you can add additional code here to perform other actions.
}
// Always call run to keep the stepper moving as needed.
stepper.run();
}
void Home() {
// Move towards the limit switch slowly
stepper.moveTo(initial_homing);
stepper.setSpeed(600);
// While we haven't hit the limit switch, keep moving
while (digitalRead(Limit_Switch)) {
stepper.run();
}
// We hit the limit switch, so first stop and back up a bit
stepper.stop();
delay(50); // Short delay for the stop to take effect
stepper.setCurrentPosition(0); // Set this position as 0
stepper.move(-100); // Move back 100 steps from switch
// Move back from the switch
while(stepper.currentPosition() > -100) {
stepper.run();
}
// Now set the desired position to go to after homing
stepper.setCurrentPosition(0); // Reset position again to 0 if needed
stepper.setMaxSpeed(400); // Set the target speed for moving towards desired position
stepper.setAcceleration(400); // Set the acceleration
// Set the target position after homing
stepper.moveTo(500); // We want to move to position 500 after homing
// Go to the target position from the backed-up position
while (!stepper.runToPosition()) { // This will run until the motor reaches the position 500
// This block will keep on looping until we reach the target position.
}
// Optional: Set the current position to 0 again if this is your new home/reference position
stepper.setCurrentPosition(0);
// Now you are ready to perform other operations, now that the motor is homed.
}
In this updated code, after touching the limit switch, the motor will reverse for 100 steps (backing away from the switch), and then move to the desired position 500 steps away from the home position. The stepper.runToPosition()
in the loop will execute until it reaches position 500.
Remember, the loop()
function will continue to execute repeatedly. If you want it to stop moving after homing or after performing a set of operations, you need to add logic to prevent the Home()
function from being called again or to exit the loop after reaching the desired state.
Finally, you should remove any unnecessary calls to stepper.stop()
once you have the movement logic correct, as runToPosition()
will handle stopping the motor when the target position is reached.