Mutiplexing several push buttons for MIDI contrller

Status
Not open for further replies.

DeMoeP

Member
I'm currently trying to do a code for a 4x4 matrix to do a launchpad and a MIDI controller in one console, I figured out the first half for my analog inputs (pots and sliders) but I can't seem to find the right logic for the matrix. I bought a CD74HC4067 mux to connect all my push buttons, when compiling and uploading my code to the Teensy 4.1 and opening FL Studio the notes react to the push button press but instead of sending one single note it sends bombs of the same note until I release the button. Right now my code only includes 4 buttons I want to figure out why it does this before adding the rest. If anyone is willing help or give tips with my logic I would very much appreciate it

const int channel = 0;

const int s0 = 1; //address pins
const int s1 = 2; //address pins
const int s2 = 3; //address pins
const int s3 = 4; //address pins

int b0 = 0; //button values
int b1 = 0; //button values
int b2 = 0; //button values
int b3 = 0; //button values

void setup() {

pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);

pinMode(5, INPUT_PULLUP);
}

void loop() {

digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
delayMicroseconds(50);
b0 = digitalRead(5);
if (b0 == LOW) {
usbMIDI.sendNoteOn(60, 99, 0);
}
////////////////////////////////////////////////////
digitalWrite(s0, HIGH);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
delayMicroseconds(50);
button1.update();
b1 = digitalRead(5);
if (b1 == LOW) {
usbMIDI.sendNoteOn(61, 99, 0);
}
////////////////////////////////////////////////////
digitalWrite(s0, LOW);
digitalWrite(s1, HIGH);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
delayMicroseconds(50);
button2.update();
b2 = digitalRead(5);
if (b2 == LOW) {
usbMIDI.sendNoteOn(62, 99, 0);
}
////////////////////////////////////////////////////
digitalWrite(s0, HIGH);
digitalWrite(s1, HIGH);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
delayMicroseconds(50);
button3.update();
b3 = digitalRead(5);
if (b3 == LOW) {
usbMIDI.sendNoteOn(63, 99, 0);
}
while (usbMIDI.read()) {

}
}
 

Attachments

  • Untitled document.txt
    1.6 KB · Views: 38
You have forgot the else case of
Code:
if (b0 == LOW) {
usbMIDI.sendNoteOn(60, 99, 0);
}
else
{
usbMIDI.sendNoteOff(60, 99, 0);
}

I would have used two cascaded 74hc165 as that don't need any complicated addressing code, and only require 3 pins (latch, clk, data) that also allow more than 16 buttons if required without needing any more pins from the teensy.

The 4067 however is more suited for reading many pots.
 
I had the else statement too but the same problem occurs, when then push button is pressed it sends multiple notes until I release it, the else would send multiple "off" statements. In the FL Studio message window it shows that there are MIDI signals being read by the program, for instance, 60 is C4 note so when it is not pressed the message window displays "C4 note off" over and over again
 
Ohh I forgot about that part, you need a toggled state functionality.
Example for b0
Code:
bc = digitalRead(5);
if (bc == LOW) {
    if (b0 == LOW) {
        b0 = HIGH;
        usbMIDI.sendNoteOn(60, 99, 0);
    }
}
else {
     if (b0 == HIGH) {
        b0 = LOW;
        usbMIDI.sendNoteOff(60, 99, 0);
    }
}

bc is a temporary storage for the digital read
 
Last edited:
OMG IT WORKS! thank you so much for the temporary storage tip, I get confused with some MIDI functions because I'm not used to coding with Arduino. Super helpful for future projects.
 
I'm currently trying to do a code for a 4x4 matrix

Sounds like the issue is solved. But if you run into more problems, maybe check out the Keypad library. Click File > Examples > Keypad to get started. It's very easy to use and works well.
 
Status
Not open for further replies.
Back
Top