Efficient pinMode assignment of 72 pins

Hi all,

I am building a 24-way cable tester.

I have the following pin assignments, and I was wondering if there is an efficient way to do the pinMode assignments? c01-c24 and t01r-t24r are outputs, t01-t24 are inputs.

// control lines
int c01 = PIN_PE0; int c02 = PIN_PE1; int c03 = PIN_PE2; int c04 = PIN_PE3;
int c05 = PIN_PE4; int c06 = PIN_PE5; int c07 = PIN_PE6; int c08 = PIN_PE7;
int c09 = PIN_PH0; int c10 = PIN_PH1; int c11 = PIN_PH2; int c12 = PIN_PH3;
int c13 = PIN_PC4; int c14 = PIN_PC5; int c15 = PIN_PC6; int c16 = PIN_PC7;
int c17 = PIN_PJ0; int c18 = PIN_PJ1; int c19 = PIN_PJ2; int c20 = PIN_PJ3;
int c21 = PIN_PJ4; int c22 = PIN_PJ5; int c23 = PIN_PJ6; int c24 = PIN_PG2;

// green leds
int t01 = PIN_PF0; int t02 = PIN_PF1; int t03 = PIN_PF2; int t04 = PIN_PF3;
int t05 = PIN_PF4; int t06 = PIN_PH4; int t07 = PIN_PH5; int t08 = PIN_PH6;
int t09 = PIN_PB0; int t10 = PIN_PB1; int t11 = PIN_PB2; int t12 = PIN_PB3;
int t13 = PIN_PG0; int t14 = PIN_PC1; int t15 = PIN_PC2; int t16 = PIN_PC0;
int t17 = PIN_PG1; int t18 = PIN_PA7; int t19 = PIN_PA4; int t20 = PIN_PC3;
int t21 = PIN_PA6; int t22 = PIN_PA5; int t23 = PIN_PA3; int t24 = PIN_PA2;

// red leds
int t01r = PIN_PB4; int t02r = PIN_PB5; int t03r = PIN_PL0; int t04r = PIN_PG4;
int t05r = PIN_PH7; int t06r = PIN_PG3; int t07r = PIN_PL2; int t08r = PIN_PL1;
int t09r = PIN_PB6; int t10r = PIN_PB7; int t11r = PIN_PL3; int t12r = PIN_PL4;
int t13r = PIN_PL5; int t14r = PIN_PD0; int t15r = PIN_PD1; int t16r = PIN_PL7;
int t17r = PIN_PL6; int t18r = PIN_PD3; int t19r = PIN_PD6; int t20r = PIN_PD2;
int t21r = PIN_PD4; int t22r = PIN_PD5; int t23r = PIN_PA1; int t24r = PIN_PA0;

I have tried a for loop but getting c & 01, or t &01 into an unsigned char is proving problematic...

Many thanks

  1. Read the sticky at the top of the forum "Read this before posting a programming question".

  2. Post the complete sketch

It appears, and without a sketch or schematic (NOT a useless Fritzing picture), I am guessing that you have cascading port expanders.

I don't understand what you need in an unsigned char, but try putting the ports to initialize into an array.

I'm using an ATMEGA2560 with megacore for access to all pins.

Thanks for the pointers, I'm at the hardware design stage and just starting the sketch.

The entirety of the sketch without setup(){} and loop(){} is posted above, hence asking about pinMode (setup) bit.

So, just put the byte values into an array then init them in a loop.

// control lines
const byte ControlLines[24] = {
PIN_PE0,PIN_PE1,PIN_PE2,PIN_PE3,
PIN_PE4,PIN_PE5, PIN_PE6,PIN_PE7,
PIN_PH0,PIN_PH1,PIN_PH2,PIN_PH3,
PIN_PC4,PIN_PC5,PIN_PC6,PIN_PC7,
PIN_PJ0,PIN_PJ1,PIN_PJ2,PIN_PJ3,
PIN_PJ4,PIN_PJ5,PIN_PJ6,PIN_PG2};

setup()
{
  for (int i=0; i<24; i++)
  {
    pinMode(ControlLines[i], INPUT);
  }
}

Thanks John,

With this type of setup, will I still be able to access individual pins in the main sketch as previously defined i.e. c01 = PIN_PE0?

Just read the code properly, and I can rename ControlLines to whatever I want or need. Nice, saved 50% dynamic memory versus the line-by-line method.

Yes. It will be called ControlLines[0] but you are probably going to do the same operation on a series of control lines so you would be using a loop instead of writing the same code 24 times:

  for (int i=0; i<24; i++)
  {
    if (digitalRead(ControlLines[i]) == HIGH)
    {
        digitalWrite(GreenLEDPins[i] , HIGH);
        digitalWrite(RedLEDPins[i] , LOW);
  }
  else
  {
        digitalWrite(GreenLEDPins[i] , LOW);
        digitalWrite(RedLEDPins[i] , HIGH);
  }

Yes, that relationship doesn't change just because all the pins are listed in an array.
If they were all sequential, you wouldn't even need the array:

for (x = 2; x <26; x=x+1){
pinMode (x, INPUT_PULLUP);
}
for (x = 27; x <54; x=x+1){
pinMode (x, OUTPUT);
}

for example.

Standard Mega only has 70 IO broken out, 0 to 53 and 54 to 69.

There are other Mega variants that bring all 86 IO out, here's one example:
[url]http://www.crossroadsfencing.com/BobuinoRev17/[/url]

I've iterated forward, backward and now moving forward again with the best (neatest) pcb design for the 2560, and the pins will not align in a nice sequential order for me this time.

Thanks John, that's perfecto.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.