I've been working on this for hours how and nothing seems to work.
I have a
Mayhew Labs Mux Sheild with 16 push buttons on the first Mux. I'm trying to debounce the input, but nothing I've tried seems to work. Here is the code I have:
#define CONTROL0 5
#define CONTROL1 4
#define CONTROL2 3
#define CONTROL3 2
int buttonState[16];
int buttonStatePrev[16];
void setup()
{
for(int i=0; i < sizeof(buttonState); i++){
buttonState[i] = 0;
buttonStatePrev[i] = 0;
}
pinMode(CONTROL0, OUTPUT);
pinMode(CONTROL1, OUTPUT);
pinMode(CONTROL2, OUTPUT);
pinMode(CONTROL3, OUTPUT);
MIDI.begin(4);
//Serial.begin(9600);
Serial.begin(115200);
pinMode(14, INPUT);
pinMode(15, OUTPUT);
pinMode(16, INPUT);
digitalWrite(14, HIGH);
digitalWrite(15, LOW);
//digitalWrite(16, HIGH);
}
void loop{
for (int i=0; i < 16; i++)
{
buttonStatePrev[i] = buttonState[i];
buttonState[i] = digitalReadMUX(i);
//Serial.println(buttonState[i]);
if (buttonState[i] == 1 && buttonStatePrev[i] == 0) {
MIDI.sendNoteOn(58-i,127,1);
} else if(buttonState[i] == 0 && buttonStatePrev[i] == 1){
MIDI.sendNoteOff(58-i,0,1);
}
}
}
void selectMUXPin(int pin)
{
digitalWrite(CONTROL0, pin & 8);
digitalWrite(CONTROL1, pin & 4);
digitalWrite(CONTROL2, pin & 2);
digitalWrite(CONTROL3, pin & 1);
}
int digitalReadMUX(int pin)
{
selectMUXPin(pin);
return (!digitalRead(14));
}
I tried importing the Bounce library, declaring a bounce object on pin 14 (the pin of the first mux) and in digitalReadMux return the bounce.read(), but for some reason that just completely destroys the logic - only the button on pin M01 pin 1 seems to be read (the rest do nothing) and the resulting MIDI message consists of 16x noteOn messages followed by 16 noteOff messages.
Can anyone help me out here? I've been at this for hours now, and I'm making absolutely no progress.