Try this code, it is a bit longer, but for a cut and paste, just to test your hardware, it will not matter.
I thought I had a hardware fault, but as it turned out, I entered this and found I didn't.
/*
MIDI note player
This sketch shows how to use the serial transmit pin (pin 1) to send MIDI note data.
If this circuit is connected to a MIDI synth, it will repeat
the chromatic scale from F#-0 (0x1E) to F#-5 (0x5A).
The circuit:
* digital 1 connected to Female MIDI socket pin 5 through 220-ohm resistor
* Female MIDI socket pin 2 + shield connected to ground
* Female MIDI socket pin 4 connected to +5V through 220-ohm resistor
Attach a MIDI cable to the Female Socket, then to a MIDI synth, and play music.
created 13 Jun 2006
modified 30 Aug 2011
by Tom Igoe
Modified by Rob Ward
23 March 2012
Tested on a Roland MT-32 (a classic!)
This example code is in the public domain
and developed from the code at:
http://www.arduino.cc/en/Tutorial/MIDI
**************************************************************************
Ardunio UNO MIDI OUT
.--------. 2
| | 220R .-------. 220R
| | ___ 5 / O \ 4 ___
| O1 |----|___|----|--O | O--|---|___|----- +5V
| | 3 | O | O | 1
| | \ | /
`--------' `---|---'
|
-----I---0V
NB Looking from the back of the female socket
**************************************************************************
For Ardunio Mega 2560 Serial2. can be substituted for Serial. and midi from
pin 16 ie TX2, then no cross talk between serial and midi IO.
*/
const int my_channel=1; // on Channel 1 :-)
const int my_instrument=1; // Acoustic Piano
int my_velocity=96; //max=127, min=0 volume
void setup() {
// Set MIDI baud rate:
Serial.begin(31250); //standard midi serial baud rate
delay (200);
//Establish channel and Instrument
midi_com(my_channel,my_instrument);
delay(200);
}
void loop() {
// play notes from F#-0 (0x1E) to F#-5 (0x5A):
for (int pitch = 0x1E; pitch < 0x5A; pitch ++) {
midi_note_on(my_channel, pitch, my_velocity);
delay(100);// sets duration of the note
//Turn off the note begun above
midi_note_off(my_channel, pitch, 0);
delay(100);// sets separation between notes
}
}
// Starts a MIDI note. Doesn't check to see that
// 0<=channel<=15, or that data values are <= 127
void midi_note_on(int channel, int pitch, int velocity) {
Serial.write(0x90+channel);
Serial.write(pitch);
Serial.write(velocity);
}
// Stops a MIDI note. Doesn't check to see that
// 0<=channel<=15, or that data values are <= 127
void midi_note_off(int channel, int pitch, int velocity) {
Serial.write(0x80+channel);
Serial.write(pitch);
Serial.write(velocity);
}
// Sends a midi command. Does not check if
// 0<=channel<=15 or 0<=instrument<=127 ie 1-128
void midi_com(int channel, int instrument) {
Serial2.write(0xC0+channel);
Serial2.write(instrument);
}
It is a little more long winded than official 1.0IDE example but the above worked for me when the IDE example did not. If you are serious about midi programming I can recommend the Mega2560 as the midi stream can be put on one of the other Serial ports very easily and the original Serial connection used for debug/control. It avoids all the weird characters appearing on the TTY and strange noises from the midi during download. ![]()
Cheers, Rob