Error Reading Multiplexer Pins' State

Good Afternoon,
Can you help me figure out why this code is not behaving properly? The goal is to have the Arduino check all 16 pins on a multiplexer, and when the state of a button has changed, send a corresponding midi on/off note depending on whether the button was just pressed or released.

I have the MIDI circuit built and hooked up to MIDI OX to monitor the signals. If I wrote this correctly the code should exit if no buttons have changed, right? Instead what’s happening is the Arduino is alternately sending all 16 noteON signals, followed by all 16 noteOFF signals. It’s also showing no response to button presses. What am I missing?

Side note: is “int a” redundant in this sketch? All the a’s were initially i’s, I blindly tried changing that to see if it affected anything.

Disclaimer: I am 100% a coding novice. I’ve spent the last couple of months trying to teach myself by reading tutorials and reference pages, watching videos, and assembling my own codes by copy/pasting sections other people’s examples. If you ask me why I did something a certain way, the answer is likely “because I have no idea what I’m doing.” :-[ :wink:

Thank you kindly!

#include <Arduino.h>
#include <MIDI.h>

const int s0 = 8;
const int s1 = 7;
const int s2 = 6;
const int s3 = 5;

int sig_pin = 9;
int i = 0;
int a = 0;
int bts[16];
boolean btgs[16];

void setup() {

  for (int i = 0; i < 16; i++) btgs[i] = false;

  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
  pinMode(sig_pin, INPUT_PULLUP);

  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);

  Serial.begin(31250);

}

void noteOn(int cmd, int pitch, int velocity) {
  Serial.write(cmd);
  Serial.write(pitch);
  Serial.write(velocity);


}

void loop() {

  for (int i = 0; i < 16; i++)
    for (int a = 0; a < 16; a++)
    {
      //The following 4 commands set the correct logic for the control pins to select the desired input
      //See the Arduino Bitwise AND Reference: http://www.arduino.cc/en/Reference/BitwiseAnd
      //See the Aruino Bitshift Reference: http://www.arduino.cc/en/Reference/Bitshift
      digitalWrite(s0, (i & 15) >> 3);
      digitalWrite(s1, (i & 7) >> 2);
      digitalWrite(s2, (i & 3) >> 1);
      digitalWrite(s3, (i & 1));

      if (!btgs[a]) {
        if (digitalRead(sig_pin) == LOW) {
          noteOn(0x9F, 0 + a, 0x7f); //send corresponding note
          btgs[a] = true;
        }
      }
      else {
        if (digitalRead (sig_pin) == HIGH) {
          noteOn(0x9F, 0 + a, 0x00); //stop corresponding note
          btgs[a] = false;
        }
      }
    }






  delay(1000);
}

This part does not look correct:

      //The following 4 commands set the correct logic for the control pins to select the desired input
      //See the Arduino Bitwise AND Reference: http://www.arduino.cc/en/Reference/BitwiseAnd
      //See the Aruino Bitshift Reference: http://www.arduino.cc/en/Reference/Bitshift
      digitalWrite(s0, (i & 15) >> 3);
      digitalWrite(s1, (i & 7) >> 2);
      digitalWrite(s2, (i & 3) >> 1);
      digitalWrite(s3, (i & 1));

Try:

      //The following 4 commands set the correct logic for the control pins to select the desired input
      //See the Arduino Bitwise AND Reference: http://www.arduino.cc/en/Reference/BitwiseAnd
      //See the Aruino Bitshift Reference: http://www.arduino.cc/en/Reference/Bitshift
      digitalWrite(s0, bitRead(i, 3));
      digitalWrite(s1, bitRead(i, 2));
      digitalWrite(s2, bitRead(i, 1));
      digitalWrite(s3, bitRead(i, 0));
  for (int i = 0; i < 16; i++)
    for (int a = 0; a < 16; a++)
    {

This looks wrong. Why check all 16 buttons agains each of 16 possible inputs? I think you want:

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