MIDI out... sending MIDI notes - the most easy way ?

Greetings,

I am all new to this Arduino programming, so what I'm looking for is the easiest program codes (almost like a "hello world" style....)

I need to connect an organ pedalboard to an Arduino MEGA to send MIDI notes to a computer (for a software : GrandOrgue)... the computer will then produce the sounds....

The pedalboard has 32 notes... and I have plenty of digital inputs on the Arduino MEGA not to use a matrix and for each not to have its own digital input connection....

There is no velocity involved (or full velocity if you prefer)... it's either note off or note on....

what kind of line code would I need to write so that : if I pressed C2 on pedalboard, the arduino send that note to the computer for as long as I press on it....
(I would repeat the same kind of code line for each of the 32 notes of the pedalboard....)

Many thanks!

With a MIDI library? Perhaps the Arduino MIDI library? Perhaps checkout its BasicIO example?

If you want serial midi (the kind that uses a DIN connector) you can use the Arduino MIDI Library.

However, if you have a board that supports it, you probably want to use USB - check out the MIDIUSB library. I have used something similar with Teensy USB Midi, it is very convenient!

You could use the Control Surface library:
NoteButton.ino
NoteButtonMatrix.ino

You can declare multiple buttons in an array:

[color=#434f54]// Instantiate an array of NoteButton objects[/color]
[b][color=#d35400]NoteButton[/color][/b] [color=#000000]buttons[/color][color=#000000][[/color][color=#000000]][/color] [color=#434f54]=[/color] [color=#000000]{[/color]
  [color=#000000]{[/color][color=#000000]5[/color][color=#434f54],[/color] [color=#d35400]note[/color][color=#000000]([/color][color=#00979c]C[/color][color=#434f54],[/color] [color=#000000]4[/color][color=#000000])[/color][color=#000000]}[/color][color=#434f54],[/color] [color=#434f54]// Push button on pin 5, Note C4 on MIDI channel 1[/color]
  [color=#000000]{[/color][color=#000000]6[/color][color=#434f54],[/color] [color=#d35400]note[/color][color=#000000]([/color][color=#00979c]Db[/color][color=#434f54],[/color] [color=#000000]4[/color][color=#000000])[/color][color=#000000]}[/color][color=#434f54],[/color] 
  [color=#000000]{[/color][color=#000000]7[/color][color=#434f54],[/color] [color=#d35400]note[/color][color=#000000]([/color][color=#00979c]D[/color][color=#434f54],[/color] [color=#000000]4[/color][color=#000000])[/color][color=#000000]}[/color][color=#434f54],[/color] 
  [color=#000000]{[/color][color=#000000]8[/color][color=#434f54],[/color] [color=#d35400]note[/color][color=#000000]([/color][color=#00979c]Eb[/color][color=#434f54],[/color] [color=#000000]4[/color][color=#000000])[/color][color=#000000]}[/color][color=#434f54],[/color] 
  [color=#000000]{[/color][color=#000000]9[/color][color=#434f54],[/color] [color=#d35400]note[/color][color=#000000]([/color][color=#00979c]E[/color][color=#434f54],[/color] [color=#000000]4[/color][color=#000000])[/color][color=#000000]}[/color][color=#434f54],[/color] 
  [color=#434f54]// ...[/color]
[color=#000000]}[/color][color=#000000];[/color]

The Arduino Mega doesn't have native MIDI over USB support, you can find more information here: MIDI over USB.

Pieter

Hello, i am new too in this great world.
I like for share my code, maybe help for your project.
This code is used on my midi keyboard and works fine. Maybe need change baudrate for your project and change pins for Arduino Mega.

For keyboard matrix, also need solder one diode for note, if not, can detect multiple keys when one key is pressed

I hope I can help you or that I can serve as an example

Cheers

//Use diodes is very important.
int notepin[7]={12,6,7,8,9,10,11}; 
int octavepin[8]={A2,A1,A0,13,2,3,4,5}; 

int note[56]={
  36,37,38,39,40,41,42,
  0 ,43,44,45,46,47,48,
  0 ,49,50,51,52,53,54,
  0 ,55,56,57,58,59,60,
  0 ,61,62,63,64,65,66,
  0 ,67,68,69,70,71,72,
  0 ,73,74,75,76,77,78,
  0 ,79,80,81,82,83,84};
  
int key[56]={};
int keyb[56]={};
int c=0;
int d=0;

void setup() {
  
  // Set pins for input and output
  for (int i=0;i<(sizeof(notepin)/sizeof(notepin[0])); i++){
    pinMode(notepin[i], INPUT);
  }
  for (int i=0;i<(sizeof(octavepin)/sizeof(octavepin[0]));i++){
    pinMode(octavepin[i],OUTPUT);
    digitalWrite(octavepin[i],LOW);
  }
  
  //Change baudrate if necesary
  Serial.begin(38400);

}

void loop() {
  readmatrix();
}

void comparator(){
  //Compare matrix keyboard value for each key, if key==1 send noteON. if not send noteOFF
  if (keyb[c] != key[c]){
    if (key[c]==1){
      sendmidi(0x90,note[c],127);
    }
    else{
      sendmidi(0x80,note[c],0);
    }
    keyb[c]=key[c];
  }
}

void sendmidi(int cmd,int note,int vel){
  Serial.write(cmd);
  Serial.write(note);
  Serial.write(vel);
}

void readmatrix(){
  c=0;
  for (int a=0;a<(sizeof(octavepin)/sizeof(octavepin[0]));a++){
    digitalWrite(octavepin[a],HIGH);
    for (int b=0;b<(sizeof(notepin)/sizeof(notepin[0]));b++){
      if (digitalRead(notepin[b]) == HIGH){
        key[c]=1;
      }
      else{
        key[c]=0;
      }
      comparator();
    c++;
    }
    digitalWrite(octavepin[a],LOW);
  }

}

Thanks everyone!!
as I was exploring your suggestions, I came across this YouTube video from Notes and Volts that worked out…

I will go with this for now… and get the project to a satisfactory completion… and will explore further after of other options or add-ons that I can incorporate to the project…

Again, Thanks!!