I2C expander and arrays

I've got a mass of I/O so I got a I2C I/O expander. (Sparkfun SX1509)
I had planned to address the I/O with arrays, so ...

for (int i=0; i<10; i++) 
{
  myData[i] = digitalRead(myButtons[i]);
}

But I've now found that to address the pins on the expander I need to use ...

for (int i=0; i<10; i++) 
{
  myData[i] = myObject.digitalRead(myButtons[i]);
}

Obviously the same applies to myObject.digitalWrite(myLeds*)*
Is there a simple way to code the read/write opperations so that myData is partly populated with myObject.digitalWrite() and partly with digitalWrite() ?
I've thought of a couple of solutions:
1) use two SX1509 expanders or
2) Or I think I could use something like the code below to write some sort of myReadFunction() and myWriteFunction(),
But I'm hoping there's a more elegant solution. Any clever ideas?
* *for (int i=0; i<5; i++) {   myData[i] = myObject.digitalRead(myButtons[i]); } for (int i=5; i<10; i++) {   myData[i] = digitalRead(myButtons[i]); }* *
Thanks.

option 2 is OK (separate calls) but ensure that what you have in the myButtons array matches what's expected.

You could assign pins on the I/O expander to be greater than 100 (or whatever) and then use that knowledge to do either a regular read or an expander read. This would allow you to mix/match pins at random.

const byte myButtons[] = { 2,3,4,5,6,101,102,103,104,105 };

int myRead( byte pin ) {
  int val;
  if ( pin > 100 ) {
    val = myObject.digitalRead( pin - 100 );
  } else {
    val = digitalRead( pin );
  }
  return val;
}

for (int i=0; i<10; i++)
{
  myData[i] = myRead(myButtons[i]);
}

Thanks.
Somehow I was hoping there was a simpler way (not that this is complicated) but I guess not. :slight_smile:

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