I have a function in which I change the pinMode on many of my Arduino's pins based on a passed in mode. I first construct an array of bytes specifying each pin to be an input or an output and then I iterate through the array to set all the pins.
void pinout(byte mode) {
switch(mode) {
case 0: char pins[13] = {'O','O','I','I','O','I','I','I','I','O','I','I','O'}; break;
case 1: char pins[13] = {'O','I','I','O','O','I','O','O','I','O','I','O','I'}; break;
//etc..
}
for(byte i = 0; i <= 13; i++) {
if(pins[i] == 'I') pinMode(i, INPUT);
else pinMode(i, OUTPUT);
}
}
However, since the array is defined inside the switch statement, I loose scope of the pins array when I move onto the for loop. I tried defining the pins[]
outside of the switch statement, but then I am confused on how to populate it efficiently. These all threw errors when I tried to populate the variable inside the switch statement:
pins[0,1,2,3,...,12,13] = {...};
pins[13] = {...};
pins[] = {...};
pins = {...};
I know I could go through each entry individually, but this could be rather inefficient use of memory and computational power as I add more cases. Any ideas?