combining two pieces of code to optimize multiplex

hello,
I am working on a project that requires the implementation of 4x16 ch analog multiplexers. It looks like most people who have used multiple muxers (generally the 4051's) have put them in a series so that the arduino polls the inputs first of one, then the next, and so on, eventually returning to the start position. While this seems effective, I need a lower latency solution. I cannot wait for such a long loop. What I want to do is use my current code for 1 multiplexer, use the same digital pins connected to all the muxers, and monitor each arduino analog input separately. I am working with MAX in conjunction with the arduino, which means I am using a serial object. Here is my current arduino code:

byte data_in = 0;
byte data_send = 0;


int anPin = 0;


byte setPort = 0;


void setup() {
Serial.begin(9600);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
}


void loop() {
if(Serial.available() > 0) {
data_in = Serial.read();
if(data_in == 76) {
for(int i = 0; i <= 7; i ++)
{
setPort = i * 4;
PORTD = setPort;
data_send = analogRead(anPin) / 4;
Serial.print(data_send);
}
Serial.flush();
}
}
}

which is being used with this max patch
http://www.madzach.com/16chmux.png

I am seeking to combine the above arduino code with something like this:

int x = 0;                              // a place to hold pin values
int ledpin = 13;

void setup()
{
  Serial.begin(115200);               // 115200 is the default Arduino Bluetooth speed
  digitalWrite(13,HIGH);              ///startup blink
  delay(600);
  digitalWrite(13,LOW);
  pinMode(13,INPUT);
}



void loop()
{ 

if (Serial.available() > 0){         // Check serial buffer for characters
        
    if (Serial.read() == 'r') {       // If an 'r' is received then read the pins
    
for (int pin= 0; pin<=5; pin++){      // Read and send analog pins 0-5
    x = analogRead(pin);
    sendValue (x);
    }

for (int pin= 2; pin<=13; pin++){     // Read and send digital pins 2-13
    x = digitalRead(pin);
    sendValue (x);
    }
  
    Serial.println();                 // Send a carriage returnt to mark end of pin data. 
    delay (5);                        // add a delay to prevent crashing/overloading of the serial port
  
  }

 }
}

void sendValue (int x){              // function to send the pin value followed by a "space". 
 Serial.print(x);
 Serial.print(32, BYTE); 
}

this arduino code is designed to go with this max patch
http://www.madzach.com/arduino2max

Basically I need to integrate the multiplexing into the arduino2max max patch.

Any ideas?

thanks,
Zach

slight correction:
here is the first piece of arduino code--

byte data_in = 0;
byte data_send = 0;


int anPin = 0;


byte setPort = 0;


void setup() {
Serial.begin(9600);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
}


void loop() {
if(Serial.available() > 0) {
data_in = Serial.read();
if(data_in == 76) {
for(int i = 0; i <= 15; i ++)
{
setPort = i * 4;
PORTD = setPort;
data_send = analogRead(anPin) / 4;
Serial.print(data_send);
}
Serial.flush();
}
}
}

i am working on something much like this, but instead of 4x16 pots, i am working on 5x16 pots. will still have one analog left for an expansion, plus the other inputs of the previous 4051's are still available.

have you figured this out yet? would love to see the code if possible.

cheers...

lewis

Just remember that even if Arduino has 6 analog pins it still only has one ADC so the one ADC will probably be more likely to be the bottleneck than the way you hook up your mux chips.

thanks for that.
doing arduino work is just like learning max, a lot of questions [most of them really stupid and silly]