Pages: [1]   Go Down
Author Topic: Help with Mayhew Labs Mux Shield  (Read 942 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've been googling high and low and I can't seem to figure this one out. How do you address specific pins with the mux shield?

I've tried this to no avail;

Code:
#define CONTROL0 5    
#define CONTROL1 4
#define CONTROL2 3
#define CONTROL3 2

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

int buttonPin = 0;
int buttonState = 0;

void setup()
{
  pinMode(CONTROL0, OUTPUT);
  pinMode(CONTROL1, OUTPUT);
  pinMode(CONTROL2, OUTPUT);
  pinMode(CONTROL3, OUTPUT);
  
  Serial.begin(9600);
  
  pinMode(14, INPUT);        
  pinMode(15, OUTPUT);
  pinMode(16, INPUT);
  
  
  digitalWrite(14, HIGH);      
  digitalWrite(15, HIGH);
  digitalWrite(16, HIGH);
}
  

void loop()
{
  //This for loop is used to scroll through and store the 16 inputs on the FIRST multiplexer
  for (int i=0; i<16; i++)
  {
    //The following 4 commands set the correct logic for the control pins to select the desired input
    //See the Arduino Bitwise AND Reference: http://www.arduino.cc/en/Reference/BitwiseAnd
    //See the Aruino Bitshift Reference: http://www.arduino.cc/en/Reference/Bitshift
    digitalWrite(CONTROL0, (i&15)>>3);
    digitalWrite(CONTROL1, (i&7)>>2);  
    digitalWrite(CONTROL2, (i&3)>>1);  
    digitalWrite(CONTROL3, (i&1));  

    buttonState = digitalRead(0);
    buttonState = digitalRead(0);
    if(buttonState == HIGH){
        Serial.println("HIGH");
    } else if (buttonState == LOW) {
        Serial.println("LOW");
    }
    mux0array[i] = !digitalRead(14);
  }
}

What I'm trying to do is read the state of buttonPin (which is pin 0 on M0), and simply print off whether its on or off.

This just prints off an endless line of "HIGH" in the serial monitor, and happens regardless of what pin I set as the button pin.

Can anyone please help?
« Last Edit: February 02, 2013, 01:46:35 pm by SquishyFish » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 221
Posts: 13848
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Do you have a link to the shield?

The shield has addressing lines to select the IO pin to use, that are the 4 control lines.

try this code
Code:
#define CONTROL0 5   
#define CONTROL1 4
#define CONTROL2 3
#define CONTROL3 2

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

int buttonPin = 0;
int buttonState = 0;

void setup()
{
  pinMode(CONTROL0, OUTPUT);
  pinMode(CONTROL1, OUTPUT);
  pinMode(CONTROL2, OUTPUT);
  pinMode(CONTROL3, OUTPUT);
 
  Serial.begin(9600);
 
  pinMode(14, INPUT);         
  pinMode(15, OUTPUT);
  pinMode(16, INPUT);
   
  digitalWrite(14, HIGH);       
  digitalWrite(15, HIGH);
  digitalWrite(16, HIGH);
}
 

void loop()
{
  Serial.print("MUX 0 = ");
  buttonState =  digitalReadMUX(buttonPin);
  if (buttonState) Serial.println("HIGH");
  else Serial.println("LOW");

  Serial.print("MUX 1 = ");
  buttonState =  digitalReadMUX(1);
  if (buttonState) Serial.println("HIGH");
  else Serial.println("LOW");

  delay(100); // just for the demo should be programmed like blink without delay of course ;)
}

int digitalReadMUX(int pin)
{
    digitalWrite(CONTROL0, pin & 8 );
    digitalWrite(CONTROL1, pin & 4 ); 
    digitalWrite(CONTROL2, pin & 2 ); 
    digitalWrite(CONTROL3, pin & 1 ); 

    return digitalRead(14);   // pin 14 is the arduino pin multiplexed // expression might need a !
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Excellent, thank you so much! That works perfectly. I think I understand how the mux shield works a bit better now, too.

I have another question though:

I will eventually have 16 inputs (and outputs). Would I be able to store the buttonState values in an array, and then iterate through the array and call the digitalReadMUX function on each value? Something like this:

Code:
[s]#define CONTROL0 5    
#define CONTROL1 4
#define CONTROL2 3
#define CONTROL3 2

int buttonPin1 = 0;
int buttonPin2 = 1;
int buttonPin3 = 2;

int buttonState[3];

void setup()
{
  for(int i=0; i<3; i++){
    buttonState[i] = 0;
  }
  pinMode(CONTROL0, OUTPUT);
  pinMode(CONTROL1, OUTPUT);
  pinMode(CONTROL2, OUTPUT);
  pinMode(CONTROL3, OUTPUT);
  
  Serial.begin(9600);
  
  pinMode(14, INPUT);        
  pinMode(15, OUTPUT);
  pinMode(16, INPUT);
    
  digitalWrite(14, HIGH);      
  digitalWrite(15, HIGH);
  digitalWrite(16, HIGH);
}
  

void loop()
{
  for (int i=0; i < buttonState.length; i++)
  {
    buttonState[i] = digitalReadMUX(i);
  }
  
  digitalWrite(15, LOW);
}

int digitalReadMUX(int pin)
{
    digitalWrite(CONTROL0, pin & 8 );
    digitalWrite(CONTROL1, pin & 4 );  
    digitalWrite(CONTROL2, pin & 2 );  
    digitalWrite(CONTROL3, pin & 1 );  

    return (!digitalRead(14));   // pin 14 is the arduino pin multiplexed // expression might need a !
}

int digitalWriteMUX (int pin){
    digitalWrite(CONTROL0, pin & 8);
    digitalWrite(CONTROL1, pin & 4);
    digitalWrite(CONTROL2, pin & 2);
    digitalWrite(CONTROL3, pin & 1);
    
    digitalWrite(15, HIGH);
    Serial.println("LED on");


}[/s]

The above code doesn't work. The working code below might help to illustrate what I mean:

Code:
[s]#define CONTROL0 5    
#define CONTROL1 4
#define CONTROL2 3
#define CONTROL3 2

int buttonPin1 = 0;
int buttonPin2 = 1;
int buttonPin3 = 2;

int ledPin1 = 0;
int ledPin2 = 1;
int ledPin3 = 2;

int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;

void setup()
{
  pinMode(CONTROL0, OUTPUT);
  pinMode(CONTROL1, OUTPUT);
  pinMode(CONTROL2, OUTPUT);
  pinMode(CONTROL3, OUTPUT);
  
  Serial.begin(9600);
  
  pinMode(14, INPUT);        
  pinMode(15, OUTPUT);
  pinMode(16, INPUT);
    
  digitalWrite(14, HIGH);      
  digitalWrite(15, HIGH);
  digitalWrite(16, HIGH);
}
  

void loop()
{  
  buttonState1 = digitalReadMUX(buttonPin1);
  buttonState2 = digitalReadMUX(buttonPin2);
  buttonState3 = digitalReadMUX(buttonPin3);
  
  if (buttonState1) digitalWriteMUX(0);
  
  if (buttonState2) digitalWriteMUX(1);
  
  if (buttonState3) digitalWriteMUX(2);
  
  digitalWrite(15, LOW);
}

int digitalReadMUX(int pin)
{
    digitalWrite(CONTROL0, pin & 8 );
    digitalWrite(CONTROL1, pin & 4 );  
    digitalWrite(CONTROL2, pin & 2 );  
    digitalWrite(CONTROL3, pin & 1 );  

    return (!digitalRead(14));   // pin 14 is the arduino pin multiplexed // expression might need a !
}

int digitalWriteMUX (int pin){
    digitalWrite(CONTROL0, pin & 8);
    digitalWrite(CONTROL1, pin & 4);
    digitalWrite(CONTROL2, pin & 2);
    digitalWrite(CONTROL3, pin & 1);
    
    digitalWrite(15, HIGH);
}[/s]

I actually got it working just a few moments after I posted that. Here's the working code for anyone who's interested:
Code:
#define CONTROL0 5    
#define CONTROL1 4
#define CONTROL2 3
#define CONTROL3 2

int buttonState[16];

void setup()
{
  for(int i=0; i<3; i++){
    buttonState[i] = 0;
  }
  pinMode(CONTROL0, OUTPUT);
  pinMode(CONTROL1, OUTPUT);
  pinMode(CONTROL2, OUTPUT);
  pinMode(CONTROL3, OUTPUT);
  
  Serial.begin(9600);
  
  pinMode(14, INPUT);        
  pinMode(15, OUTPUT);
  pinMode(16, INPUT);
    
  digitalWrite(14, HIGH);      
  digitalWrite(15, HIGH);
  digitalWrite(16, HIGH);
}
  

void loop()
{
  for (int i=0; i < sizeof(buttonState); i++)
  {
    buttonState[i] = digitalReadMUX(i);
  }
  
  for (int i=0; i < sizeof(buttonState); i++)
  {
    if (buttonState[i]) digitalWriteMUX(i);
  }
  
  digitalWrite(15, LOW);
}

int digitalReadMUX(int pin)
{
    digitalWrite(CONTROL0, pin & 8 );
    digitalWrite(CONTROL1, pin & 4 );  
    digitalWrite(CONTROL2, pin & 2 );  
    digitalWrite(CONTROL3, pin & 1 );  

    return (!digitalRead(14));   // pin 14 is the arduino pin multiplexed // expression might need a !
}

int digitalWriteMUX (int pin){
    digitalWrite(CONTROL0, pin & 8);
    digitalWrite(CONTROL1, pin & 4);
    digitalWrite(CONTROL2, pin & 2);
    digitalWrite(CONTROL3, pin & 1);
    
    digitalWrite(15, HIGH);
}

In summary: the above code reads the inputs of M0 and turns on a corresponding LED from M1.
« Last Edit: February 02, 2013, 06:09:19 pm by SquishyFish » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 221
Posts: 13848
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Think you should have a good look at your digitalWriteMUX() code, it misses something smiley-wink

Additional you can extract the pin selection code, see below
Code:
int digitalReadMUX(int pin)
{
    selectMUXPin(pin);
    return (!digitalRead(14));
}

int digitalWriteMUX(int pin, int value)
{
    selectMUXPin(pin);
    digitalWrite(15, value);
}

void selectMUXPin(int pin)
{
    digitalWrite(CONTROL0, pin & 8);
    digitalWrite(CONTROL1, pin & 4);
    digitalWrite(CONTROL2, pin & 2);
    digitalWrite(CONTROL3, pin & 1);
}
if you want you can replace the code in the selectMUXPin() with code to control a shift register that only uses 3 lines - http://arduino.cc/en/Tutorial/ShiftOut -
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the advice robtillaart - your code improved efficiency by a considerable amount! It runs much faster now smiley
Logged

Pages: [1]   Go Up
Jump to: