NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Define an array of Pin difentions

March 19, 2021
by scootergarrett
scootergarrett's Avatar

I use the BV.h header to set and clear pins. I would like to do some setting and clearing of pins in a loop, and it would be nice if I could put all my pin definitions into an array so I could loop through them with a signal command

Something like this:

#define M1_EnablePin  D,7
#define M2_EnablePin  D,6
#define M3_EnablePin  D,5
#define M4_EnablePin  D,4

#define  EnablePin[] = {M1_EnablePin, M2_EnablePin, M3_EnablePin}, M4_EnablePin};

...

for(MotorIndex = 0; MotorIndex < 4; ++MotorIndex)
{
    SET(EnablePin[MotorIndex]);
}

the preprocessor doesn't like passing the pins definitions like this. I see the Arduino can do this because there pin definitions are just a number. dose anyone have a good handle on preprocessor with pin definitions?

March 27, 2021
by sask55
sask55's Avatar

Hi

I think the problem is with your #define statement

#define  EnablePin[] = {M1_EnablePin, M2_EnablePin, M3_EnablePin}, M4_EnablePin};

I don't think that is valid use of #define

It looks like the SET function is expecting a char and a int as arguments. In the example the char remains constant "D", only the int argument changes.

something like this may work

 int epins[4] ={7,6,5,4};
 for(MotorIndex = 0; MotorIndex < 4; ++MotorIndex)
{
   SET(D,epins[MotorIndex]);
 }

or even more directly don't use #define or an array at all

  for(MotorIndex = 7; MotorIndex > 3; --MotorIndex)
  {
   SET(D,MotorIndex);
  }
March 27, 2021
by scootergarrett
scootergarrett's Avatar

Thanks for the input. Your idea works until I change the pin from D3 to another port, say C0.

Post a Reply

Please log in to post a reply.

Did you know that our kits have shipped to hobbyists in 40 countries? Learn more...