I tried to use the SoftwareSerial library for MIDI (by 47 effects), but it seems I get regularly corrupted/unexpected messages.
The circuit I'm using works perfectly when I connect it with the HardwareSerial solution (so the problem must lay in software).
What I see is:
- When I press notes and release notes, LED 13 goes on and off correctly.
- When I use Serial.print for debugging I get correct messages. However, in the example below I removed the print statements to have a minimal example).
- When I use SoftwareSerial, LED 13 goes on and off for every note correctly. However, when I send many messages (for example by aftertouch/pitch bend which send a lot of messages in a short time), I noticed Note On/Off commands with 'random' values are sent.
Some sources say SoftwareSerial should work for MIDI, however, so far it is far from perfect. Do I make some mistake?
(btw, the baudrate of MIDI is 31.250 bps, when I use pitchband several hundreds of bytes per second are sent, so far within the MIDI spec).
The sketch I use is:
#include <MIDI.h> // Add Midi Library
#include <SoftwareSerial.h>
SoftwareSerial swSerial(2, 11); // RX, TX
MIDI_CREATE_INSTANCE(SoftwareSerial, swSerial, midiSw1);
#define LED 13 // Arduino Board LED is on Pin 13
void setup()
{
pinMode (LED, OUTPUT); // Set Arduino board pin 13 to output
midiSw1.begin(MIDI_CHANNEL_OMNI);
midiSw1.setHandleNoteOn(MyHandleNoteOn);
midiSw1.setHandleNoteOff(MyHandleNoteOff);
}
void loop()
{
midiSw1.read();
}
void MyHandleNoteOn(byte channel, byte pitch, byte velocity)
{
digitalWrite(LED, HIGH); //Turn LED on
}
void MyHandleNoteOff(byte channel, byte pitch, byte velocity)
{
digitalWrite(LED, LOW); //Turn LED off
}