Go Down

Topic: Help with Mayhew Labs Mux Shield (Read 1 time) previous topic - next topic

SquishyFish

Feb 02, 2013, 07:41 pm Last Edit: Feb 02, 2013, 07:46 pm by SquishyFish Reason: 1
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: [Select]

#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?

robtillaart


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: [Select]

#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 !
}
Rob Tillaart

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

SquishyFish

#2
Feb 02, 2013, 09:05 pm Last Edit: Feb 03, 2013, 12:09 am by SquishyFish Reason: 1
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: [Select]
[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: [Select]
[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: [Select]
#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.

robtillaart

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

Additional you can extract the pin selection code, see below
Code: [Select]

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 -
Rob Tillaart

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

SquishyFish

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

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy