Defining Arrays of Objects and in AVR

Hi y’all,

I have two questions about declaring arrays.

I have been working with the Accelstepper library to make a collection of musical stepper motors. I’m currently only working with 2 stepper motors and have been declaring them as shown below:

AccelStepper STEPPER_0(AccelStepper::DRIVER, 4);
AccelStepper STEPPER_1(AccelStepper::DRIVER, 6);

(Please note that the direction pin in this case is not important to what I’m doing, so it has just been tied low.)

I’m planning to add many more stepper motors later on, but I know that if I don’t find a way to wrap all of my individual Accelstepper objects into an array of some sort that my code is going to get messy very fast. Is there a way to declare an array of objects instead of having to define each one individually? I’ve tried researching how to do this before but didn’t have any luck.

In the same project, I’ve also been using the AVR library to define pins for driving the sleep enable / disable pin on each stepper motor driver. Similarly with wrapping my Accelstepper objects into an array, it would make the rest of my code a lot cleaner if I could wrap all the sleep pins into an array.

I know how to declare an array of pins using pinMode() like below:

int OUTPUT_PINS[] = {2, 4, 6};

void setup(){
  for(int p = 0; p < sizeof(OUTPUT_PINS); p ++){
    pinMode(OUTPUT_PINS[p], OUTPUT);

But I’d like to learn how to do something similar in AVR with macro functions, if it is possible…? I’m assuming it must be, but I’m still a bit of a newb with AVR. I’m currently just defining the pins as below:


Any suggestions or points towards the right direction are much appreciated!

Thanks y’all,

1 untested:

AccelStepper STEPPER[] {
  {AccelStepper::DRIVER, 4},
  {AccelStepper::DRIVER, 6}

2 why?
avoid using precompiler #define when you can use a variable instead. Only thing I would do is to make it const and use a byte variable instead

const byte OUTPUT_PINS[] = {2, 4, 6};

and from C++11 on you can use a auto range for

for (auto &p : OUTPUT_PINS)
  pinMode(p, OUTPUT);