Go Down

Topic: multiple multiplexing dont understand much (Read 734 times) previous topic - next topic

michael040990

hello all i am here to learn a little about the CD4051BE multiplexer tonight i have 4 coming in the mail and i would like to figure out how to use all 4 to read  the maximum amount of buttons as i can so i can make a midi fighter with tuns of buttons my question is how to i implement all 4 of them and how many buttons will they allow me to have i have check out the http://playground.arduino.cc/learning/4051 page but i don't quite understand and the wiring pictures are too small/blurry to read correctly and there is only reference code for one scimatic that i see and i'm not sure witch scimatic that is for.

as a side note i would like to know if its possible to read the buttons fast enough to make it seem that they are all reading at once and suppoort button pressed and button released and debounce for each button feature

thank you in advance for helping me understand how to use this multiplexer :)

michael040990

#1
Jan 11, 2013, 12:02 pm Last Edit: Jan 11, 2013, 03:04 pm by michael040990 Reason: 1
here is the code ive been trying to work with its only for one 4051 but its a start
Code: [Select]

#include <Debounce.h>
int mux_buttonPin = 5;
Debounce debouncer = Debounce( 20 , mux_buttonPin );
int  digital_state [7]  = {
 LOW};//set aside input values for mux

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;     // storeing 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
void setup(){
 for (int i=0; i<=7; i++) {
   pinMode(digital_state[i], INPUT); //mux inputs
 }

 pinMode(2, OUTPUT);    // s0
 pinMode(3, OUTPUT);    // s1
 pinMode(4, OUTPUT);    // s2
 pinMode(mux_buttonPin, INPUT);     // mux read pin //digital

 Serial.begin(9600);
}
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]); //print current bin
   debouncer.update ( ); // Update the debouncer
   digital_state[count] = digitalRead(debouncer.read); //set mux input val for each digitalstate value
   Serial.println(digital_state[count]); // print digital_state for debug


 }
 delay (10);   //delay 10 for read between values


}


im having a hard time implementing the button pressed and release

Grumpy_Mike

Quote
how many buttons will they allow me to have

It will be 8 per chip so for four chips that gives you 32 inputs.
This will "cost you" some inputs / outputs on the arduino.
One way to wire it up is to have the three select lines for all 4 chips wired in parallel and then the common from the chip to one of 4 separate arduino inputs.
You can wire it up like in this project:-
http://www.thebox.myzen.co.uk/Hardware/MIDI_Footsteps.html
That shows just two chips and uses the analogue inputs. If you have just push buttons you can use digital inputs instead of the analogue ones.

Grumpy_Mike

That code is for a matrix, this is a bit more complex to wire up. With 4 chips wired as a matrix you can have 16 x 16 = 256 switches but they take longer to read. I am sure you don't need that many.

michael040990

#4
Jan 11, 2013, 01:49 pm Last Edit: Jan 11, 2013, 03:04 pm by michael040990 Reason: 1
thank you for you input about were to look eventually i want to have 100 buttons but they need to be read fast  here is my code so far it includes the debounce and the button pressed and release for 1 mux chip letme know if this is correct i dont have the chips yet so this is all just in thearoy cant test my code yet
Code: [Select]


#include <Debounce.h>
int mux_buttonPin = 5;
Debounce debouncer = Debounce( 20 , mux_buttonPin );

int buttonState = LOW;
int lastButtonState = LOW;

int state = 0;


int  digital_state [7]  = {
 LOW};//set aside input values for mux

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;     // storeing 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
void setup(){
 for (int i=0; i<=7; i++) {
   pinMode(digital_state[i], INPUT); //mux inputs
 }

 pinMode(2, OUTPUT);    // s0
 pinMode(3, OUTPUT);    // s1
 pinMode(4, OUTPUT);    // s2
 pinMode(mux_buttonPin, INPUT);     // mux read pin //digital

 Serial.begin(9600);
}
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]); //print current bin
   debouncer.update(); // Update the debouncer
   digital_state[count] = digitalRead(debouncer.read()); //set mux input val for each digitalstate value
   //Serial.println(digital_state[count]); // print digital_state for debug

   buttonState = digital_state[count];
   if (buttonState != lastButtonState) {
     // if the state has changed, increment the counter
     if (buttonState == HIGH) {
       // if the current state is HIGH then the button
       // wend from off to on:
       Serial.print("button");
       Serial.print(count);
       Serial.println(" pressed");
     }
     else {
       // if the current state is LOW then the button
       // wend from on to off:
       Serial.print("button");
       Serial.print(count);
       Serial.println(" released");
     }
   }
   // save the current state as the last state,
   //for next time through the loop
   lastButtonState = buttonState;

 }
 delay (10);   //delay 10 for read between values

}




so basically to modify this code
pins 2, 3, 4 (select pins) will go to all 4 multiplexer select pins and i would add pins 5,6,7,8 as the read pins from the mux as stated in the code by the mux_buttonPin value ? then just read from 5,6,7,8 and append there value to there respective variables digital_state[]?

with this set up the arduino can take a maxamim of 9 mux chips for digital using pins 2-13
at 8 pins a mux thats 72 buttons i could use?

using a aditional 6 mux i could have 48 analog values by putting the extra 6 inputpins in pins a0 through a5 right?

Go Up