finding the fastest route through the accelstepper library

good day,

I'm kinda new to programming with the Arduino and now i'm working on a new home project where you have a closet that spins through 2 stepper motors that kinda looks like this : shelfs-jepg hosted at ImgBB — ImgBB

But now I'm running in a little issue, I'm using the accelstepper library were i can set the position for the stepper motors which is really working well but but what is happening is this lets say your current position is shelf 1 and you want to go to shelf 6 what it does is this: (shelf 1,2,3,4,5,6) it does 5 steps,
instead i want is: (shelf 1,6) just take 1 step, so my problem is i have no clue how to do this.

here is my code:

//for now i will use only 1 stepper motor
//and I'm sorry if my code looks absolute garbage
#include <AccelStepper.h>
AccelStepper stepperN(1, 9, 8); // pin 9 = step, pin 8 = direction
//the position from the shelfs
int shelf1 =0;
int shelf2 =1000;
int shelf3 =2000;
int shelf4 =3000;
int shelf5 =4000;
int shelf6 =5000;

void setup() {
 Serial.begin(9600); 
 Serial.println("type shelf1/6 ");
}

void loop() {
stepperN.run();
 while(Serial.available() > 0 ){
    String str = Serial.readString();
    if(str.indexOf("shelf1") > -1){
      stepperN.setAcceleration(50); stepperN.setMaxSpeed(313); stepperN.moveTo(shelf1);  stepperN.run();
      } 
      else if(str.indexOf("shelf2") > -1){
      stepperN.setAcceleration(50); stepperN.setMaxSpeed(313); stepperN.moveTo(shelf2);  stepperN.run();
      } 
      else if(str.indexOf("shelf3") > -1){
      stepperN.setAcceleration(50); stepperN.setMaxSpeed(313); stepperN.moveTo(shelf3);  stepperN.run();
      } 
      else if(str.indexOf("shelf4") > -1){
      stepperN.setAcceleration(50); stepperN.setMaxSpeed(313); stepperN.moveTo(shelf4);  stepperN.run();
      } 
      else if(str.indexOf("shelf5") > -1){
      stepperN.setAcceleration(50); stepperN.setMaxSpeed(313); stepperN.moveTo(shelf5);  stepperN.run();
      } 
      else if(str.indexOf("shelf6") > -1){
      stepperN.setAcceleration(50); stepperN.setMaxSpeed(313); stepperN.moveTo(shelf6);  stepperN.run();
      }     
    }
}

I hope someone is able to help me.

mikalting1:
instead i want is: (shelf 1,6) just take 1 step, so my problem is i have no clue how to do this.

I guess I don't understand the problem.

From a quick look at the code it seems to me it should go straight to whichever shelf you select.

Exactly what message are you sending to the Arduino?

Separately ...

Code is much easier to read if there is just one statement on each line.
You don't need stepperN.run(); in each IF statement. It is sufficient to have it in loop().
As these statements never change just put them in setup()

stepperN.setAcceleration(50); 
stepperN.setMaxSpeed(313);

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

...R
Serial Input Basics - simple reliable ways to receive data.