The CD74HC4067 multiplexer, arduino and Pd

Hi all, I've been trying to use the CD74HC4067 multiplexer with arduino to read 16 (at least) analog inputs in Pd (Pure Data). I've checked the code in this web site http://bildr.org/2011/02/cd74hc4067-arduino/ but it wouldn't really do, so I searched a bit more and ended up with some code I partly wrote, partly copied myself, which is this

#define CONTROL0 11
#define CONTROL1 10
#define CONTROL2 9
#define CONTROL3 8

byte muxarray[16];

void setup() {
  Serial.begin(9600);
  pinMode(CONTROL0, OUTPUT);
  pinMode(CONTROL1, OUTPUT);
  pinMode(CONTROL2, OUTPUT);
  pinMode(CONTROL3, OUTPUT);
}

void loop() {
  for (int i = 0; i < 16; i++) {
    digitalWrite(CONTROL0, (i&15)>>3);
    digitalWrite(CONTROL1, (i&7)>>2);
    digitalWrite(CONTROL2, (i&3)>>1);
    digitalWrite(CONTROL3, (i&1));
    
    muxarray[i] = analogRead(0);
  }
    Serial.write(muxarray, 16);
}

Checking it yesterday with a couple of potentiometers (and the rest of the multiplexer inputs and arduino's inputs connected to ground), I was getting values from 0-255 over and over. Trying to correct this I tryied this

#define CONTROL0 11
#define CONTROL1 10
#define CONTROL2 9
#define CONTROL3 8

byte muxarray0[16];
byte muxarray1[16];

void setup() {
  Serial.begin(9600);
  pinMode(CONTROL0, OUTPUT);
  pinMode(CONTROL1, OUTPUT);
  pinMode(CONTROL2, OUTPUT);
  pinMode(CONTROL3, OUTPUT);
}

void loop() {
  for (int i = 0; i < 16; i++) {
    digitalWrite(CONTROL0, (i&15)>>3);
    digitalWrite(CONTROL1, (i&7)>>2);
    digitalWrite(CONTROL2, (i&3)>>1);
    digitalWrite(CONTROL3, (i&1));
    
    muxarray0[i] = analogRead(0);
    muxarray1[i] = muxarray0[i] >> 8;
  }
    Serial.write(muxarray0, 16);
    Serial.write(muxarray1, 16);
}

which really doesn't work. Trying the first code with 16 potentiometers attached to the circuit, today I was only getting jumping numbers. Anyone knows how to use this multiplexer with Pd? Btw, I know little about programming (at least with the arduino language).

Thanks

The output of analogRead() is 10 bits (0-1023). When you store that in a byte as you did in the first sketch it throws away the top 2 bits so instead of 0-1023 you get 0-255,0-255,0-255,0-255.

Either store the value in int (if you want the full range) or divide the value by 4 before you store it in a byte.

unsigned int muxarray[16];

//  OR

byte muxarray[16];
    muxarray[i] = analogRead(0) / 4;

Thanks! Diving by four worked fine (I had to solder the multiplexer's legs actually, that's why I was getting all that noise). But assigning my array as an unsigned int was causing an error to Serial.write...Got any idea?

Btw, I ended up with this sketch

#define CONTROL0 11
#define CONTROL1 10
#define CONTROL2 9
#define CONTROL3 8
#define CONTROL4 7
#define CONTROL5 6
#define CONTROL6 5
#define CONTROL7 4

byte muxarray0[16];
byte muxarray1[16];

void setup() {
  Serial.begin(9600);
  pinMode(CONTROL0, OUTPUT);
  pinMode(CONTROL1, OUTPUT);
  pinMode(CONTROL2, OUTPUT);
  pinMode(CONTROL3, OUTPUT);
  pinMode(CONTROL4, OUTPUT);
  pinMode(CONTROL5, OUTPUT);
  pinMode(CONTROL6, OUTPUT);
  pinMode(CONTROL7, OUTPUT);
}

void loop() {
  for (int i = 0; i < 16; i++) {
    digitalWrite(CONTROL0, (i&15)>>3);
    digitalWrite(CONTROL1, (i&7)>>2);
    digitalWrite(CONTROL2, (i&3)>>1);
    digitalWrite(CONTROL3, (i&1));
    
    muxarray0[i] = analogRead(0) / 4;
  }
  
  for (int j = 0; j < 16; j++) {
    digitalWrite(CONTROL4, (j&15)>>3);
    digitalWrite(CONTROL5, (j&7)>>2);
    digitalWrite(CONTROL6, (j&3)>>1);
    digitalWrite(CONTROL7, (j&1));
   
   muxarray1[j] = digitalRead(A1);
  } 
    Serial.write(muxarray0, 16);
    Serial.write(muxarray1, 16);
}

Which works rather fine, with 16 potentiometers and 16 buttons. I'm receiving the data in Pd as a group of 32 values, the two arrays I'm sending to Serial.write, but even though I do get the data, every time I start the Pd patch, the numbers go to a different place since the last time, randomly, which is not that good if you want to build a controller. Is there something I'm missing here? Maybe I should start the array with some specific number (I saw that in the 'repatcher' arduino sketch), and then do the for loop?

Greets

Looks like it settled down, routing analog values properly and buttons in some whatever row (except if it's some specific algorithm I'm unable of recognizing), but every time the same. I checked it four times, every time quitting Pd and unplugging and replugging the arduino and it works...can't really understand why, but I hope it stays like this.