multiple initialization

Hi people,

I'm doing this:

pinMode(3, OUTPUT);
pinMode(4,OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);

is there anyway to do it faster? something like multple declaration, like pinMode(3,4,5,6,7,8,9,10, OUTPUT); or something like that

Thanks

for(int i = 3; i < 11; i++)
   pinMode(I, OUTPUT);

nice :slight_smile:

You could do it the way you suggested if you overload the pinMode function ]:smiley:

void pinMode(unsigned int pinA, unsigned int pinB, unsigned int mode)
{
  pinMode(pinA,mode);
  pinMode(pinB,mode);
}

void pinMode(unsigned int pinA, unsigned int pinB, unsigned int pinC, unsigned int mode)
{
  pinMode(pinA,mode);
  pinMode(pinB,mode);
  pinMode(pinC,mode);
}

... etc ...

Or re-write the pinMode function so it uses variable arguments (as printf does)

function pinMode(unsigned int pinA, unsigned int pinB, unsigned int mode)

"function" - that's a C++ keyword, is it?

D'oh... Switching between two or three programming languages in a short period of time always melts my brain.

I was partially in javascript there. s/function/void/g

is there anyway to do it faster?

Note that using a for loop is not faster than individual statements. In fact, it might be slower, because of the overhead of the loop. It is shorter, though, in terms of the number of lines of code.

If you really meant faster, then you need to look at direct port manipulation.

In fact, it might be slower, because of the overhead of the loop

That depends on optimization.

-funroll-loops (part of -O2 iirc) will convert it to individual calls to the pinMode function with static values. Makes for bigger code, but faster execution.

That depends on optimization.

That's why I said might, rather than will.

As we have no control over optimazation with the IDE, we can't be exactly sure what the compiler will be told to do.