pin setup

hi, just wondering if there is an easier way to set up input and output pins.
im using a mega adk. and have set up 50 pins and pin names with int.

I know I can use a for loop to set all the pins to either output or input. but this doesn't assign the pin name.
the only way I know of is to type 50 pinMode lines of code to assign each individually.
is there a way of taking the pin name and number from the int code line and running them both through a for loop?

thanks.

suttle0577:
is there a way of taking the pin name and number from the int code line and running them both through a for loop?

You need to post an example of what you are thinking of doing.

You can create an array with the pin numbers and then iterate over the array to set the mode.

You could also have a series of variables that give names to the elements of the array so that you could also address an single pin with something like

switchPinVal = digitalRead(pinArray[switchPin]);

...R

hi thanks for the reply.
don’t have a example as such yet. im trying to replace an old plc for a robot and thought id have a go at using an Arduino.
have wrote the program for the inverter controller, which will be run in a Arduino nano.

And ive now started to write the program for the main controller . it has 23 inputs and 18 outputs.

thats as far as i am at moment.

understand what your saying, type all the pin names and pin numbers into a variable and use for loop to read the variable and put info into the pinMode comand.
will this work to set up all the int code statements for the pins aswell.

thanks for your help

suttle0577:
understand what your saying, type all the pin names and pin numbers into a variable and use for loop to read the variable and put info into the pinMode comand.

I’m not sure you have quite the correct idea. Which is why I was hoping you would describe your own plan. When you set out your thinking I don’t waste time telling you things you already know. And I don’t assume you know things that are obvious to me :slight_smile:

Be aware that variable names don’t exist in a running program - they are only used by the compiler.

If you want to iterate over the array (for example to set pinMode) you need to do it like this

for (byte n = 11; n <= 13; n++) {
   pinMode(arrayPin[n], OUTPUT);
}

Obviously that only works if all the OUTPUT pins are in consecutive elements of the array. But you can put the pin numbers in any position in the array. For example

arrayPin[11] could have the value 9 (for pin 9)
arrayPin[12] could have the value 26 (for pin 26)
arrayPin[13] could have the value 27 (for pin 27)

and then the above code snippet would set pins 9, 26 and 27 as OUTPUT.

And, (referring pack to my earlier Reply) if the variable switchPin has the value 12 then

switchPinVal = digitalRead(pinArray[switchPin]);

will read the value of pin 26 because that is what is in array element 12

Of course it would make more sense to have pin 26 as an INPUT or INPUT_PULLUP but you should get the idea.

…R

If you’re complaining about needing to type:

static const int LED_DOOR_OPEN = 1;
static const int LED_DOOR_CLOSED = 2;
   :
static const int RELAY_OPEN_DOOR = 30;
static const int RELAY_CLOSE_DOOR = 30;

For all 50 pins, then I sympathize, but all I can suggest is getting good at copy&paste (of “static const int LED_”, for example), or using a more capable editor than the one that is provided with the Arduino IDE, and then copying or importing the text into your sketch.
Note that I have carefully demonstrated the sort of error that can occur if you’re not careful!