Pages: [1]   Go Down
Author Topic: separating analog channel data in Max/MSP  (Read 748 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I dont understand how to separate each analog channel's serial data coming into max. I know the 10 and 13 are ascii newline and carriage return, but how do I know what data is coming from what channel? I have tried using the example patch to filter out the 10's and 13's but if more than one analog stream is being captured and sent,I cant work out a way of separating them... please help!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
if more than one analog stream is being captured and sent,I cant work out a way of separating them.
You need to change the code that is sending the data. Either prefix each value sent with the pin number of the sender (2:865), or send all the data every time (100, 390, 205, 890, etc).
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

oh, that makes sense. trouble is for me i cant seem to change the code correctly :-(
the below code is based on an example i found using a multiplexer to get 16 analog in channels into Max. My changes to add a pin number dont work. Any ideas what I need to change? Thanks a lot!
Code:
byte mask = 1;
byte serialOut;
byte i;
int val;

void setup()
{
 
  //Declare the pins used for controlling the multiplexer as inputs
  for (i = 2; i < 6; i++){
    pinMode(i, OUTPUT); 
    digitalWrite(i, LOW);
  }
 
  Serial.begin(9600);
}

void loop()
{
  delay(100);
  for (i = 0; i < 16; i++) {
   
    val = readAnalogM(0, i);
    if (i == 0 ) Serial.println(i, val);  // is this line correct to get the pin name followed by the data?
  } 
}


int readAnalogM( int analogIn, int channel ){
    /*
      analogIn - board's analog pin concected to the output of a multiplexer
      channel - multiplexer channel being read, 0 to 15
    */
   
    serialOut = 2;
   
    //Get binary form of the channel and send it through serialOuts
    for ( mask = 1; mask < 16 ; mask <<= 1 ){
   
       if ( mask & channel ) {
         digitalWrite( serialOut++, HIGH );
       } else {
         digitalWrite( serialOut++, LOW );
       }
    }
    return analogRead( analogIn );
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    if (i == 0 ) Serial.println(i, val);  // is this line correct to get the pin name followed by the data?
No, that line is not correct. The Serial.print() functions output ONE value. To output two values (you really need three - pin number, delimiter, value), you need more than one Serial.print() statement.

Syntactically, that statement is fine, but it will try to output 0 to val decimal places.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh I see, so if I change the loop to this;

  for(int i = 0; i < 16; i ++){
    Serial.print(i);
    Serial.print(" ");
    Serial.println(readMux(i));
    delay(100);
  }

I get what I want in the Serial Monitor, which is channel number, a space, then last the data. But when I print the serial object's output in Max I get only one datastream and the values are different to the 0 to 1023... Why is the data different in Max???? Its like Max isnt taking in all three Serial.print statements but only the third Serial.print data value. and then scaling it in some bizaare way...
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Why is the data different in Max????
No idea. Can't help you with that side of the serial stream.

Perhaps if you posted your Max code, someone else could help.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i think the issue is that arduino language converts serial data to integer values between 0 and 1023. But how can I remove this conversion and just get the channel number, a space, and then the value? Below is code and a pic of the max patch...
Code:
//Mux control pins
int s0 = 8;
int s1 = 9;
int s2 = 10;
int s3 = 11;

//Mux in "SIG" pin
int SIG_pin = 0;

void setup(){
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);

  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);

  Serial.begin(9600);
}

void loop(){

  //Loop through and read all 16 values
  //Reports back Value at channel 6 is: 346
  for(int i = 0; i < 16; i ++){

[color=red]    Serial.print(i); // return the channel number
    Serial.print(" "); /// put a space inbetween channel number and value
    Serial.print(readMux(i));// return the value[/color]
    delay(10);
  }

}

int readMux(int channel){
  int controlPin[] = {s0, s1, s2, s3};

  int muxChannel[16][4]={
    {0,0,0,0}, //channel 0
    {1,0,0,0}, //channel 1
    {0,1,0,0}, //channel 2
    {1,1,0,0}, //channel 3
    {0,0,1,0}, //channel 4
    {1,0,1,0}, //channel 5
    {0,1,1,0}, //channel 6
    {1,1,1,0}, //channel 7
    {0,0,0,1}, //channel 8
    {1,0,0,1}, //channel 9
    {0,1,0,1}, //channel 10
    {1,1,0,1}, //channel 11
    {0,0,1,1}, //channel 12
    {1,0,1,1}, //channel 13
    {0,1,1,1}, //channel 14
    {1,1,1,1}  //channel 15
  };

  //loop through the 4 sig
  for(int i = 0; i < 4; i ++){
    digitalWrite(controlPin[i], muxChannel[channel][i]);
  }

  //read the value at the SIG pin
  int val = analogRead(SIG_pin);

  //return the value
  return val;
}


* Screen shot 2011-04-18 at 12.47.11 PM.png (24.85 KB, 575x487 - viewed 5 times.)
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i think the issue is that arduino language converts serial data to integer values between 0 and 1023.
No, it does not. Serial data is sent and received one byte at a time. There may be a conversion from decimal to string, depending on which method is used to output the data. There is no conversion on the way in, unless you perform it.
Logged

Pages: [1]   Go Up
Jump to: