Playing A Tune Via MIDI

Introduction

This project demonstrates the basic principle behind playing a tune over MIDI. It uses exactly the same setup as the previous MIDI project.

You Will Need

  • 1 x 220 Ohm Resistor
  • 1 x MIDI Jack
  • Jumper Wires
  • USB MIDI Adapter

Making The Circuit

Arduino Circuit

Programming The Arduino

int beats[] = {1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2 };
int tunelength = 14;
int tune[] = {0x3C, 0x3C, 0x43, 0x43, 0x45, 0x45, 0x43, 0x41, 0x41, 0x40, 0x40, 0x3E, 0x3E, 0x3C};
int tempo = 300;

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

void loop() {
  for (int i =0;i<tunelength;i++)
  {
    noteOn(0x90, tune[i], 0x45);
    delay(beats[i]*tempo);
    noteOn(0x90, tune[i], 0x00);      
    delay(tempo/10);
  }
  while(true){
  }
}

void noteOn(int cmd, int pitch, int velocity) {
  Serial.write(cmd);
  Serial.write(pitch);
  Serial.write(velocity);
}

If you compare this to the code used for playing a tune on a buzzer, you can see that this is very similar. The only thing not catered for is a rest. Look at the buzzer version and you should be able to work out how to do that.

MIDI Note Table

Musical NoteHex Value
C(-1)00
C#(-1)01
D(-1)02
D#(-1)03
E(-1)04
F(-1)05
F#(-1)06
G(-1)07
G#(-1)08
A(-1)09
A#(-1)0A
B(-1)0B
C00C
C#00D
D00E
D#00F
E010
F011
F#012
G013
G#014
A015
A#016
B017
C118
C#119
D11A
D#11B
E11C
F11D
F#11E
G11F
G#120
A121
A#122
B123
C224
C#225
D226
D#227
E228
F229
F#22A
G22B
G#22C
A22D
A#22E
B22F
C330
C#331
D332
D#333
E334
F335
F#336
G337
G#338
A339
A#33A
B33B
C43C
C#43D
D43E
D#43F
E440
F441
F#442
G443
G#444
A445
A#446
B447
C548
C#549
D54A
D#54B
E54C
F54D
F#54E
G54F
G#550
A551
A#552
B553
C654
C#655
D656
D#657
E658
F659
F#65A
G65B
G#65C
A65D
A#65E
B65F
C660
C#761
D762
D#763
E764
F765
F#766
G767
G#768
A769
A#76A
B76B
C86C
C#86D
D86E
D#86F
E870
F871
F#872
G873
G#874
A875
A#876
B877
C978
C#979
D97A
D#97B
E97C
F97D
F#97E
G97F