Initialize pins in separate cpp file

I was hoping to assign pinModes in a separate *.cpp as to keep my main.cpp clean and easier to read.

I have limited C++ expriance and would appreciate some guidance.

Lets' say I would like to do the following in a separate *.cpp

How would I structure the *.h file and the *.cpp file.


void initialize_pins(){

    /*Initialize Dosing Pumps*/
  pinMode(M0_STEP_PIN, OUTPUT);
  pinMode(M0_DIR_PIN, OUTPUT);
  pinMode(M0_ENABLE_PIN, OUTPUT);
  pinMode(M0_CS_PIN, OUTPUT);

  pinMode(M1_STEP_PIN, OUTPUT);
  pinMode(M1_DIR_PIN, OUTPUT);
  pinMode(M1_ENABLE_PIN, OUTPUT);
  pinMode(M1_CS_PIN, OUTPUT);

  pinMode(M2_STEP_PIN, OUTPUT);
  pinMode(M2_DIR_PIN, OUTPUT);
  pinMode(M2_ENABLE_PIN, OUTPUT);
  pinMode(M2_CS_PIN, OUTPUT);

  pinMode(M3_STEP_PIN, OUTPUT);
  pinMode(M3_DIR_PIN, OUTPUT);
  pinMode(M3_ENABLE_PIN, OUTPUT);
  pinMode(M3_CS_PIN, OUTPUT);

  pinMode(M4_STEP_PIN, OUTPUT);
  pinMode(M4_DIR_PIN, OUTPUT);
  pinMode(M4_ENABLE_PIN, OUTPUT);
  pinMode(M4_CS_PIN, OUTPUT);

  pinMode(M5_STEP_PIN, OUTPUT);
  pinMode(M5_DIR_PIN, OUTPUT);
  pinMode(M5_ENABLE_PIN, OUTPUT);
  pinMode(M5_CS_PIN, OUTPUT);

  pinMode(M6_STEP_PIN, OUTPUT);
  pinMode(M6_DIR_PIN, OUTPUT);
  pinMode(M6_ENABLE_PIN, OUTPUT);
  pinMode(M6_CS_PIN, OUTPUT);

  pinMode(M7_STEP_PIN, OUTPUT);
  pinMode(M7_DIR_PIN, OUTPUT);
  pinMode(M7_ENABLE_PIN, OUTPUT);
  pinMode(M7_CS_PIN, OUTPUT);
}

Thank you,
Jason

All constants go into the header file, also the external function declaration.

It may be easier to define an array of pin numbers for use in a for loop.

why not pass your sub-function an array of pins, dirs and possibly initial state for output pins

This is one of the many solutions:

const int motorPins[][4] = 
{
  { 22, 23, 24, 25},       // step, dir, enable, cs
  { 28, 29, 30, 31},
  { 34, 35, 36, 37},
  { 40, 41, 42, 43},
};

void setup()
{
  // make all the pins output and low
  for( int motor=0; motor<4; motor++)
  {
    for( int pin=0; pin<4; pin++)
    {
       pinMode( motorPins[motor][pin], OUTPUT);
    }
  }
}

I agree, I normally do it that way. Some reason this compiles but does not work.

I have coded my projects like this consistently and they have been fast and 100% working, but for some reason that escapes me I can not figure out why this compiles without errors but still does not function as coded...

If I do it without the array it works, but efficiently programmed it does not? I have done this programming a millions times, but this has me scratching my head???

//// 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

#define MOTOR_STEP                  0
#define MOTOR_DIRECTION             1
#define MOTOR_ENABLE                2
#define MOTOR_CS                    3


#define MOTOR_ENABLE                      LOW
#define MOTOR_DISABLE                     HIGH

#define MOTOR_FORWARD                     HIGH
#define MOTOR_REVERSE                     LOW


const int 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}};

void setup() {
    for (int i = 0; i <= 7; i++) { // Initialize STEPPERs
      pinMode(STEPPER[i][MOT_STEP], LOW);
      pinMode(STEPPER[i][MOT_DIRECTION], LOW);
      pinMode(STEPPER[i][MOT_ENABLE], DISABLE);
      pinMode(STEPPER[i][MOT_CS], LOW);
    }
}

void loop() {
    for (int i = 0; i <= 7; i++) { 
      SerialUSB.print("Motor  : ");
      SerialUSB.println(i, DEC);
      step_motor(i, MOTOR_FORWARD,  (STEPS_PER_MILLILETER * 1));//30
    }
  
  void step_motor(int iMOTOR, int iDIR, int iSTEPS){
    for (int i = 0; i <= iSTEPS; i++) { // Step iSTEPS
      digitalWrite(LED_PIN, HIGH);
  
      digitalWrite(STEPPER[iMOTOR][MOT_STEP], HIGH);  
      delayMicroseconds(STEP_TIMER);
      digitalWrite(STEPPER[iMOTOR][MOT_STEP], LOW);
  
      digitalWrite(LED_PIN, LOW);
    }
  }


So do not define the dimension of the first part of the array?

In the "Preferences" set compiler warnings to "All".

Then the compiler says exactly what the problem is, with line numbers and everything.
warning: "MOTOR_ENABLE" redefined
You have a "#define MOTOR_ENABLE" twice.

HOnestly, I am having a WTF arduino programming moment.

I have done this many times without issue and 100% efficient code execution.

I am stuck in an infinite head scratching loop on this one, lol

Use "const int motorPins[][4] =" or "const int motorPins[10][4] =", it is the same.

Do you see that I use the pin numbers directly in the array.
Perhaps you have programming experience, but in the Arduino world it is often better to be direct and not abstract. Keep it simple and keep it easy to maintain.

Sorry, you are right.

In the version that does not work I changed those definitions to :slight_smile:


#define MOT_STEP                  0
#define MOT_DIRECTION             1
#define MOT_ENABLE                2
#define MOT_CS                    3

Right, I am using the BTT Octopus Board. So there are 8x stepper motors.

That is why I did :slight_smile: const int motorPins[8][4]. There are 8 motors, and 4x pins per motor.

Should I try it this way:

const int motorPins[][4]

over this way:
const int motorPins[8][4]

???

This is puzzling me...

It does not matter, either you or the compiler will fill in the 8.

Try to listen better to the compiler, turn also the "verbose" mode on for the compiler output.
error: 'STEPPER' was not declared in this scope, note: suggested alternative: 'STEPPERS'

This bulky unnecessary code works, the logical array construct does not and I do not know why?

//// 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


void setup() {
  pinMode(M0_STEP_PIN, OUTPUT);
  pinMode(M0_DIR_PIN, OUTPUT);
  pinMode(M0_ENABLE_PIN, OUTPUT);
  pinMode(M0_CS_PIN, OUTPUT);

  pinMode(M1_STEP_PIN, OUTPUT);
  pinMode(M1_DIR_PIN, OUTPUT);
  pinMode(M1_ENABLE_PIN, OUTPUT);
  pinMode(M1_CS_PIN, OUTPUT);

  pinMode(M2_STEP_PIN, OUTPUT);
  pinMode(M2_DIR_PIN, OUTPUT);
  pinMode(M2_ENABLE_PIN, OUTPUT);
  pinMode(M2_CS_PIN, OUTPUT);

  pinMode(M3_STEP_PIN, OUTPUT);
  pinMode(M3_DIR_PIN, OUTPUT);
  pinMode(M3_ENABLE_PIN, OUTPUT);
  pinMode(M3_CS_PIN, OUTPUT);

  pinMode(M4_STEP_PIN, OUTPUT);
  pinMode(M4_DIR_PIN, OUTPUT);
  pinMode(M4_ENABLE_PIN, OUTPUT);
  pinMode(M4_CS_PIN, OUTPUT);

  pinMode(M5_STEP_PIN, OUTPUT);
  pinMode(M5_DIR_PIN, OUTPUT);
  pinMode(M5_ENABLE_PIN, OUTPUT);
  pinMode(M5_CS_PIN, OUTPUT);

  pinMode(M6_STEP_PIN, OUTPUT);
  pinMode(M6_DIR_PIN, OUTPUT);
  pinMode(M6_ENABLE_PIN, OUTPUT);
  pinMode(M6_CS_PIN, OUTPUT);

  pinMode(M7_STEP_PIN, OUTPUT);
  pinMode(M7_DIR_PIN, OUTPUT);
  pinMode(M7_ENABLE_PIN, OUTPUT);
  pinMode(M7_CS_PIN, OUTPUT);
}



void step_motor(int iMOTOR, int iDIR, int iSTEPS){
    switch (iMOTOR) {
      case 0:
        digitalWrite(M0_DIR_PIN, iDIR); // Set Direction
        digitalWrite(M0_ENABLE_PIN, MOTOR_ENABLE); // Enable Motor For Stepping
        break;

     case 1:
        digitalWrite(M1_DIR_PIN, iDIR); // Set Direction
        digitalWrite(M1_ENABLE_PIN, MOTOR_ENABLE); // Enable Motor For Stepping
        break;

      case 2:
        digitalWrite(M2_DIR_PIN, iDIR); // Set Direction
        digitalWrite(M2_ENABLE_PIN, MOTOR_ENABLE); // Enable Motor For Stepping
        break;

      case 3:
        digitalWrite(M3_DIR_PIN, iDIR); // Set Direction
        digitalWrite(M3_ENABLE_PIN, MOTOR_ENABLE); // Enable Motor For Stepping
        break;

      case 4:
        digitalWrite(M4_DIR_PIN, iDIR); // Set Direction
        digitalWrite(M4_ENABLE_PIN, MOTOR_ENABLE); // Enable Motor For Stepping
        break;

      case 5:
        digitalWrite(M5_DIR_PIN, iDIR); // Set Direction
        digitalWrite(M5_ENABLE_PIN, MOTOR_ENABLE); // Enable Motor For Stepping
        break;

      case 6:
        digitalWrite(M6_DIR_PIN, iDIR); // Set Direction
        digitalWrite(M6_ENABLE_PIN, MOTOR_ENABLE); // Enable Motor For Stepping
        break;

      case 7:
        digitalWrite(M7_DIR_PIN, iDIR); // Set Direction
        digitalWrite(M7_ENABLE_PIN, MOTOR_ENABLE); // Enable Motor For Stepping
        break; 

      default:
        // statements
        break;
    }  




  for (int i = 0; i <= iSTEPS; i++) { // Step iSTEPS
    digitalWrite(LED_PIN, HIGH);
    
    switch (iMOTOR) {
      case 0:
        digitalWrite(M0_STEP_PIN, HIGH);
        delayMicroseconds(STEP_TIMER);
        digitalWrite(M0_STEP_PIN, LOW);
        break;

     case 1:
        digitalWrite(M1_STEP_PIN, HIGH);
        delayMicroseconds(STEP_TIMER);
        digitalWrite(M1_STEP_PIN, LOW);
        break;

      case 2:
        digitalWrite(M2_STEP_PIN, HIGH);
        delayMicroseconds(STEP_TIMER);
        digitalWrite(M2_STEP_PIN, LOW);
        break;

      case 3:
        digitalWrite(M3_STEP_PIN, HIGH);
        delayMicroseconds(STEP_TIMER);
        digitalWrite(M3_STEP_PIN, LOW);
        break;

      case 4:
        digitalWrite(M4_STEP_PIN, HIGH);
        delayMicroseconds(STEP_TIMER);
        digitalWrite(M4_STEP_PIN, LOW);
        break;

      case 5:
        digitalWrite(M5_STEP_PIN, HIGH);
        delayMicroseconds(STEP_TIMER);
        digitalWrite(M5_STEP_PIN, LOW);
        break;

      case 6:
        digitalWrite(M6_STEP_PIN, HIGH);
        delayMicroseconds(STEP_TIMER);
        digitalWrite(M6_STEP_PIN, LOW);
        break;

      case 7:
        digitalWrite(M7_STEP_PIN, HIGH);
        delayMicroseconds(STEP_TIMER);
        digitalWrite(M7_STEP_PIN, LOW);
        break; 

      default:
        // statements
        break;
    }   
    
    digitalWrite(LED_PIN, LOW);

  }


void loop() {
//int iLoops = 100;

if (!digitalRead(E3_DIAG_PIN)){
  iSteps_Counter++;
  //SerialUSB.print("STEPS: ");
  //SerialUSB.println((iSteps_Counter *  STEPS_PER_MILLILETER), DEC);
  for (int i = 0; i <= 7; i++) { 
    SerialUSB.print("Motor  : ");
    SerialUSB.println(i, DEC);
    step_motor(i, MOTOR_FORWARD,  (STEPS_PER_MILLILETER * 1));//30
  }

}
'''

The compiler does not report any errors at all. The 8 is understood. I like to be static for non dynamic arrays, but seriously, this is odd to me.

Is there a special verbose mode in PlatformIO I may not be aware of?

I yield to the smarter coder here.

The Arduino IDE 2.0 compiler output tells exactly what is wrong, yet you use PlatformIO because it is "better" :face_with_spiral_eyes:

Just to clarify, when I use the bulky code version it works.

When I design it for efficient arrays, it doesn't.

now the interesting thing is the motors enable, and they feel like they are trying to step, but they do not, then they disable.

Would more efficient code require me to increase the delay between step+ and step-?

I am using PlatformIO. It is giving me zero errors. I love PlatformIO!

Also, I am using a BTT Octopus, this project needs PlatformIO...

What exactly is it about the project that needs PlatformIO ?