Advertisement
rinaldohack

Arduino Send MIDI

Mar 14th, 2019
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   MIDI note player
  3.  
  4.   This sketch shows how to use the serial transmit pin (pin 1) to send MIDI note data.
  5.   If this circuit is connected to a MIDI synth, it will play the notes
  6.   F#-0 (0x1E) to F#-5 (0x5A) in sequence.
  7.  
  8.   The circuit:
  9.   - digital in 1 connected to MIDI jack pin 5
  10.   - MIDI jack pin 2 connected to ground
  11.   - MIDI jack pin 4 connected to +5V through 220 ohm resistor
  12.   - Attach a MIDI cable to the jack, then to a MIDI synth, and play music.
  13.  
  14.   created 13 Jun 2006
  15.   modified 13 Aug 2012
  16.   by Tom Igoe
  17.  
  18.   This example code is in the public domain.
  19.  
  20.   http://www.arduino.cc/en/Tutorial/Midi
  21. */
  22.  
  23. void setup() {
  24.   // Set MIDI baud rate:
  25.   Serial.begin(31250);
  26.   pinMode(LED_BUILTIN, OUTPUT);
  27. }
  28.  
  29. void loop() {
  30.   // play notes from F#-0 (0x1E) to F#-5 (0x5A):
  31.   for (int note = 0x1E; note < 0x5A; note ++) {
  32.     //Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
  33.     noteOn(0x90, note, 0x45);
  34.     digitalWrite(LED_BUILTIN, HIGH);
  35.     delay(1000);
  36.     //Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
  37.     noteOn(0x90, note, 0x00);
  38.     digitalWrite(LED_BUILTIN, LOW);
  39.     delay(1000);
  40.   }
  41. }
  42.  
  43. // plays a MIDI note. Doesn't check to see that cmd is greater than 127, or that
  44. // data values are less than 127:
  45. void noteOn(int cmd, int pitch, int velocity) {
  46.   Serial.write(cmd);
  47.   Serial.write(pitch);
  48.   Serial.write(velocity);
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement