Multi-Stepper Zeroing Using a limit switch (Help)

Hello,

I am ironing out an attempted improvement to my existing code. I have two stepper motors that ultimately move two bars in or out that are attached to a linkage between the ends.
Rough sketch:

Currently, I have this:

while (!digitalRead(LIMIT_PIN_1)) {stepper1.setSpeed(1000); stepper1.run();}  // Negative speed for reverse direction

  while (!digitalRead(LIMIT_PIN_2)) {stepper2.setSpeed(1000); stepper2.run();}  // Negative speed for reverse direction

And it works, but if the bars are too far out, it will cause a mechanical failure due to how it is mounted, the bar needs to be "horizontal" during operation. In the bigger picture, I want to zero each motor individually before assigning them to a MultiStepper grouping for the rest of the program.

So I tried to write this:

while ((!digitalRead(LIMIT_PIN_1))||(!digitalRead(LIMIT_PIN_3))){
   if (!digitalRead(LIMIT_PIN_1)) {stepper1.setSpeed(1000); stepper1.run();}  // Negative speed for reverse direction
   if (!digitalRead(LIMIT_PIN_2)) {stepper2.setSpeed(1000); stepper2.run();}  // Negative speed for reverse direction
  }

It compiles fine, but I get no movement in the steppers. No other changes were made. Is there a limit to AccelStepper that I don't know of in this reguard?

Shouldn't this not also be LIMIT_PIN_2 ?

Very true, thanks for picking that out. Would have been a lot of troubleshooting to fix that alone. However, the program at large has 8 motors, in pairings of 2, so there shouldn't be any null pointer issues with that typo, and even after fixing that, it does not work.

I think your logic is not correct. You are testing your limit switches and if they are LOW, you are stepping so you should continue if either of them is HIGH. It is easier if you put all this in a function

void home() {
  stepper1.setSpeed(1000);
  stepper2.setSpeed(1000);

  bool done = false;

  while ( !done ) {
    int limit1 = digitalRead(LIMIT_PIN_1);
    int limit2 = digitalRead(LIMIT_PIN_2);

    if (limit1 == LOW) stepper1.run();
    if (limit2 == LOW) stepper2.run();

    done = (limit1 == HIGH) && (limit2 == HIGH);
  }
}

Logic was sound, there was somehow a typo that happened between the logic integration and when I made the post, fixed that, zeroed like a charm.

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