AccelStepper after setting current position

Hello,
I would like to ask a question about AccelStepper_h after setCurrentPosition.
First Initialization of motor position. This is done in void setup(), so it does not loop.
I setCurrentPosition(0) to 0.
With a "while" I move my stepper to negative position (-maxDist) until a limit switch is pressed.
Then I set the current position to negative value -maxDist/2 where the limit switch is.
Then I want to move back to 0, (so the position is in the middle of -maxDist/2 and +maxDist.2), but for some reason motor moves in the same negative direction instead (in fact it goes to -maxDist).
The only way to do this if I set the current position to +maxDist/2. The motor starts moving to 0 in reverse.
Any idea why this is happening?
Here is the code for this initialization:
void setup()
{
stepper2.setAcceleration(accel);
stepper2.setPinsInverted(false, false, true);

stepper2.setEnablePin(8);

digitalWrite(posPin,HIGH); //set limit switch pin high
stepper2.setCurrentPosition(0);
while (digitalRead(posPin)==1) {
stepper2.moveTo(-maxDist);
stepper2.setSpeed(300);
stepper2.runSpeed();
}
stepper2.setCurrentPosition(-maxDist/2);
delay(1000);
Serial.println((String)"Current position:"+stepper2.currentPosition()); //this displays correctly -xxx
//delay(1000);
//stepper2.moveTo(0);
stepper2.runToNewPosition(0); //moves in the same direction as previous.

}

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