AccelStepper - deceleration after switch button

Hello,
I need to help.
I have a stepper motor and an Arduino UNO, I need in the second condition (after i switch button) to decelerate motor to a complete stop.I've tried many variants but nothing worked well.
Could you help me? Thank you...

#include <AccelStepper.h>
int startTl = 2;
int rychleTl = 3;
int StartBt;
int RychlostBt;
int Position;

#define dirPin 6
#define stepPin 7
#define motorInterfaceType 1

AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup() {
  // maximum speed and acceleration:
  stepper.setMaxSpeed(2000000);
  stepper.setAcceleration(300000);
  //stepper.setSpeed(7000);
}

void loop() {
  StartBt = digitalRead(startTl);
  RychlostBt = digitalRead(rychleTl);

  if  (StartBt == HIGH && RychlostBt == HIGH ) { //speed
    stepper.setSpeed(6000);
    stepper.runSpeed();
  }
  else if (StartBt == HIGH && RychlostBt == LOW ) { //deceleration!!!
  long currentPosition = stepper.currentPosition();
  
    stepper.moveTo(currentPosition+10);
    stepper.runToPosition();
    stepper.stop();

  }


  else if (StartBt == LOW && RychlostBt == LOW ) {
  }

}

Try this and see what happens

void setup() {
  // maximum speed and acceleration:
  stepper.setMaxSpeed(2000);
  stepper.setAcceleration(200);
}

void loop() {
  StartBt = digitalRead(startTl);
  RychlostBt = digitalRead(rychleTl);

  if  (StartBt == HIGH && RychlostBt == HIGH ) { //speed
    stepper.move(50000L);
  }
  else if (StartBt == HIGH && RychlostBt == LOW ) { //deceleration!!!
    stepper.stop();
  }


  else if (StartBt == LOW && RychlostBt == LOW ) {
  }
  
  stepper.run();
}

...R

it Works :smiley: Thanks man!