2

This is the image of the schematic for the project I am working onI am working on a project with a claw machine. I am working on this chip (CD4067BE which is equivalent to the CD74HC4067) and I was wondering: can I read more than one button with the CD4067? I tried the following code, and it works great, but when I want to push two buttons, it will not read both of them as zero. Why am I using a chip with the buttons? It was to reduce I/O pins on my PCB for the claw machine. Yes, I could use a matrix, but I want control over my components, whether to turn it on or off. Also, the CD4067 includes all my buttons. I have thirteen buttons. (13 buttons). By the way, this chip is a multiplexer/demultiplexer. I am using this chip as a multiplexer. Oh, and also, I have an Arduino Mega 2560 rev3. I have followed the instructions of this question, but this doesn't answer if the chip can read more than button at one time. Thanks,

Austin

//Mux control pins
int s0 = 5;
int s1 = 4;
int s2 = 3;
int s3 = 2;


//Mux in "SIG" pin
int SIG_pin = 6;

void setup() {
  
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
  
  pinMode(SIG_pin, INPUT_PULLUP);

  Serial.begin(9600);
}

void loop() {

  //Loop through and read all 16 values
  for (int i = 0; i < 16; i ++) {
    Serial.print("Value at channel ");
    Serial.print(i); 
    Serial.print("is : ");
    Serial.println(readMux(i));
    delay(1000);
  }
}

int readMux(int channel) {
  int controlPin[] =       {s0, s1, s2, s3};
  int muxChannel[16][4] = { {0, 0, 0, 0},
                            {1, 0, 0, 0},
                            {0, 1, 0, 0},
                            {1, 1, 0, 0},
                            {0, 0, 1, 0},
                            {1, 0, 1, 0},
                            {0, 1, 1, 0},
                            {1, 1, 1, 0},
                            {0, 0, 0, 1},
                            {1, 0, 0, 1},
                            {0, 1, 0, 1},
                            {1, 1, 0, 1},
                            {0, 0, 1, 1},
                            {1, 0, 1, 1},
                            {0, 1, 1, 1},
                            {1, 1, 1, 1}
                          };
  //loop through the 4 sig
  for (int i = 0; i < 4; i ++) {
    digitalWrite(controlPin[i], muxChannel[channel][i]);
  }
  //read the value at the SIG pin
  int val = digitalRead(SIG_pin); //return the value
  return val;
}
17
  • 1
    There is no reason why it can't be used as you want. You're not reading two buttons at once, you're only ever reading one button, the other buttons are ignored. However you should have a pullup on every button, not just the Arduino's IO pin.
    – Majenko
    Jun 14 at 17:47
  • Why do the buttons need a pullup resistor? How do I go about reading more than one button at one time?
    – Austin
    Jun 14 at 18:02
  • 1
    So, this "floating", is it the same as "ghosting" from that tutorial?
    – Austin
    Jun 14 at 18:20
  • 1
    Yeah, I am not so sure what floating means and I still don't get the difference.
    – Austin
    Jun 15 at 0:20
  • 1
    I think you're over-estimating human perceptive ability, or under-estimating computer speed. Jun 15 at 10:00
0

An alternative way to write to the 4-bit MUX select pins is to use shifting and masking, something like this:

// MUX control pins
byte s0 = 2;
byte s1 = 3;
byte s2 = 4;
byte s3 = 5;
byte controlPin[] = { s0, s1, s2, s3 };

. . .

byte readMux(byte channel)
{
  // Loop through the 4 signals
  for (byte b = 0; b < 4; b++, channel >>= 1)
  {
    digitalWrite(controlPin[b], channel & 1);
  }
  // Read and return the value at the SIG pin
  return digitalRead(SIG_pin);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.