如何将 CD4067BE 库用于多个按钮

How to Use The CD4067BE Library for Multiple Buttons

我只是对这个 Arduino 库有一个小问题: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-areCD4067BE datasheet。 这是代码:

/*
   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);

  }
}

这就是我的问题的答案:我需要扫描各个频道以确定按下的是哪个频道。这是代码,以防有人感兴趣:

/*
   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(1);

    if (digitalRead(g_common_pin) == LOW) {

        digitalWrite(ledPin, HIGH);
        function();

      }

      else {

        digitalWrite(ledPin, LOW);

      }
  */

  for (int i = 0; i < 16; i++) {

    Serial.println(i);

    my_mux.channel(i);



    if (digitalRead(g_common_pin) == LOW) {

      digitalWrite(ledPin, HIGH);
      //function();

    }

    else {

      digitalWrite(ledPin, LOW);


    }

    delay(1000);

    /*
        }

        void function() {

        Serial.print("In function");

        }
    */
  }
}