Button press output errors - assistance needed

Dave71

Member
Hi. Ive got a Teensy 4.0 setup to send midi notes via usb to a piece of software but I'm getting errors in the notes being sent. I have 12 momentary buttons wired up using the keypad library and have tested the setup using a simple serial.print command for each button. I've found some issues with button 7 output. Button 7 is setup to print Btn7 and send midi note 6 with velocity of 127 on channel 1 but i get inconsistent data being sent.

As far as far as i can tell, the issue is related just to button 7. Suspected a wiring issue but that should create a constant fault? I'll work on a schematic to show the wiring as soon a possible for clarification

Here's the code for button 7 - complete code is below

Thanks...Dave

if (customKey == '7') {
usbMIDI.sendNoteOn(6, 127, 1);
delay(50);
usbMIDI.sendNoteOff(6, 127, 1);
Serial.println("Btn7");
}

Serial monitor is picking this up from numerous presses of button 7 - all the other buttons appear to be ok
Btn7
Btn1
Btn1
Btn7
Btn7
Btn1
Btn7
Btn1
Btn1


#include <Keypad.h>
#include <MIDI.h>


const byte ROWS =2;
const byte COLS =6;

char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', '4', '5', '6'},
{'7', '8', '9', 'A', 'B', 'C'},
};

byte rowPins[ROWS] = {1, 2,};
byte colPins[COLS] = {3, 4 ,5, 6, 7, 8};

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup(){
Serial.begin(9600);
pinMode(14, OUTPUT);
}


void loop()

{
digitalWrite(14, HIGH);


{
char customKey = customKeypad.getKey();

if (customKey == '1') {
usbMIDI.sendNoteOn(0, 127, 1);
delay(50);
usbMIDI.sendNoteOff(0, 127, 1);
Serial.println("Btn1");
}

if (customKey == '2') {
usbMIDI.sendNoteOn(1, 127, 1);
delay(50);
usbMIDI.sendNoteOff(1, 127, 1);
Serial.println("Btn2");
}

if (customKey == '3') {
usbMIDI.sendNoteOn(2, 127, 1);
delay(50);
usbMIDI.sendNoteOff(2, 127, 1);
Serial.println("Btn3");
}

if (customKey == '4') {
usbMIDI.sendNoteOn(3, 127, 1);
delay(50);
usbMIDI.sendNoteOff(3, 127, 1);
Serial.println("Btn4");
}

if (customKey == '5') {
usbMIDI.sendNoteOn(4, 127, 1);
delay(50);
usbMIDI.sendNoteOff(4, 127, 1);
Serial.println("Btn5");


}

if (customKey == '6') {
usbMIDI.sendNoteOn(5, 127, 1);
delay(50);
usbMIDI.sendNoteOff(5, 127, 1);
Serial.println("Btn6");
}

if (customKey == '7') {
usbMIDI.sendNoteOn(6, 127, 1);
delay(50);
usbMIDI.sendNoteOff(6, 127, 1);
Serial.println("Btn7");
}

if (customKey == '8') {
usbMIDI.sendNoteOn(7, 127, 1);
delay(50);
usbMIDI.sendNoteOff(7, 127, 1);
Serial.println("Btn8");
}

if (customKey == '9') {
usbMIDI.sendNoteOn(8, 127, 1);
delay(50);
usbMIDI.sendNoteOff(8, 127, 1);
Serial.println("Btn9");
}

if (customKey == 'A') {
usbMIDI.sendNoteOn(9, 127, 1);
delay(50);
usbMIDI.sendNoteOff(9, 127, 1);
Serial.println("Btn10");
}

if (customKey == 'B') {
usbMIDI.sendNoteOn(10, 127, 1);
delay(50);
usbMIDI.sendNoteOff(10, 127, 1);
Serial.println("Btn11");
}

if (customKey == 'C') {
usbMIDI.sendNoteOn(11, 127, 1);
delay(50);
usbMIDI.sendNoteOff(11, 127, 1);
Serial.println("Btn 12");
}
}
}
 
Sounds like you need to add the debounce function to setup. From the keypad library.


Code:
// Minimum debounceTime is 1 mS. Any lower *will* slow down the loop().
void Keypad::setDebounceTime(uint debounce) {
    debounce<1 ? debounceTime=1 : debounceTime=debounce;
}
 
Thanks for the reply. Was my understanding a debouce was already built into the library but will add that anyway.
main issue is why I’m getting random button 1 press when I hit button 7
 
Try setting Tools > CPU Speed for slower, like 150 MHz or even 24 MHz. Quick and easy way to test if running too quickly for signals to stabilize.
 
Great, thanks for that. Seems to have cured the issue. 50+ button pushes later and I’ve got 100% success rate

thanks again!
 
Back
Top