Multi-Dimensional Array of #definitions

HI,

I am programming for the BigTreeTech Octopus board. I am trying to make cycling through the motors using a multi-dimensional array.

Like this:

//// Steppers
//
#define M0_STEP_PIN                         PF13  // MOTOR 0
#define M0_DIR_PIN                           PF12
#define M0_ENABLE_PIN                        PF14
#define M0_CS_PIN                            PC4

#define M1_STEP_PIN                          PG0   // MOTOR 1
#define M1_DIR_PIN                           PG1
#define M1_ENABLE_PIN                        PF15
#define M1_CS_PIN                            PD11

#define M2_STEP_PIN                          PF11  // MOTOR 2
#define M2_DIR_PIN                           PG3
#define M2_ENABLE_PIN                        PG5
#define M2_CS_PIN                            PC6

#define M3_STEP_PIN                         PG4   // MOTOR 3
#define M3_DIR_PIN                          PC1
#define M3_ENABLE_PIN                       PA0
#define M3_CS_PIN                           PC7

#define M4_STEP_PIN                         PF9   // MOTOR 4
#define M4_DIR_PIN                          PF10
#define M4_ENABLE_PIN                       PG2
#define M4_CS_PIN                           PF2

#define M5_STEP_PIN                         PC13  // MOTOR 5
#define M5_DIR_PIN                          PF0
#define M5_ENABLE_PIN                       PF1
#define M5_CS_PIN                           PE4

#define M6_STEP_PIN                         PE2   // MOTOR 6
#define M6_DIR_PIN                          PE3
#define M6_ENABLE_PIN                       PD4
#define M6_CS_PIN                           PE1

#define M7_STEP_PIN                         PE6   // MOTOR 7
#define M7_DIR_PIN                          PA14
#define M7_ENABLE_PIN                       PE0
#define M7_CS_PIN                           PD3


const  STEPPERS[8][4]{{M0_STEP_PIN, M0_DIR_PIN, M0_ENABLE_PIN,  M0_CS_PIN},
                            {M1_STEP_PIN, M1_DIR_PIN, M1_ENABLE_PIN,  M1_CS_PIN},
                            {M2_STEP_PIN, M2_DIR_PIN, M2_ENABLE_PIN,  M2_CS_PIN}, 
                            {M3_STEP_PIN, M3_DIR_PIN, M3_ENABLE_PIN,  M3_CS_PIN}, 
                            {M4_STEP_PIN, M4_DIR_PIN, M4_ENABLE_PIN,  M4_CS_PIN}, 
                            {M5_STEP_PIN, M5_DIR_PIN, M5_ENABLE_PIN,  M5_CS_PIN},
                            {M6_STEP_PIN, M6_DIR_PIN, M6_ENABLE_PIN,  M6_CS_PIN}, 
                            {M7_STEP_PIN, M7_DIR_PIN, M7_ENABLE_PIN,  M7_CS_PIN}};

Or this:

const   STEPPERS[8][4]{{PF13, PF12, PF14,  PC4},
                            {PG0, PG1, PF15,  PD11},
                            {PF11, PG3, PG5,  PC6}, 
                            {PG4, PC1, PA0,  PC7}, 
                            {PF9, PF10, PG2,  PF2},
                            {PC13, PF0, PF1,  PE4},
                            {PE2, PE3, PD4,  PE1}, 
                            {PE6, PA14, PE0,  PD3}};

In Marlin this header file defines the pins, but some are integers, and some are strings : "Marlin-bugfix-2.1.x\buildroot\share\PlatformIO\variants\MARLIN_H723Zx*variant_MARLIN_STM32H723ZX.h*"

Like Such:

#define PE13                    75
#define PE14                    76
#define PE15                    77
#define PF0                     78
#define PF1                     79
#define PF2                     80
#define PF3                     PIN_A14
#define PF4                     PIN_A15
#define PF5                     PIN_A16
#define PF6                     PIN_A17
#define PF7                     PIN_A18
#define PF8                     PIN_A19
#define PF9                     PIN_A20
#define PF10                    PIN_A21
#define PF11                    PIN_A22
#define PF12                    PIN_A23
#define PF13                    PIN_A24
#define PF14                    PIN_A25

I am not really sure how to initialize a multi-dimentional array when the data being stored varies.

If you are not sure, but want to also use the BTT Octopus for something other than a 3D Printer, then you might want to check out this thread on the PlatformIO Forum:

Thank you,
Jason

Actually they are all numbers. The PIN_Axx notation comes from a #define statement that will equate (eventually) to the actual hardware pin number.

1 Like

I like to loop through when I initialize pins like the example below.

//Relay Sensor Definitions
const byte RELAYS[4]{7,6,5,4};  //Relays{1,2,3,4}


void setup() {
  // Relay Pin Assignments:
  for (int iRelay  = 0; iVRelay  <= 3; iRelay ++) {
    pinMode(VALVES[iRelay ], OUTPUT);
    digitalWrite(VALVES[iRelay ], LOW);
  }
}

Thank you David,

Do you know where I might cross reference those actual pin numbers?

Try this file:

Thanks, Digging into it now. The data sheet wasn't working for me.

DaveX,

So I was able to hover over after including the "pins_arduino_analog.h". Now I figured out that way that what these pins were and it worked.

#define M3_STEP_PIN                         PG4   // MOTOR 3
#define M3_DIR_PIN                          203//PC1
#define M3_ENABLE_PIN                       192//PA0
#define M3_CS_PIN                           PC7

Now, I could just adjust everything, but I would really like to learn exactly what it is doing exactly. If you know could you explain it for me?

I would like to learn more so I can be more effective helping other and enhancing my coding skills.

Thanks for helping me regardless.

-Jason

If someone can use these new definitions @DaveX helped me discover.

//// Steppers
#define M0_STEP_PIN                         93	//PF13  // MOTOR 0
#define M0_DIR_PIN                           92	//PF12
#define M0_ENABLE_PIN                        94	//PF14
#define M0_CS_PIN                            206	//PC4

#define M1_STEP_PIN                          96	//PG0   // MOTOR 1
#define M1_DIR_PIN                           97	//PG1
#define M1_ENABLE_PIN                        95	//PF15
#define M1_CS_PIN                            59	//PD11

#define M2_STEP_PIN                          91	//PF11  // MOTOR 2
#define M2_DIR_PIN                           99	//PG3
#define M2_ENABLE_PIN                        101	//PG5
#define M2_CS_PIN                            38	//PC6

#define M3_STEP_PIN                         100	//PG4   // MOTOR 3
#define M3_DIR_PIN                          203	//203//PC1
#define M3_ENABLE_PIN                       192	//192//PA0
#define M3_CS_PIN                           39	//PC7

#define M4_STEP_PIN                         214	//PF9   // MOTOR 4
#define M4_DIR_PIN                          215	//PF10
#define M4_ENABLE_PIN                       98	//PG2
#define M4_CS_PIN                           82	//PF2

#define M5_STEP_PIN                         45	//PC13  // MOTOR 5
#define M5_DIR_PIN                          80	//PF0
#define M5_ENABLE_PIN                       81	//PF1
#define M5_CS_PIN                           68	//PE4

#define M6_STEP_PIN                         66	//PE2   // MOTOR 6
#define M6_DIR_PIN                          67	//PE3
#define M6_ENABLE_PIN                       52	//PD4
#define M6_CS_PIN                           65	//PE1

#define M7_STEP_PIN                         70	//PE6   // MOTOR 7
#define M7_DIR_PIN                          14	//PA14
#define M7_ENABLE_PIN                       64	//PE0
#define M7_CS_PIN                           51	//PD3

might consider organizing things as follows


struct Motor {
    const byte PinStep;
    const byte PinDir;
    const byte PinEnable;
    const byte PinCs;
}
motor [] = {
    {  93,  92,  94, 206 }, 
    {  96,  97,  95,  59 }, 
    {  91,  99, 101,  38 }, 
    { 100, 203, 192,  39 }, 
    { 214, 215,  98,  82 }, 
    {  45,  80,  81,  68 }, 
    {  66,  67,  52,  65 }, 
    {  70,  14,  64,  51 }, 
};
const int Nmotor = sizeof(motor)/sizeof(Motor);

// -----------------------------------------------------------------------------
void
loop (void)
{
}

void
setup (void)
{
    Serial.begin (9600);

    for (int n = 0; n < Nmotor; n++)  {
        pinMode (motor [n].PinStep,   OUTPUT);
        pinMode (motor [n].PinDir,    OUTPUT);
        pinMode (motor [n].PinEnable, OUTPUT);
        pinMode (motor [n].PinCs,     OUTPUT);
    }
}
2 Likes

That is an interesting way to organize things. I will give your suggestion a try and maybe adopt it in practice.

Thank You!

-Jason

Agree( seeing well written code is the best way to learn to write well written code. );

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.