Homing stepper help

178 views
Skip to first unread message

jimmyv...@gmail.com

unread,
Feb 5, 2022, 5:40:06 PM2/5/22
to accelstepper
I'm trying to home a stepper by going into one limit switch and then back out to another and seem to be having trouble making it move correctly.
When I power up the motor runs to the in limit switch but it doesn't seem to want to move to the out limit switch. It stops correctly at the in but when I ask it to move in the opposite direction it starts to go in the in direction no matter what I set the motion direction with a positive or negative step.
I'm not sure what to make of the direction problem and any help is appreciated.

#include <AccelStepper.h>

AccelStepper gantry(AccelStepper::DRIVER, 5, 6);

//VALUE FOR GANTRY MOVE
int gantryin = 46;
int gantryout = 43;

void setup() {

  pinMode(gantryout, INPUT);
  pinMode(gantryin, INPUT);

  Serial.begin(115200);

  gantry.setMaxSpeed(1500.0);

  while (digitalRead(gantryin) == HIGH) {
    gantry.move(1);
    gantry.setSpeed(100);
    gantry.runSpeed();
  }
  delay(1000);
  while (digitalRead(gantryout) == HIGH) {
    gantry.moveTo(-1);
    gantry.setSpeed(100);
    gantry.runSpeed();
  }
}

jimmyv...@gmail.com

unread,
Feb 6, 2022, 12:37:37 AM2/6/22
to accelstepper
I got it worked out finally, after searching the right thing on google I found a post by Gregor that said I needed to have the setSpeed() set to a negative value also.
The smallest things can really make you feel dumb. LOL

Jim Larson

unread,
Feb 7, 2022, 12:21:29 AM2/7/22
to accelstepper
You could also try changing gantry.moveTo(-1) to gantry.move(-1)  and using gantry.runSpeedToPosition() instead of gantry.runSpeed(). Then you shouldn't need to make speed negative.

jimmyv...@gmail.com

unread,
Feb 7, 2022, 10:30:55 PM2/7/22
to accelstepper
I tried using gantry.move(-1) and gantry.runSpeed(100); on my first try but it didn't move at all and had to change to
the gantry.moveTo(-1) to get it to move in any direction.
I know the difference as far as move() and moveTo() but not sure why the difference between the first while() statement and the second while() statement makes 1 move and the other not written with both as move().

Jim Larson

unread,
Feb 8, 2022, 12:26:20 AM2/8/22
to accelstepper
I'll do the experiment tomorrow and see what I can figure out. It "ought" to work! Not sure what's wrong.

Jim Larson

unread,
Feb 8, 2022, 5:16:34 PM2/8/22
to accelstepper
This is an interesting problem and I'm still checking into it. But I think this will solve your problem.
Note that runSpeed() ignores any target position it only reacts to the speed setting. That's why you had to use a negative value for speed to make it run in the opposite direction.
What you need to do is use runSpeedToPosition() in place of runSpeed(). Then move(-1) will work. So the code in the second while loop looks like:
gantry.move(-1);
gantry.setSpeed(100);
gantry.runSpeedToPosition();
Change gantry.runSpeed() in the first while loop.
That works for me.

jimmyv...@gmail.com

unread,
Feb 8, 2022, 8:27:38 PM2/8/22
to accelstepper
Thanks for explaining it so clear for me, I tried to move all motors with just runSpeed() instead of using the move() command as I'm always bouncing between limit switches and I didn't do it right so I went to the move(). With this explanation I think I will give it another try and just drop the move().
Thanks for the help.

Reply all
Reply to author
Forward
0 new messages