Sketch for MIDI in to control Tone Function

Hey folks.
I'm a complete newbie here but I was wondering if anyone had any advice on a sketch that takes a MIDI in and uses it to control the tone function for On, Off, and the frequency. This is the MIDI in sketch:

/*Receive Midi
By Amanda Ghassaei
July 2012

  • This program is free software; you can redistribute it and/or modify
  • it under the terms of the GNU General Public License as published by
  • the Free Software Foundation; either version 3 of the License, or
  • (at your option) any later version.

*/

byte commandByte;
byte noteByte;
byte velocityByte;

void setup(){
Serial.begin(31250);
}

void checkMIDI(){
do{
if (Serial.available()){
commandByte = Serial.read();//read first byte
noteByte = Serial.read();//read next byte
velocityByte = Serial.read();//read final byte
}
}
while (Serial.available() > 2);//when at least three bytes available
}

void loop(){
checkMIDI();
}

and I found this from a basic melody sketch:

/*************************************************

  • Public Constants
    *************************************************/

#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978

but I'm not sure how to combine them. I'm sure that there must be sketches like this that already exist somewhere but I can't seem to find any. Any feedback is greatly appreciated! Thank you!

Your checkMIDI function wont work.

To start with its a blocking function. As soon as some other MIDI event is sent that does not contain 3 bytes it will become out of sync or hang.

Then you need to take care of something called running status.
Note on/off are not always 3 bytes if the status byte is the same as the last one.

Also that long list of notes is useless, you need to be able to match those numbers to a MIDI note number. This means they need to be put in an array who's index is the MIDI note number or some simple function of it.
Possibly this is beyond what you can do yet, but keep working through the tutorials and IDE examples and you will get there.

Ah thanks for your help guys. I think I'm starting to have a better idea of what to google and what I need to learn how to understand. I just discovered this:

http://www.narbotic.com/projects/midivox/

The code for the shield I think is more along the lines of what I'm looking for. Its the sort of "if" command that I'm trying to figure out how to learn. What is the proper name of that so I can try to google a tutorial for that? From what I'm reading, thats what I'd have to write out....

Thank you!!

There are lots of projects that take in MIDI and do things like light LEDs. Just take that code and modify it to turn on and off the tone generator instead of the LED.
Here is one of mine:-
http://www.thebox.myzen.co.uk/Hardware/Glockenspiel.html
You need to learn about arrays.
http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

This is the UART irq handler for my dsp-G1 synthesizer.
Its written for the ARM cortex but show what you need to deal with.

void UART0_IRQHandler(void) {
      uint8_t MIDIRX;
	  while (!(LPC_USART0->STAT & UART_STATUS_TXRDY));
	  MIDIRX = LPC_USART0->RXDATA;

	  /*
	  Handling "Running status"
	  1.Buffer is cleared (ie, set to 0) at power up.
	  2.Buffer stores the status when a Voice Category Status (ie, 0x80 to 0xEF) is received.
	  3.Buffer is cleared when a System Common Category Status (ie, 0xF0 to 0xF7) is received.
	  4.Nothing is done to the buffer when a RealTime Category message is received.
	  5.Any data bytes are ignored when the buffer is 0.
	  */

	  if ((MIDIRX>0xBF)&&(MIDIRX<0xF8)) {
		  MIDIRUNNINGSTATUS=0;
		  MIDISTATE=0;
		  return;
	  }

	  if (MIDIRX>0xF7) return;

	  if (MIDIRX & 0x80) {
		  MIDIRUNNINGSTATUS=MIDIRX;
		  MIDISTATE=1;
		  return;
	  }

	  if (MIDIRX < 0x80) {
	  	  if (!MIDIRUNNINGSTATUS) return;
	  	  if (MIDISTATE==1) {
	  		  MIDINOTE=MIDIRX;
	  		  MIDISTATE++;
	  		  return;
	  	  }
	  	  if (MIDISTATE==2) {
	  		  MIDIVEL=MIDIRX;
	  		  MIDISTATE=1;
	  		  if ((MIDIRUNNINGSTATUS==0x80)||(MIDIRUNNINGSTATUS==0x90)) handleMIDINOTE(MIDIRUNNINGSTATUS,MIDINOTE,MIDIVEL);
	  		  if (MIDIRUNNINGSTATUS==0xB0) handleMIDICC(MIDINOTE,MIDIVEL);

	  		  return;
	  	  }
	  	  }

	  return;
}

This code handles just the note on/off and MIDI#CC messages but its the bare minimum you need to handle.