Using AccelStepper library to control stepper motors and limit switches

I am trying to control the position of multiple stepper motors, and then reset its position by rotating counter clockwise,until it hits the limit switch, and loop through these functions. I tested the autohome and runmotors functions separately and they work as intended (The runmotors function takes in parameters from the serial to determine its end position).

When I run the current code, it skips the runmotors function and performs the autohome function, but once it hits the limit switch it does not stop, even though it works fine when I remove the runmotors function. How would I get it to runmotors, wait until that function has finished, and then perform the autohome function?

void autohome(){
      if(digitalRead(limX) == HIGH){
        motorX.setSpeed(25);
        motorX.runSpeed();
      }
      else if(digitalRead(limX) == LOW){
        motorX.stop();
      }
      if(digitalRead(limY) == HIGH){
        motorY.setSpeed(25);
        motorY.runSpeed();
      }
      else if(digitalRead(limY) == LOW){
        motorY.stop();
      }      
      if(digitalRead(limZ) == HIGH){
        motorZ.setSpeed(25);
        motorZ.runSpeed();
      }
      else if(digitalRead(limZ) == LOW){
        motorZ.stop();
      }
}
void runmotors(){
        motorX.moveTo(theta1 / -1.8);
//      motorY.moveTo(theta2 / -1.8);
//      motorZ.moveTo(theta3 / -1.8);
//      
      motorX.run();
//      motorY.run();
//      motorZ.run();
}
void loop() {
      runmotors();
      autohome();

}

.run() and .runSpeed() will, at most, take one step. You have to repeat them. If you set a new target position with .move() or .moveTo() you call .run() until it returns false. If you are just moving at a constant speed, call .runSpeed() until you reach the destination.

To repeat, you could change your 'if' statements to 'while' loops.

You could look at the stepper motor example in my Multi-tasking in Arduino tutorial
https://www.forward.com.au/pfod/ArduinoProgramming/RealTimeArduino/index.html#Temp

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.