Go Down

Topic: Mux Shield Analog In/Digital Out (Read 1 time) previous topic - next topic

etk1

Aug 02, 2011, 11:38 am Last Edit: Aug 02, 2011, 09:32 pm by etk1 Reason: 1
I'm trying to use a Mux Shield in a way that it would have M0 and M1 sending analog in data over serial, and having M2 print digital data received via serial.

I'm trying to get both to work simultaneously in MaxMSP, and am wondering if it is even possible to both send and received different streams of serial data at the same time; although I had figured that if should be doing that as long as I'm not using a write function in the Arduino code.  Here's my sketch so far as I've written it. Sorry if its a tad messy.

Thanks in advance!

http://mayhewlabs.com/products/arduino-mux-shield

Code: [Select]

//Attempting mux0 and mux1 analog in, and mux2 digital out
#define CONTROL0 5    
#define CONTROL1 4
#define CONTROL2 3
#define CONTROL3 2

int maxData;

int mux0array[16];
int mux1array[16];

int A;
int B;
int C;
int D;

void setup()
{
 
 Serial.begin(28800);

 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));    
   
   mux0array[i] = analogRead(0);
   Serial.print("mux0array: ");
   for (int i=0; i<16; i++)
 {
   Serial.print(mux0array[i]);
   Serial.print("-");
 }
   Serial.println();
 }
 
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));    
   mux1array[i] = analogRead(1);
   
   Serial.print("mux1array: ");
   for (int i=0; i<16; i++)
 {
   Serial.print(mux1array[i]);
   Serial.print("-");
 }
   Serial.println();
}
 
 
 {  
 pinMode(16, OUTPUT);
 }
  while(Serial.available() > 0) {
     maxData = Serial.read();
   }
    if (maxData == 0)
   {
   A = 2;
   B = 2;
   C = 2;
   D = 2;
   }
   if (maxData == 1)
   {
   A = 0;
   B = 0;
   C = 0;
   D = 0;
   }
   if (maxData == 3)
   {
   A = 1;
   B = 0;
   C = 0;
   D = 0;
   }
    if (maxData == 5)
   {
   A = 0;
   B = 1;
   C = 0;
   D = 0;
   }
   if (maxData == 7)
   {
   A = 1;
   B = 1;
   C = 0;
   D = 0;
   }
   
 {
   digitalWrite(16, HIGH);
 }
 for (int i=0; i<16; i++)
   digitalWrite(CONTROL0, (D)); //S3
   digitalWrite(CONTROL1, (C)); //S2
   digitalWrite(CONTROL2, (B)); //S1
   digitalWrite(CONTROL3, (A)); //S0
}


 
 

 

     
   

Grumpy_Mike

#1
Aug 02, 2011, 01:16 pm Last Edit: Aug 02, 2011, 09:56 pm by Grumpy_Mike Reason: 1

Also please supply a link to the shield you have so that we can see what it is supposed to do.

You can send and receive serial information at the same time.


Grumpy_Mike

Thanks for making the changes.

Not sure what the digital stuff is doing at the end but the way you had the analogue input bit going outputted all 16 values in the array every time you read in one value.
This will result in lots of printed output and once every 16 times it will be right.

Try this modification:-
Code: [Select]
//Attempting mux0 and mux1 analog in, and mux2 digital out
#define CONTROL0 5   
#define CONTROL1 4
#define CONTROL2 3
#define CONTROL3 2

int maxData;

int mux0array[16];
int mux1array[16];

int A;
int B;
int C;
int D;

void setup()
{
 
  Serial.begin(28800);

  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));     
   
    mux0array[i] = analogRead(0);
  }
  // now print out the array
      Serial.print("mux0array: ");
    for (int i=0; i<16; i++)
  {
    Serial.print(mux0array[i]);
    Serial.print("-");
  }
    Serial.println();

 
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));     
    mux1array[i] = analogRead(1);
}
    Serial.print("mux1array: ");
    for (int i=0; i<16; i++)
  {
    Serial.print(mux1array[i]);
    Serial.print("-");
  }
    Serial.println();
 
   
  pinMode(16, OUTPUT);
   while(Serial.available() > 0) {
      maxData = Serial.read();
    }
     if (maxData == 0)
    {
    A = 2;
    B = 2;
    C = 2;
    D = 2;
    }
    if (maxData == 1)
    {
    A = 0;
    B = 0;
    C = 0;
    D = 0;
    }
    if (maxData == 3)
    {
    A = 1;
    B = 0;
    C = 0;
    D = 0;
    }
     if (maxData == 5)
    {
    A = 0;
    B = 1;
    C = 0;
    D = 0;
    }
    if (maxData == 7)
    {
    A = 1;
    B = 1;
    C = 0;
    D = 0;
    }
   
  {
    digitalWrite(16, HIGH);
  }
  for (int i=0; i<16; i++)
    digitalWrite(CONTROL0, (D)); //S3
    digitalWrite(CONTROL1, (C)); //S2
    digitalWrite(CONTROL2, (B)); //S1
    digitalWrite(CONTROL3, (A)); //S0
}

etk1

Alright, thank you, that seems to be a bit more clean.  The digital stuff at the end was trying to output received serial data (from MaxMSP) out of the third multiplexer.  I had it working on its own perfectly okay (ill post the code) and the problem I was running into was getting both the 2 mux arrays analog in's whilst sending the 3rd mux digital out.

The maxData (four zero's) was being tested by if statements to produce a single led HIGH on my command from incoming Serial data

Truth table for the mux chips found here:

http://focus.ti.com/lit/ds/symlink/cd74hc4067.pdf

Code: [Select]



//Give convenient names to the control pins
#define CONTROL0 5   
#define CONTROL1 4
#define CONTROL2 3
#define CONTROL3 2

int maxData;

int A;
int B;
int C;
int D;

void setup()
{
 
  Serial.begin(28800);
  //Set MUX control pins to output
  pinMode(CONTROL0, OUTPUT);
  pinMode(CONTROL1, OUTPUT);
  pinMode(CONTROL2, OUTPUT);
  pinMode(CONTROL3, OUTPUT);
}
 

void loop()
{
 
  pinMode(16, OUTPUT);
{
    while(Serial.available() > 0) {
      maxData = Serial.read();
    }
     if (maxData == 0)
    {
    A = 2;
    B = 2;
    C = 2;
    D = 2;
    }
    if (maxData == 1)
    {
    A = 0;
    B = 0;
    C = 0;
    D = 0;
    }
    if (maxData == 3)
    {
    A = 1;
    B = 0;
    C = 0;
    D = 0;
    }
     if (maxData == 5)
    {
    A = 0;
    B = 1;
    C = 0;
    D = 0;
    }
    if (maxData == 7)
    {
    A = 1;
    B = 1;
    C = 0;
    D = 0;
    }
   
  {
    digitalWrite(16, HIGH);
  }

    digitalWrite(CONTROL0, (D)); //S3
    digitalWrite(CONTROL1, (C)); //S2
    digitalWrite(CONTROL2, (B)); //S1
    digitalWrite(CONTROL3, (A)); //S0
}
}
     
   



Go Up