Go Down

Topic: Hairless MIDI <-> Serial Bridge shows multiple NoteOn / NoteOff commands (fixed) (Read 179 times) previous topic - next topic

marcin_rajski

I wrote a simple code for DIY MIDI controller. When using Serial Monitor I see, that buttons are programmed correctly - seems like there's no debouncing. But when I launch Hairless MIDI and press one of the buttons, I get tons of NoteOn command, from which only one is with correct parameters. I'm attaching code and screenshots from Serial Monitor and Hairless MIDI after pressing a button ONCE.

I'm using this MIDI library: https://github.com/FortySevenEffects/arduino_midi_library/releases/tag/4.3.1




Code: [Select]

#include "MIDI.H"
boolean pressedButtons[8] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH}; // array with information which buttons are pressed

MIDI_CREATE_DEFAULT_INSTANCE();

void setup() {
  MIDI.begin(3); // Launch MIDI and listen to channel 3
  Serial.begin(115200);
  for (int i = 2; i <= 9; i++) {
    pinMode(i, INPUT_PULLUP); // initialize digital inputs, INPUT_PULLUP reverses button logic
    Serial.write("Button number "); // for debug purpose
    Serial.print(i-1);
    Serial.write(" initialized. \n");
  }
}

void loop() {
  for (int i = 0; i <= 7; i++) {
    if (digitalRead(i+2) == LOW && pressedButtons[i] == HIGH) { // if button is PRESSED and was previously DEPRESSED
      Serial.write("Button number "); // for debug purpose
      Serial.print(i+1);
      Serial.write(" pressed. \n");
      pressedButtons[i] = LOW; // update the array
      MIDI.sendNoteOn(42,127,1); // pitch 42, velocity 127, channel 1
    }
   if (digitalRead(i+2) == HIGH && pressedButtons[i] == LOW) { // if button is DEPRESSED and was previously PRESSED
      Serial.write("Button number "); // for debug purpose
      Serial.print(i+1);
      Serial.write(" depressed. \n");
      pressedButtons[i] = HIGH; // update the array
      MIDI.sendNoteOff(42,0,1); // pitch 42, velocity 127, channel 1
   }
  }
}

MarkT

Yes, you need to debounce the button yourself before generating MIDI messages.  That could be as simple as
delaying 10ms after sending the noteON or noteOFF messages, although delaying will create issues with responsiveness.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

marcin_rajski

I soleved it! I just had to remove Serial.write and Serial.print commands - they were contributing to those multiple MIDI commands ;)

So now, I get single NoteOn and NoteOff commands, but only, if I press button slowly. When I do it fast, some debouncing occurs, so I applied simple delay(); as you suggested.

I tag this topic as solved.

Go Up