Pages: [1]   Go Down
Author Topic: multiple multiplexing dont understand much  (Read 632 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 1
Posts: 71
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 71
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

here is the code ive been trying to work with its only for one 4051 but its a start
Code:
#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
« Last Edit: January 11, 2013, 09:04:33 am by michael040990 » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 597
Posts: 33292
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 597
Posts: 33292
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 71
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

#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?
 
« Last Edit: January 11, 2013, 09:04:05 am by michael040990 » Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 71
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

alright i think i fillally did it although i cant test it out since i dont have nay mux yet

Code:
#include <Debounce.h>
int mux_buttonPin[4] = {5,6,7,8};
Debounce debouncer0 = Debounce( 20 , mux_buttonPin[0] );
Debounce debouncer1 = Debounce( 20 , mux_buttonPin[1] );
Debounce debouncer2 = Debounce( 20 , mux_buttonPin[2] );
Debounce debouncer3 = Debounce( 20 , mux_buttonPin[3] );

   
int buttonState0 = LOW;
int lastButtonState0 = LOW;

int buttonState1 = LOW;
int lastButtonState1 = LOW;

int buttonState2 = LOW;
int lastButtonState2 = LOW;

int buttonState3 = LOW;
int lastButtonState3 = LOW;



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


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


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


int  digital_state3 [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_state0[i], INPUT); //mux0 inputs
    pinMode(digital_state1[i], INPUT); //mux1 inputs
    pinMode(digital_state2[i], INPUT); //mux2 inputs
    pinMode(digital_state3[i], INPUT); //mux3 inputs
  }

  pinMode(2, OUTPUT);    // s0
  pinMode(3, OUTPUT);    // s1
  pinMode(4, OUTPUT);    // s2
  pinMode(mux_buttonPin[0], INPUT);     // mux0 read pin //digital
pinMode(mux_buttonPin[1], INPUT);     // mux1 read pin //digital
pinMode(mux_buttonPin[2], INPUT);     // mux2 read pin //digital
pinMode(mux_buttonPin[3], INPUT);     // mux3 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
    debouncer0.update(); // Update the debouncer
    debouncer1.update(); // Update the debouncer
    debouncer2.update(); // Update the debouncer
    debouncer3.update(); // Update the debouncer
   
        digital_state0[count] = digitalRead(debouncer0.read()); //set mux0 input val for each digitalstate value
        digital_state1[count] = digitalRead(debouncer1.read()); //set mux1 input val for each digitalstate value
        digital_state2[count] = digitalRead(debouncer2.read()); //set mux2 input val for each digitalstate value
        digital_state3[count] = digitalRead(debouncer3.read()); //set mux3 input val for each digitalstate value
    //Serial.println(digital_state0[count]); // print digital_state0 for debug
  //Serial.println(digital_state1[count]); // print digital_state1 for debug
  //Serial.println(digital_state2[count]); // print digital_state2 for debug
  //Serial.println(digital_state3[count]); // print digital_state3 for debug

    buttonState0 = digital_state0[count];
   
    buttonState1 = digital_state1[count];
   
    buttonState2 = digital_state2[count];
   
    buttonState3 = digital_state3[count];
   
   
    //MUX0
    if (buttonState0 != lastButtonState0) {
      // if the state has changed, increment the counter
      if (buttonState0 == HIGH) {
        // if the current state is HIGH then the button
        // wend from off to on:
        Serial.print("MUX0 button ");
        Serial.print(count);
        Serial.println(" pressed");
      }
      else {
        // if the current state is LOW then the button
        // wend from on to off:
        Serial.print("MUX0 button ");
        Serial.print(count);
        Serial.println(" released");
      }
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState0 = buttonState0;
   
   
     //MUX1
    if (buttonState1 != lastButtonState1) {
      // if the state has changed, increment the counter
      if (buttonState1 == HIGH) {
        // if the current state is HIGH then the button
        // wend from off to on:
        Serial.print("MUX1 button ");
        Serial.print(count);
        Serial.println(" pressed");
      }
      else {
        // if the current state is LOW then the button
        // wend from on to off:
        Serial.print("MUX1 button ");
        Serial.print(count);
        Serial.println(" released");
      }
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState1 = buttonState1;
   
   
 //MUX2
    if (buttonState2 != lastButtonState2) {
      // if the state has changed, increment the counter
      if (buttonState2 == HIGH) {
        // if the current state is HIGH then the button
        // wend from off to on:
        Serial.print("MUX2 button ");
        Serial.print(count);
        Serial.println(" pressed");
      }
      else {
        // if the current state is LOW then the button
        // wend from on to off:
        Serial.print("MUX2 button ");
        Serial.print(count);
        Serial.println(" released");
      }
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState2 = buttonState2;

//MUX3   
    if (buttonState3 != lastButtonState3) {
      // if the state has changed, increment the counter
      if (buttonState3 == HIGH) {
        // if the current state is HIGH then the button
        // wend from off to on:
        Serial.print("MUX3 button ");
        Serial.print(count);
        Serial.println(" pressed");
      }
      else {
        // if the current state is LOW then the button
        // wend from on to off:
        Serial.print("MUX3 button ");
        Serial.print(count);
        Serial.println(" released");
      }
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState3 = buttonState3;
   
   
   
  }
  delay (10);  //delay 10 for read between values

}



i have implemented the arduino uno to read from all 8 pins on all 4 mux and output each variable to
digital_state0[] //array of 7 inputs from mux0
digital_state1[] //array of 7 inputs from mux1
digital_state2[] //array of 7 inputs from mux2
digital_state3[] //array of 7 inputs from mux3
and have all the inputs debounced at 20 microseconds each
and each debounced input has a button pressed and button released
and is displayed on serial if pressed or not for specific mux

as for wiring i have pins 3,4,5 as select pins and 5,6,7,8 as datain pins
with a small delay of 10 between each loop cycle

does this code look right? do you think there might be any lag when a button is pressed or released? and finally is there any way to simplify this code to be more compact and run faster to prevent delays

eventually instead of writing serial on and off it will send midi notes for a midi fighter
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 597
Posts: 33292
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It looks to me like you are hung up on denouncing, it is much simpler than this. Once you detect a change in a switch just delay for 20mS or so and that allows it to stabilise. Assuming of course you have nothing to do in those 20mS, most of the time you don't.

Anyway try it. Without a schematic of how you intend to wire this up it is impossible to say much about the code.
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 71
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it took some time but i drew up the scimatics can you tell me if i have the pull up resisters in the right place
http://i48.tinypic.com/qsthms.jpg




how would i go about such a delay and read tactic for debouncing?
Logged

Pages: [1]   Go Up
Jump to: