unable two read last to inputs from a 4051 mux

Hi everyone, I’m in a physical computing class that has to do with interactive furniture and I’m working on my final project. I’m making a “musical” bookshelf by reading in the weight of someone’s books and playing back in MIDI and samples. Everything was going fine until I ran into a few problems recently.

Before I post all the photos, code, and all that, I thought I would first just see if people are familiar with the problem I’m having. My setup is such:

24 pressure sensors and handful of pots being read through 4 separate 4051 multiplexors. Using the Arduino2Max package, I have all that going into max and controlling various noises. Or that is what I want. Right now I am testing it out with just 8 sensors and 1 mux. For the most part, everything has been working great, but recently I’ve had a bug that I haven’t been able to figure out.

Most relevant info: What happens is that the LAST TWO inputs into the mux (y6 and y7, straddling the wire to analog input in Arduino) are not being read by MaxMsp and just remain 0 regardless of how hard I press the sensor. The curious thing is that the rest of the inputs and sensors work perfectly.

I’ve tried different power, different muxes, different breadboards, different wires, different sensors, and anything else you could possibly change. It’s definitely not the hardware and not the wires. When I go back to the non-modified Arduino2Max code, and just do an analogRead on the pin from the mux, I can read every sensor. Within MaxMsp, everything is set up as normal through the Arduino2Max. So if it’s not the hardware, and not the arduino code, and not MaxMsp, what is the problem? Is there some magic going on between Arduino and MaxMSp that is ruining this whole setup? Who knows.

One strange point, that I have had a problem with is that i can’t see anything from a Serial.print() inside the if-statement in arduino2max, and if I try to Serial.print() the values from Serial.available–I get -1 and Serial.read()–I get nothing. This has me thinking that the if-statement is not evaluating, but it has to be if the rest of my setup is working.

Anyway, I’m posting this because I’m at my wits end and can’t think of any possible reason for this.

Also, a random question, but, if I actually get this all working, does anyone know if there will be significant slowdown by reading in 24 sensors + POTS into MaxMsp and triggering various samples? So far it hasn’t been an issue, and I’m hoping if there is slowdown it won’t be too much :-/

For completions sake, I decided to post the code, too (although it’s not too different from the original)

//////////////////////////////////////////////////////////////////////code example


 * codeexample for useing a 4051 * analog multiplexer / demultiplexer
 * by david c. and tomek n.* for k3 / malmö högskola

int x = 0;
int led = 13; //just a led
int r0 = 0; //value select pin at the 4051 (s0)
int r1 = 0; //value select pin at the 4051 (s1)
int r2 = 0; //value select pin at the 4051 (s2)
int row = 0; // storeing the bin code
int count = 0; // just a count
int bin [] = {000, 1, 10, 11, 100, 101, 110, 111};//bin = binär, some times it is so easy
int myAnalogue = 0;

void setup(){

  pinMode(2, OUTPUT);    // s0
  pinMode(3, OUTPUT);    // s1
  pinMode(4, OUTPUT);    // s2
 digitalWrite(led, HIGH); 
//  beginSerial(9600);
Serial.begin(9600); //115200

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);



if (Serial.available() > 0){         // Check serial buffer for characters
    if (Serial.read() == 'r') {       // If an 'r' is received then read the pins

  for (count=0; count<=7; count++) {
    row = bin[count];      
    r0 = row & 0x01;
  //  Serial.print("r0 is :");
  //  Serial.println(r0);
    r1 = (row>>1) & 0x01;
  //  Serial.print("r1 is :");
  //  Serial.println(r1);
    r2 = (row>>2) & 0x01;
   // Serial.print("r2 is :");
   // Serial.println(r2);
   // Serial.print(r2);
    digitalWrite(2, r0);
    digitalWrite(3, r1);
    digitalWrite(4, r2);
   myAnalogue =  analogRead(0);
  // Serial.println(bin[count]);
  //  Serial.println(myAnalogue);
   // Serial.println();
   // delay (1000);
  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(32, BYTE); 

edit, I just realized that this probably falls under the software interfacing forum as well/instead?

hey I dont think I can help, but im also very interested in using a multiplexer with max. can you show any screenshots of the patch? pointers/ etc.

thanks so much


i had a similar problem, and i solved it by swapping two of the wires going from the 4051 to the digital inputs on the Arduini board. Unfortunately i cant remember which two it was. But give it a try.

Well, I figured out the problem. It is in the MaxMSP patch if you go inside the "arduinoSerialReader" patch there is an unpack object that is assigning all the incoming values to variables for use later on. The problem is that it is only assigning 6 analog inputs since it's assuming you're just using as many analog pins as arduino has.

So you have to put a lot more i's within the unpack object, and then shift everything over to read the additinoal analog inputs.

As far as a screenshot, I'm just using this: http://www.arduino.cc/playground/uploads/Interfacing/Arduino2MAX_V.4.zip

Which you can download for free and use.