How Do I Use This Multiplexing Library for Arduino with More Than One Button?

  arduino, arduino-ide, button, c++

I just have a small problem with this Arduino library: CD74HC4067. I am not sure how to use multiple buttons with this multiplexer library. I have an Arduino Mega 2560 and the CD4067BE [multiplexer]. The connections are fairly simple: just like this, but with the signal pins going to 2, 3, 4, 5, and 6: https://electronics.stackexchange.com/questions/278321/reducing-the-number-of-pins-needed-to-read-a-12-key-keypad-where-the-buttons-are.
CD4067BE datasheet.
Here is the code:

/*
   Controlling and looping through a CD74HC4067's channel outputs


   Connect the four control pins to any unused digital or analog pins.
   This example uses digital pins 4, 5, 6, and 7.

   Connect the common pin to any other available pin. This is the pin that will be
   shared between the 16 channels of the CD74HC4067. The 16 channels will inherit the
   capabilities of the common pin. For example, if it's connected to an analog pin,
   you will be able to use analogRead on each of the 16 channels.

*/

#include <CD74HC4067.h>

// s0 s1 s2 s3
CD74HC4067 my_mux(2, 3, 4, 5);  // create a new CD74HC4067 object with its four control pins

const byte g_common_pin = 6; // select a pin to share with the 16 channels of the CD74HC4067

const byte ledPin = 7;

void setup()
{
  Serial.begin(9600);
  pinMode(g_common_pin, INPUT_PULLUP); // set the initial mode of the common pin.
  // This can be changed in loop() for each channel.

  pinMode(ledPin, OUTPUT);

}

void loop()
{

  my_mux.channel(0);
 

  if (digitalRead(g_common_pin) == LOW) {

    digitalWrite(ledPin, HIGH);

  }

  else {

    digitalWrite(ledPin, LOW);

  }
}

Source: Windows Questions C++

LEAVE A COMMENT