Putting rows together? serial monitor/serial.print

Hello ello,

i’m a relative newbie to the arduino environment, and although I have a lot of patience with programming, i’ve come to a dead end after a lot of work (almost done though!).

I’m using a multiplexer with 6 piezo pins connected to analog 0, and then 3 more piezo’s connected to 1, 3 and 4. So I’m using 9 sensors in total.

I need to make the piezo’s values print to the serial ‘matrix style’ in rows, so that max/msp can read it successfully.

Max reads the information fine (and the serial monitor is understandable) when it’s just the multiplexer data being sent, or alternatively, just when the analog pins 1, 3 and 4 are enabled, but as soon as I try and compile the two together it doesn’t work, and the serial monitor goes into overdrive :S

I have tried adding delays into the code (maybe something to do with the different resolutions is messing with it???), testing different Serial.print options and placing the data in different parts of the sketch but nothing else I can think of is working :frowning:

any help or advice would be masssssively appreciated!

/*
 * code example for using a 4051 * analog multiplexer / demultiplexer
 * by david c. and tomek n.* for k3 / malm? h?gskola
 * modifications by Richard Hoadley & mr osborn
 * 
 *
 */  

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;     // storing 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 val = 0;     // storage
int val1 = 0;    // storage 2 if needs be



void setup(){
// (upload = dev/tty.usbserial-A600bRNW);
  pinMode(2, OUTPUT);    // s0
  pinMode(3, OUTPUT);    // s1
  pinMode(4, OUTPUT);    // s2
 digitalWrite(led, HIGH); 
//  Serial.begin(9600);
    Serial.begin(115200);
}

void loop () {

  for (count=0; count<=7; count++) {
    row = bin[count];      
    r0 = row & 0x01;
    r1 = (row>>1) & 0x01;
    r2 = (row>>2) & 0x01;
    digitalWrite(2, r0);
    digitalWrite(3, r1);
    digitalWrite(4, r2);    
    // Serial.println(bin[count]);


     // ANALOG PINS 1, 3 and 4 in use, 2 & 5 grounded
 
     
  { 
        val = analogRead(1);    
    //  Serial.print("x1 ");  // analog pin 1   (   X1)
      Serial.print(val);
      Serial.print("\t");
 
      }
      
   { 
        val = analogRead(2);    
     //   Serial.print("x2: ");  // analog pin 2 nothing (grounded)
        Serial.print(val);
        Serial.print("\t");
       
      }
      
      
       { 
        val = analogRead(3);    
     //  Serial.print("x3: ");  // analog pin 3  (   X3)
        Serial.print(val);
         Serial.print("\t");
   
      }
      
      
       { 
        val = analogRead(4);    
     //  Serial.print("x3: ");  // analog pin 4   (  X2)
        Serial.print(val);
        Serial.print("\t");
   
      }
      
       { 
        val = analogRead(5);    
     //  Serial.print("x3: ");  // analog pin 5  (grounded again)
        Serial.print(val);
         Serial.print("\t");
   //     Serial.println( );       
    }
      

      
// MUX PINS 0 - 5 in use, 6 & 7 just to ground     
// int  bin [] = {000, 1, 10, 11, 100, 101, 110, 111};     
   //             yo, y1, y2, y3,  y4,  y5,  y6, y7 

 
  
 if ( bin[count] == 000 )   // yo
 {     
        val = analogRead(0);
      // Serial.println("y0 ");
       Serial.print(val);
       Serial.print("\t"); 
 }
 
 if ( bin[count] == 1 )  // y 1
{
        val = analogRead(0);
        //Serial.println("y1 ");
        Serial.print(val);
       Serial.print("\t");
               
}

 if ( bin[count] == 10 )   // y2
 {     
        val = analogRead(0);
      // Serial.println("y2 ");
       Serial.print(val);
       Serial.print("\t"); 
 }


 if ( bin[count] == 11 )   // y3
 {     
        val = analogRead(0);
      // Serial.println("y3 ");
       Serial.print(val);
       Serial.print("\t"); 
 }

 if ( bin[count] == 100 )   // y4
 {     
        val = analogRead(0);
      // Serial.println("y4 ");
       Serial.print(val);
       Serial.print("\t"); 
 }
 
  if ( bin[count] == 101 )   // y5
 {     
        val = analogRead(0);
      // Serial.println("y4 ");
       Serial.print(val);
       Serial.print("\t"); 
 }
 
  if ( bin[count] == 110 )   // y6 nothing, groundead
 {     
        val = analogRead(0);
      // Serial.println("y4 ");
       Serial.print(val);
       Serial.print("\t"); 
 }
 
 
  if ( bin[count] == 111 )   // y7 nothing,  grarnded
 {     
        val = analogRead(0);
      // Serial.println("y5 ");
       Serial.print(val);
       Serial.print("\t"); 
      Serial.println();
 }


 


// int  bin [] = {000, 1, 10, 11, 100, 101, 110, 111};     
   //             yo, y1, y2, y3,  y4,  y5,  y6, y7
      {    
    delay (4);
  }  // end of for
  
} // end of loop
}

http://img31.imageshack.us/img31/7679/togetheruhhh.png

I'm operating with a proxy server that block images from imageshask.us, so I can't see your picture. But, I have to wonder why you are reading from analog pins that are grounded. You know that you will get a 0. Why spend the time that analogRead takes when you know you are going to get a 0?

Calls to delay are not going to improve the way the data is printed in the serial monitor.

but as soon as I try and compile the two together it doesn't work, and the serial monitor goes into overdrive :S

What does this mean? How does it "not work"?

I've never seen the serial monitor go into overdrive. I didn't even know it could. Is that accompanied by a neat sound track, too? What does this mean?

Hi, thanks for the reply.

I wasn't very clear in stating that I can read the sensors fine in the arduino environment (so it's not just 0's that are printed) until I try read the mux's sensors and the straight analog pin's sensors at the same time (evaluating the entire block as is coded ^^^^)...

this is when there are extra rows (x axis) made in the print window for seemingly no reason (there should be only 12 rows printed right?)...and also the values don't behave themselves and stay in their own row when played with once the code is evaluated in total. :-?

I'd start with un-commenting these types of lines:

// Serial.println("y4 ");

Change the println to print, and add a space in front of the text, too.

Then, run the program. You should see what each value is. The extra/wrong values will then be identifiable. You, and we can help, will have a better chance of identifying what is wrong.

Ahhh, thanks, that was a good idea to get some clarity back.

After doing what you’ve said, I have found that it repeats the analog pins (printed “x_”) data over an over (seven times) in between the mux data (printed “y_”).

This means that I could calculate which inlet row should go where in max (not very tidy and a loooong route signal would be needed), or alternatively I could some how try and get rid of that repeating data, any ideas would be very welcome!

(far left of serial monitor)
http://yfrog.com/juscreenshot20100223at133p

(far right of serial monitor)
http://yfrog.com/evscreenshot20100223at153p

It looks like you are reading the non-multiplexed pins inside the multiplex loop. Try moving the non-multiplexed reads (the ones of pins 1 to 5) to before the for (count=0; count<=7; count++) line.

shit, thanks. ahahah i am such a fool!

Thank you so much both of you, that has worked like a dream. I'm internally grateful for your help! :)