Help! AccelStepper passing as a parameter

Hi everyone,

I'm using AccelStepper.h to run 4 stepper motors and control them individually. I have created a function but I don't know what type of data is the "AccelStepper" so I can assign it as a variable?
I attached my code, and the data type is the only thing stopping me to continue forward.

Appreciate your helps guys

void runposition_stepper (int x, AccelStepper stepper1) {

  if (stepper1.currentPosition() < 0) {
    while ( stepper1.currentPosition() < 0 & stepper1.currentPosition() != 0) {
      stepper1.setSpeed(setspeed);
      stepper1.run();
      delay (2);
      Serial.println (stepper1.currentPosition());
      continue;
    }
    while (stepper1.currentPosition() != x) {
      stepper1.setSpeed(setspeed);
      stepper1.runSpeed();
      Serial.println (stepper1.currentPosition());
      continue;
    }





  } else  if (stepper1.currentPosition() > 0) {





    while ( stepper1.currentPosition() > 0 & stepper1.currentPosition() != 0) {
      stepper1.setSpeed(-setspeed);
      stepper1.run();
      delay (2);
      Serial.println (stepper1.currentPosition());
      continue;
    }
    while (stepper1.currentPosition() != x) {
      if (x < 0) {
        stepper1.setSpeed(-setspeed);
        stepper1.runSpeed();
        Serial.println (stepper1.currentPosition());
      } else if ( x > 0) {
        stepper1.setSpeed(setspeed);
        stepper1.runSpeed();
        Serial.println (stepper1.currentPosition());
      }
    }





  } else  if (stepper1.currentPosition() == 0) {



    while (stepper1.currentPosition() != x) {
      if (x < 0) {
        stepper1.setSpeed(-setspeed);
        stepper1.runSpeed();
        Serial.println (stepper1.currentPosition());
      } else if ( x > 0) {
        stepper1.setSpeed(setspeed);
        stepper1.runSpeed();
        Serial.println (stepper1.currentPosition());
      }
    }
  }
}

What error are you getting?

And please post the complete program.

...R

You can look in AccelStepper.h to see AccelStepper is a class.

So you should be passing a reference to it rather than copying the object:

void runposition_stepper1 (int x, AccelStepper & stepper1) {

Though since runposition_stepper1 seems to be specific to stepper1 there's no need to pass it.

Hi,
Sorry if I'm wasn't clear enough,

I'm trying to control each motor individually in the loop with the function, so I was thinking something like the following based on the user input:

void loop() {


  
  while (Serial.available()) {
    user_input = Serial.read();           //Read user input and trigger appropriate function
    if (user_input == '1')
    {
      runposition_stepper (-30, stepper1);

      printintro (stepper1.currentPosition(), stepper2.currentPosition(), stepper3.currentPosition(), stepper4.currentPosition());
    }
    else if (user_input == '2')
    {
      runposition_stepper (40, stepper2);

      printintro (stepper1.currentPosition(), stepper2.currentPosition(), stepper3.currentPosition(), stepper4.currentPosition());
    }
    else if (user_input == '3')
    {
      runposition_stepper (10, stepper3);

      printintro (stepper1.currentPosition(), stepper2.currentPosition(), stepper3.currentPosition(), stepper4.currentPosition());
    }
    else if (user_input == '4')
    {
      runposition_stepper (40, stepper4);

      printintro (stepper1.currentPosition(), stepper2.currentPosition(), stepper3.currentPosition(), stepper4.currentPosition());
    }
  }
}

Therefore I wanted a function in which I can assign each stepper individually. like this for example

void runposition_stepper (int x, "data type?" stepper ) {

but I'm still don't know what type of data I should assign it for? or how I can do that?

Robin2:
What error are you getting?

And please post the complete program.

...R

I'm getting " 'AccelStepper' has not been declared" I guess as @MarkT said "AccelStepper" is a class.

My question is how I can assign "stepper" as a variable in the function?

One solution would be to create a global array of AccelStepper instances and then you can refer to them by index, such as

for (byte n = 0; n < 4; n++) {
   myStepper[n].run();
}

...R