0

My objective is to light a led on a strip corresponding to a keyboard note. I have Arduino Uno with MIDI in module hooked up to a serial port. Piano is hooked with a standart midi cable, Led Strip is powered using Arduino's 5v (Figured i don't need more because Max 10 leds will be active at a time).

My setup works but not accurately. All is fine when only 1 note at a time is played, but the denser the music becomes (faster or more notes are played simultaniously) more inaccuracies show - more leds don't turn off or on, or the notes I didn't play light up.

At first i used software serial and there were even more mistakes, when i switched to hardware it became better. Could it be that Arduino UNO is just not powerfull enough and cannot keep up with incoming midi input or is there another problem?

Here is the code:

#include <MIDI.h>
#include <Adafruit_NeoPixel.h>

MIDI_CREATE_DEFAULT_INSTANCE();

#define STRIP_PIN 11
#define STRIP_NUM_PIXELS 88

Adafruit_NeoPixel strip = Adafruit_NeoPixel(STRIP_NUM_PIXELS, STRIP_PIN, NEO_GRB + NEO_KHZ800);

void handleNoteOn(byte channel, byte pitch, byte velocity)
{
  strip.setPixelColor(pitch - 21, strip.Color(50, 50, 50));
  strip.show();
}

void handleNoteOff(byte channel, byte pitch, byte velocity)
{
  strip.setPixelColor(pitch - 21, strip.Color(0, 0, 0));
  strip.show();
}

// -----------------------------------------------------------------------------

void setup()
{ 
    MIDI.setHandleNoteOn(handleNoteOn);
    MIDI.setHandleNoteOff(handleNoteOff);
    MIDI.begin(MIDI_CHANNEL_OMNI);
    strip.begin();
}

void loop()
{
    MIDI.read();  
}
  • I would collect events until a gap in stream of note events and show them at once – Juraj Mar 16 at 14:38
  • Missing notes or releases could be due to timing but the notifications of notes you didn't play is more worrying. Try removing the neopixel stuff and just recording the notes - does the midi stuff work reliably on its own? (You might have to think of some clever way to get the data out since you're using the serial port.) – Mark Smith Mar 16 at 20:33
  • Yeah i dont really know how to properly debug. I think the notes that i didnt play are because midi packet was read incorrectly so some other parametres were read as notes somehow. When i tested through SoftwareSerial everything seemed to work fine on the input when i just read midi data. – Alex G Mar 16 at 20:40
  • Also consider the problem is not inside your sketch, but inside your circuit; First try to just print the values, and check if all MIDI on/off messages are received. If you are sure that the circuit (and sketch regarding MIDI input is ok), add the LED strip code again. – Michel Keijzers Mar 17 at 0:13
0

The biggest problem is that updating the LEDs is an intensive task.

  • Hand-crafted assembly is written to get the timing right
  • Interrupts are disabled while the update is running
  • 88 LEDs take a (relatively) long time to update.

At about 1.2µS per LED plus 50µS for the reset pulse, you're looking at about 155µS that the Arduino can't be doing anything else at all. That includes receiving anything through serial. And since you update for every note you receive, if there is a second note coming in with 155µS of that one, it will be missed.

You may get better results with the FastLED library instead of the Adafruit Neopixel library, but ideally you want to work with a board that is fast enough to not need to do such tight bit-banging for the protocol, so that interrupts can still be enabled for receiving data.

  • Thanks for this knowledge! I need it to work perfectly without missing a beat... do you think Arduino DUE will be up for this task? – Alex G Mar 18 at 20:03
  • Only if the LED library you use allows it to work without disabling interrupts. The FastLED library might have a DMA mode that operates completely asynchronously, but I don't know if it works on the Due. Some more research would be needed. – Majenko Mar 18 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.