Defining a set of pins as digital input in one statement

Hi -

For one of my project, I’m using around 30 digital pins(finding it tedious to write to define each one of them as digital input),and want to define all of them as input - with just one statement!

Wondering if there is one, would be grateful if shown a way to do it and syntax, (below is example - a small portion of the coding)

int sen={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
#define pinMode(A0, INPUT);
#define pinMode(A1, INPUT);
#define pinMode(A2, INPUT);
#define pinMode(A3, INPUT);
#define pinMode(A4, INPUT);
#define pinMode(A5, INPUT);
#define pinMode(A6, INPUT);
#define pinMode(A7, INPUT);

void setup() {

for(int i;i<32;i+1)
{
PinMode(sen*,LOW);*
}

``````#define pinMode(A0, INPUT);
#define pinMode(A1, INPUT);
#define pinMode(A2, INPUT);
#define pinMode(A3, INPUT);
#define pinMode(A4, INPUT);
#define pinMode(A5, INPUT);
#define pinMode(A6, INPUT);
#define pinMode(A7, INPUT);
``````

Nonsense. Go back to the example codes for a little bit and study up on syntax a little. This is nothing even close to how to set pinModes. #define is a find and replace, you can’t call lines of code this way.

( Note how I use code tags so the i doesn’t turn mine to itallics.)

``````void setup() {

for(int i;i<32;i+1)
{
PinMode(sen[i],LOW);
}
``````

It’s spelled pinMode, c++ is case sensitive. And pinMode doesn’t need HIGH or LOW, it needs INPUT or OUTPUT. LOW works since it has the same value as INPUT, but it looks stupid and you don’t want to look like that.

But yes, a for loop is how you would do this. Although if the pins are all sequential like that, you don’t really need the array do you?

I'm learning! thanks, could you pl help with how to designate many pins as Digital inputs in one statement

Delta_G already told you; replace LOW by INPUT.

sterretje:
Delta_G already told you; replace LOW by INPUT.

Put he didn't post it so I can just copy and paste

.

rockwellramesha:
I’m learning! thanks, could you pl help with how to designate many pins as Digital inputs in one statement

It can’t be done in one statement. You have to use a for loop.

If there were a smaller number of pins and they were all on the same port then you could use direct port manipulation. But that’s not the case here.

rockwellramesha:

``````for(int i; i<32; i++) {
``````

pinMode(sen[i], INPUT);
}

That IS one statement. If you meant “one line” then:

``````for(int i; i<32; i++) {PinMode(sen[i], INPUT);}
``````

(C++ doesn’t generally care about space, tab, and newline characters between tokens.)

Delta_G, direct port manipulation - thanks for this tip

Also, pins DEFAULT to "INPUT" (possibly not including the analog pins), so you don't really need to configure them at all.

rockwellramesha:
For one of my project, I’m using around 30 digital pins(finding it tedious to write to define each one of them as digital input),and want to define all of them as input - with just one statement!

You could use

``````;
``````

if you insist in using one statement to initialize all inputs (in setup), because

westfw:
pins DEFAULT to “INPUT” (possibly not including the analog pins), so you don’t really need to configure them at all.

And another single statement that does the trick (using a comma expression):

``````  pinMode(sen[0], INPUT),  pinMode(sen[1], INPUT),  pinMode(sen[2], INPUT),  pinMode(sen[3], INPUT),  pinMode(sen[4], INPUT),  pinMode(sen[5], INPUT),  pinMode(sen[6], INPUT),  pinMode(sen[7], INPUT),  pinMode(sen[8], INPUT),  pinMode(sen[9], INPUT),  pinMode(sen[10], INPUT),  pinMode(sen[11], INPUT),  pinMode(sen[12], INPUT),  pinMode(sen[13], INPUT),  pinMode(sen[14], INPUT),  pinMode(sen[15], INPUT),  pinMode(sen[16], INPUT),  pinMode(sen[17], INPUT),  pinMode(sen[18], INPUT),  pinMode(sen[19], INPUT),  pinMode(sen[20], INPUT),  pinMode(sen[21], INPUT),  pinMode(sen[22], INPUT),  pinMode(sen[23], INPUT),  pinMode(sen[24], INPUT),  pinMode(sen[25], INPUT),  pinMode(sen[26], INPUT),  pinMode(sen[27], INPUT),  pinMode(sen[28], INPUT),  pinMode(sen[29], INPUT),  pinMode(sen[30], INPUT), pinMode(sen[31], INPUT);
``````

johnwasser:
And another single statement that does the trick (using a comma expression):

``````  pinMode(sen[0], INPUT),  pinMode(sen[1], INPUT),  pinMode(sen[2], INPUT),  pinMode(sen[3], INPUT),  pinMode(sen[4], INPUT),  pinMode(sen[5], INPUT),  pinMode(sen[6], INPUT),  pinMode(sen[7], INPUT),  pinMode(sen[8], INPUT),  pinMode(sen[9], INPUT),  pinMode(sen[10], INPUT),  pinMode(sen[11], INPUT),  pinMode(sen[12], INPUT),  pinMode(sen[13], INPUT),  pinMode(sen[14], INPUT),  pinMode(sen[15], INPUT),  pinMode(sen[16], INPUT),  pinMode(sen[17], INPUT),  pinMode(sen[18], INPUT),  pinMode(sen[19], INPUT),  pinMode(sen[20], INPUT),  pinMode(sen[21], INPUT),  pinMode(sen[22], INPUT),  pinMode(sen[23], INPUT),  pinMode(sen[24], INPUT),  pinMode(sen[25], INPUT),  pinMode(sen[26], INPUT),  pinMode(sen[27], INPUT),  pinMode(sen[28], INPUT),  pinMode(sen[29], INPUT),  pinMode(sen[30], INPUT), pinMode(sen[31], INPUT);
``````

But if you do this, don't come here and expect anyone to try to help you debug it. One of the goals of writing code is to make it readable in case you ever need anyone else to try to understand it.

``````int sen[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
``````

Good idea using an array. Bad idea using pins 0 and 1 on most Arduinos. Which Arduino is this for?

You don’t need any of those #-defines.

Note that you can use the names printed on the PCB as the names for the pins. You might use…

``````int sen[]={A0,A1,A2,SDA,SCL,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
``````

On some Arduinos, you even have names like TXLED as an addressable pin.

Mega2560, I've moved to pins beginning 22(realized errors!).Thx John Wasser, Delta_G and MorganS