Arduino mega USB MIDI with loopMidi and S2Midi

Hey guys! This is my first post, i doesnt really know anything about the rules here, so im sorry if i cross any of them. I have an old keyboard, and i want to modify it to send midi to my pc via usb. I heard about flashing the firmware on the arduino, but thats not an option for me since i want to use it for many other projects, and i dont want to do flash it every time. Heres my code:

#define LED 13    // LED pin on Arduino board
#define switch1 2              // 1st Switch
#define MIDI_COMMAND_NOTE_ON B10010000
#define MIDI_COMMAND_NOTE_OFF B10000000

int switch1LastState = 0;
int switch1CurrentState = 0;

// the format of the message to send Via serial 
typedef union {
    struct {
 uint8_t command;
 uint8_t channel;
 uint8_t data2;
 //uint8_t data3;
    } msg;
    uint8_t raw[3];
} t_midiMsg;
void setup() {
  pinMode(LED, OUTPUT);

void loop() {
t_midiMsg midiMsg1;  // MIDI message for Switch 1
switch1CurrentState = digitalRead(switch1);
if (switch1CurrentState == 1){
    if(switch1LastState == 0){
           midiMsg1.msg.command = MIDI_COMMAND_NOTE_ON ;
  = 0x3D;
           midiMsg1.msg.data2   = 0x79;
           //midiMsg1.msg.data3   = 0x30;
           //Serial.write(midiMsg1.raw, sizeof(midiMsg1)); 
switch1LastState = 1;               
if (switch1CurrentState == 0){
    if(switch1LastState == 1){
           midiMsg1.msg.command = MIDI_COMMAND_NOTE_OFF;
  = 0x10;
           midiMsg1.msg.data2   = 0x20;
           //midiMsg1.msg.data3   = 0x30;
           Serial.write(midiMsg1.raw, sizeof(midiMsg1));  
switch1LastState = 0;      


I started this tutorial : His t_midiusb union uses 4 bytes, and it works, but as i checked with midi-ox hes wrong with the names, data3 is lost in the communication, and only the first 3 bytes is used. I readd various descriptions on midi, and all of them said that the standard midi messege is 3 bytes long.Like this one So i deleted the 4th byte, and now it doesnt works and i dont know why. But thats not my biggest problem. The problem is, that the note off doesnt shows up, however the arduino sends the right messege for example 80 10 20 , or 80 10 20 30, but theres no note off in midi-ox, and i dont know why. Is there anyone who can help me? Sorry for my bad english.

I have a simple solution: If i want a note off, then i send a midi messege with 0 velocity. It works, but its ugly. Anyone with any idea how to use the note off?

According to page 16, Midi in USB messages are always four bytes with unused bytes padded with zeroes.


Why do you define your MIDI message like this ? struct { uint8_t command; uint8_t channel; uint8_t data2; }

You can see here that a MIDI Message has a Status, and 1 or 2 data values :

So you should define it like this :

struct { uint8_t status; uint8_t data1; uint8_t data2; }

The channel is inside "status" value. For example, if you want to send a NoteOn message on channel 1, you send : status = 0b10010001 data1 = pitch data2 = velocity

a noteOff : status = 0b10000001 data1 = pitch data2 = velocity

But the best should be to use the MIDI.h library, you wouldn't loose your time.