MidiLibrary crapping out


I’ve got an optocoupler set up with Arduino and MidiLibrary and my keyboard. I’ve got a very basic setup using their sample code which just lights an LED on NoteOn and turns the LED off on NoteOff, but it seems very unreliable. Note on/note off is sometimes missed and after < 10 key presses the arduino just stops doing anything.

I’m at work right now so can’t post code, but I literally just took the sample code on the MidiLibrary example and replaced the blinkLED for PC (program change) with NoteOn, and NoteOff and digitalwrite(LED, HIGH) and (LED, LOW). Just wondered whether there was a known issue with the MidiLibrary code getting, for want of a better phrase, “clogged up” with midi messages or something.

but I’ll follow up with the code tonight.

Any ideas?

Could you also post a schematic, it might be something is wrong like a missing ground.

Hey, it's fine now, it turns out it was something to do with the MIDI output of my digital piano. Not sure what exactly but it works fine with the MIDI output of my synth.

Code for info (incorporated with some LCD display stuff):

// Just a test to show details of MIDI input on LCD display. 

// The updating of the LCD is pretty slow so eventually 
// I will just use the display as a menu type thing,
// but for now... why not show my debug info on it? :)

#include <MIDI.h>
#include <LiquidCrystal.h>
// Rows and columns of LCD
const int numRows = 2; 
const int numCols = 16;
// Pins used for LCD
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); 
// Init string to construct text to print to LCD
String text = "";
// variable for number of notes currently pressed
int notesOn = 0; 

// LED pin on Arduino board
#define LED 13               

void setup() {
  //Status LED set to output
  pinMode(LED, OUTPUT);
  // start LCD
  // Launch MIDI with default options
  // (input channel is default set to 1)

void loop() {
  if (MIDI.read()) {                    // Is there a MIDI message incoming ?
    switch(MIDI.getType()) {            // Get the type of the message we caught
    case NoteOn:
      text = "ON: ";
      text = text + (int) MIDI.getData1(); // cast to int so we don't get weird ascii characters
      text = text + " V:";
      text = text + (int) MIDI.getData2();
      notesOn = notesOn + 1;

    case NoteOff:  
      text = "OFF:";
      text = text + (int) MIDI.getData1();
      text = text + " V:";
      text = text + (int) MIDI.getData2();
      notesOn = notesOn - 1;

    lcd2_write((String) "Notes on: " + notesOn);

// Rough functions to replace entire line of LCD

void lcd1_write(String text) {
  lcd.print("                ");
void lcd2_write(String text) {
  lcd.print("                ");

I'm probably still doing something stupid :stuck_out_tongue: