Declaring AccelStepper variables w/ arrays

I’m working to clean up my code for a sketch where I’m declaring many stepper motors w/ AccelStepper with better use of arrays, but I’m not sure how to simplify the AccelStepper constructor I’m declaring as a global variable.

I was hopeful that I could declare these in an array with a for() loop in the setup() section, but then they are not set as global variables.

My plan for the project will continue to scale with more motors, so I’m hopeful there is a way to simplify how I define the AccelStepper variables.

Appreciate the coding tips!

#include <AccelStepper.h>
#define HALFSTEP 8

int MotorPin1[] = {3, 7, 10, 14, 47, 22, 23, 30, 31, 38, 39, 48};
int MotorPin2[] = {2, 6, 11, 15, 49, 24, 25, 32, 33, 40, 41, 46};
int MotorPin3[] = {5, 9, 12, 16, 51, 26, 27, 34, 35, 42, 43, 52};
int MotorPin4[] = {4, 8, 13, 17, 53, 28, 29, 36, 37, 44, 45, 50};

int x;
const int NumberOfSteppers = 12;
float MaxSpeed = 1200;
float Accel = 55000;
float Speed = 1400;

/////////// TRYING TO SIMPLIFY THIS SECTION BELOW

AccelStepper stepper0(HALFSTEP, MotorPin1[0], MotorPin3[0], MotorPin2[0], MotorPin4[0]);
AccelStepper stepper1(HALFSTEP, MotorPin1[1], MotorPin3[1], MotorPin2[1], MotorPin4[1]);
AccelStepper stepper2(HALFSTEP, MotorPin1[2], MotorPin3[2], MotorPin2[2], MotorPin4[2]);
AccelStepper stepper3(HALFSTEP, MotorPin1[3], MotorPin3[3], MotorPin2[3], MotorPin4[3]);
AccelStepper stepper4(HALFSTEP, MotorPin1[4], MotorPin3[4], MotorPin2[4], MotorPin4[4]);
AccelStepper stepper5(HALFSTEP, MotorPin1[5], MotorPin3[5], MotorPin2[5], MotorPin4[5]);
AccelStepper stepper6(HALFSTEP, MotorPin1[6], MotorPin3[6], MotorPin2[6], MotorPin4[6]);
AccelStepper stepper7(HALFSTEP, MotorPin1[7], MotorPin3[7], MotorPin2[7], MotorPin4[7]);
AccelStepper stepper8(HALFSTEP, MotorPin1[8], MotorPin3[8], MotorPin2[8], MotorPin4[8]);
AccelStepper stepper9(HALFSTEP, MotorPin1[9], MotorPin3[9], MotorPin2[9], MotorPin4[9]);
AccelStepper stepper10(HALFSTEP, MotorPin1[10], MotorPin3[10], MotorPin2[10], MotorPin4[10]);
AccelStepper stepper11(HALFSTEP, MotorPin1[11], MotorPin3[11], MotorPin2[11], MotorPin4[11]);

AccelStepper* stepperPtrArray[NumberOfSteppers]={&stepper0,&stepper1,&stepper2,&stepper3,&stepper4,
&stepper5,&stepper6,&stepper7,&stepper8,&stepper9,&stepper10,&stepper11};

/////////// TRYING TO SIMPLIFY THIS SECTION ABOVE

void setup() {
  for (x = 0; x < NumberOfSteppers; x++)
  {
    stepperPtrArray[x]->setMaxSpeed(MaxSpeed);
    stepperPtrArray[x]->setAcceleration(Accel);
    stepperPtrArray[x]->setSpeed(Speed);
  }
  
}

Try this. I only implemented 3 of your steppers because typing all those pin numbers was too much work. But, as you can see, it’s easily expanded.

#include <AccelStepper.h>
#define HALFSTEP 8

uint8_t MotorPins[][4] = {
  {3, 2, 5, 6}, {7, 6, 9, 8}, {14, 15, 16, 17}
};

const uint8_t NumberOfSteppers = sizeof(MotorPins) / sizeof(MotorPins[0]);

AccelStepper *stepperPtrArray[NumberOfSteppers];

float MaxSpeed = 1200;
float Accel = 55000;
float Speed = 1400;


void setup() {
  for (uint8_t x = 0; x < NumberOfSteppers; x++)  {
    stepperPtrArray[x] = new AccelStepper(MotorPins[x][0], MotorPins[x][1], MotorPins[x][2], MotorPins[x][3]);
    stepperPtrArray[x]->setMaxSpeed(MaxSpeed);
    stepperPtrArray[x]->setAcceleration(Accel);
    stepperPtrArray[x]->setSpeed(Speed);
  }
}

void loop() {
}

You can also just define an array of steppers statically:

#include <AccelStepper.h>
#define HALFSTEP 8

AccelStepper stepperArray[] = {
  {3, 2, 5, 6}, {7, 6, 9, 8}, {14, 15, 16, 17}
};

const uint8_t NumberOfSteppers = sizeof(stepperArray) / sizeof(stepperArray[0]);

float MaxSpeed = 1200;
float Accel = 55000;
float Speed = 1400;


void setup() {
  for (uint8_t x = 0; x < NumberOfSteppers; x++)  {
    stepperArray[x].setMaxSpeed(MaxSpeed);
    stepperArray[x].setAcceleration(Accel);
    stepperArray[x].setSpeed(Speed);
  }
}

void loop() {
}

gfvalvo:
Try this. I only implemented 3 of your steppers because typing all those pin numbers was too much work. But, as you can see, it’s easily expanded.

This worked perfectly! Thanks!!