Noob Question On Code!

when u select the pin mode to input or output is their a way in which instead of putting

 pinMode(0, INPUT);
 pinMode(1, INPUT);
 pinMode(2, INPUT);
 pinMode(3, INPUT);
 pinMode(4, INPUT);

u can put it all in 1 line if you understand like

pinMode(0,1,2,3,4 INPUT);

No, but you could do it in a for loop:

for(int i=0; i<4; i++)
{
  pinMode(i, INPUT);
}

oh thanks

No, but you could do it in a for loop:

In theory (IIRC) you could extend the class where pinMode() is declared to add another polymorphic pinMode() function that would take an array as the first parameter instead of an int, and the function would loop thru that array like your code does, Paul.

Of course, that kind of change is probably beyond the level of understanding of DJB right now...and of course it would break on the next update unless you somehow convinced the Arduino crew to include the update in the next release.

:D

The difficulty with overloading the pinMode function is that it can't take a variable number of arguments, since the last argument is the mode.

One could overload pinMode to take either a since int or an array of ints as the 1st argument. But it's only one line of code in setup(), if done right, versus a bunch of code in wiring.c and wiring.h.

Could you assign the last spot in the array to be a 0 or 1, thus replacing INPUT and OUTPUT respectively? This way you can set up as many pins as you want with an array running in the pinMode function

INPUT and OUTPUT are just symbolic names for 0 and 1.

The whole idea of passing an array to pinMode is theoretical, since pinMode is not currently overloaded to take an array as an argument. If it were, then an array of pins and a mode as the two arguments, or a pin and a mode as the two arguments make more sense than a single argument where the last value in the array is the mode.

thanks for the replys i did a lot of arduino code but then stoped and i cant remember so is there a way u could talk to me in a more understanding way thanks

One could overload pinMode to take either a since int or an array of ints as the 1st argument. But it's only one line of code in setup(), if done right, versus a bunch of code in wiring.c and wiring.h.

That's what I was getting at, and I was only half-serious. It might be a nice addition for a future release, perhaps (though I guess that would fork the wiring stuff, then, wouldn't it?). A similar change to the read functions that returned a pointer to an array might be nice.

But yeah, ultimately it is all a big change that you would have to do to various bits of internal logic, far beyond what nearly any Arduino fan or user would want to do. Unless the Arduino team (or Wiring, or Processing - not sure who's in charge of those bits) wants to do it, it likely isn't worth doing except as an experimental thing, especially since doing it the "regular way" is straightforward enough.

:)

Just to clarify, PaulS’s for loop will set pins 0, 1, 2 and 3.

i, the counter, starts at 0 and goes up by 1 while the condition (i<4) is true. When i is 4, i is not less than 4 so the loop will terminate.

To set pins 0 to 4 you’d need i < 5 or i <= 4.

is there a way u could talk to me in a more understanding way

What? "polymorphic overloading of base classes" isn't understandable?

Basically, there is no function for setting the direction of multiple pins at one time, but you could write such a function. Either specific to your program, or to be included in the standard Arduino libraries. People have now gone off on a tangent and are discussing the "best" ways to write this function... At any moment, the discussion should turn to other programming languages and how they'd work better...

Note: the default mode of pins is "INPUT"; so you really don't need a list ofpinMode(n, INPUT);statements at all.

Maximum code efficiency would involve using direct port manipulation, which allows (nearly insists) on setting the pin directions for an entire port (8 pins) in two instructions

DDB = 0x11;  // pins 7 through 13
DDD = 0xC3;  // pins 0 through 7