(edit sidebar)
Intro to Physical Computing Syllabus

Research & Learning

Other Class pages

Shop Admin

ITP Help Pages
Tom's pcomp site
DanO's pcomp site


MIDI Output using an Arduino

Labs.MIDIOutput History

Hide minor edits - Show changes to output

Deleted lines 67-68:
 (:div class=code :)
 
Added line 116:
 
Changed lines 118-121 from:
 
 
(:divend:)

to:
Deleted lines 124-125:

(:div class=code :)
Changed lines 216-218 from:
(:divend:)

to:
Added lines 68-70:
 (:div class=code :)
 
 #include <SoftwareSerial.h>
Changed lines 72-73 from:
#include <SoftwareSerial.h>
to:
 %color=#7e7e7e%// Variables:
  %color=#cc6600%byte%% note = 0;            %color=#7e7e7e%// The MIDI note value to be played
Changed lines 75-76 from:
 // Variables:
  byte note
= 0;            // The MIDI note value to be played
to:
 %color=#7e7e7e%//software serial
 SoftwareSerial midiSerial(2, 3); %color
=#7e7e7e%// digital pins that we'll use for soft serial RX & TX
Deleted lines 77-78:
 //software serial
 SoftwareSerial midiSerial(2, 3); // digital pins that we'll use for soft serial RX & TX
Added lines 80-85:
  %color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
    %color=#7e7e7e%//  Set MIDI baud rate:
    %color=#cc6600%'''Serial'''%%.%color=#cc6600%begin%%(9600);
      midiSerial.%color=#cc6600%begin%%(31250);
   
  }
Changed lines 87-97 from:
  void setup() {
    //  Set MIDI baud rate:
    Serial.begin
(9600);
      midiSerial.begin(31250);
    
  }

  void loop() {
    // play notes from F#-0 (30) to F#-5 (90):
    for (note = 30; note < 90; note ++) {
     
//Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
to:
  %color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
    %color=#7e7e7e%// play notes from F#-0 (30) to F#-5 (90):
    %color=#cc6600%for%% (note = 30; note < 90; note ++) {
     %color=#7e7e7e%//Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
Changed lines 92-93 from:
     delay(100);
      //Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
to:
     %color=#cc6600%delay%%(100);
      %color=#7e7e7e%//Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
Changed line 95 from:
     delay(100);
to:
     %color=#cc6600%delay%%(100);
Changed lines 99-104 from:
  //  plays a MIDI note.  Doesn't check to see that
  //  cmd is greater than 127, or that data values are  less than 127:
  void noteOn(byte cmd, byte data1, byte data2) {
    midiSerial.write(cmd);
    midiSerial.write(data1);
    midiSerial.write(data2);
to:
  %color=#7e7e7e%//  plays a MIDI note.  Doesn't check to see that
  %color=#7e7e7e%//  cmd is greater than 127, or that data values are  less than 127:
  %color=#cc6600%void%% noteOn(%color=#cc6600%byte%% cmd, %color=#cc6600%byte%% data1, %color=#cc6600%byte%% data2) {
    midiSerial.%color=#cc6600%write%%(cmd);
    midiSerial.%color=#cc6600%write%%(data1);
    midiSerial.%color=#cc6600%write%%(data2);
Changed lines 107-113 from:
     //prints the values in the serial monitor so we can see what note we're playing
    Serial.print("cmd: ");
    Serial.print(cmd);
      Serial.print(", data1: ");
  Serial.print(data1);
      Serial.print(", data2: ");
    Serial.println(data2);
to:
     %color=#7e7e7e%//prints the values in the serial monitor so we can see what note we're playing
    %color=#cc6600%'''Serial'''%%.%color=#cc6600%print%%(%color=#006699%"cmd: "%%);
    %color=#cc6600%'''Serial'''%%.%color=#cc6600%print%%(cmd);
      %color=#cc6600%'''Serial'''%%.%color=#cc6600%print%%(%color=#006699%", data1: "%%);
  %color=#cc6600%'''Serial'''%%.%color=#cc6600%print%%(data1);
      %color=#cc6600%'''Serial'''%%.%color=#cc6600%print%%(%color=#006699%", data2: "%%);
    %color=#cc6600%'''Serial'''%%.%color=#cc6600%println%%(data2);
Added line 118:
(:divend:)
Added line 120:
 
Changed lines 69-70 from:
 %color=#7e7e7e%// Variables:
 %color=#cc6600%byte%% note = 0;            %color=#7e7e7e%// The MIDI note value to be played
to:
#include <SoftwareSerial.h>
Changed lines 71-74 from:
 %color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
  %color=#7e7e7e%//  Set MIDI baud rate:
  %color=#cc6600%Serial%%.%color=#cc6600%begin%%(31250);
 }
to:
 // Variables:
  byte note
= 0;           // The MIDI note value to be played
Changed lines 74-84 from:
 %color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
  %color=#7e7e7e%
// play notes from F#-0 (30) to F#-5 (90):
 
  %color=#cc6600%for%% (note = 30; note < 90; note ++) {
    %color=#7e7e7e%//Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
    noteOn(0x90, note, 0x45);
    %color=#cc6600%delay%%(100);
    %color=#7e7e7e%//Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
    noteOn(0x90, note, 0x00); 
    %color=#cc6600%delay%%(100);
  }
 }
to:
 //software serial
 SoftwareSerial midiSerial(2, 3);
// digital pins that we'll use for soft serial RX & TX
Deleted lines 76-82:
 %color=#7e7e7e%//  plays a MIDI note.  Doesn't check to see that
 %color=#7e7e7e%//  cmd is greater than 127, or that data values are  less than 127:
 %color=#cc6600%void%% noteOn(%color=#cc6600%byte%% cmd, %color=#cc6600%byte%% data1, %color=#cc6600%byte%% data2) {
  %color=#cc6600%Serial%%.%color=#cc6600%write%%(cmd);
  %color=#cc6600%Serial%%.%color=#cc6600%write%%(data1);
  %color=#cc6600%Serial%%.%color=#cc6600%write%%(data2);
 }
Changed lines 79-90 from:
(:divend:)

!!! Allow a Person to Play Notes

The previous example will just play notes, no interactivity
.  The example below uses an analog input to set the pitch, and a digital input (a switch) to start and stop the note:

(:div class=code :)


(:div class=code :)
 
 #include <SoftwareSerial.h>
to:
  void setup() {
    //  Set MIDI baud rate:
    Serial
.begin(9600);
     midiSerial.begin(31250);
   
  }
Changed lines 86-87 from:
 %color=#cc6600%const%% %color=#cc6600%int%% switchPin = 10;  %color=#7e7e7e%// The switch is on Arduino pin 10
 %color=#cc6600%const%% %color=#cc6600%int%% LEDpin = 13;
    %color=#7e7e7e%//  Indicator LED
to:
  void loop() {
 
   // play notes from F#-0 (30) to F#-5 (90):
    for (note = 30; note < 90; note ++) {
      //Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
      noteOn(0x90, note, 0x45);
      delay(100);
      //Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
      noteOn(0x90, note, 0x00); 
      delay(100);
    }
  }
Changed lines 98-103 from:
  %color=#7e7e7e%// Variables:
  %color=#cc6600%byte%%
note = 0;             %color=#7e7e7e%// The MIDI note value to be played
  %color=#cc6600%int%% AnalogValue = 0;      %color=#7e7e7e%// value from the analog input
  %color=#cc6600%int%% lastNotePlayed = 0;    %color=#7e7e7e%// note turned on when you press the switch
  %color=#cc6600%int%% lastSwitchState = 0;    %color=#7e7e7e%// state of the switch during previous time through the main loop
  %color=#cc6600%int%% currentSwitchState = 0;
to:
  //  plays a MIDI note.  Doesn't check to see that
  //  cmd is greater than 127, or that data values are  less than 127:
  void noteOn(byte cmd, byte data1, byte data2) {
    midiSerial.write(cmd);
    midiSerial.write(data1);
 
  midiSerial.write(data2);
   
   
    //prints
the values in the serial monitor so we can see what note we're playing
    Serial.print("cmd: ");
 
  Serial.print(cmd);
 
     Serial.print(", data1: ");
  Serial.print(data1);
      Serial.print(", data2: ");
    Serial.println(data2);
   
   
  }
Deleted lines 116-117:
 %color=#7e7e7e%//software serial
 SoftwareSerial midiSerial(2, 3); %color=#7e7e7e%// digital pins that we'll use for soft serial RX & TX
Added lines 118-129:
(:divend:)

!!! Allow a Person to Play Notes

The previous example will just play notes, no interactivity.  The example below uses an analog input to set the pitch, and a digital input (a switch) to start and stop the note:

(:div class=code :)


(:div class=code :)
 
 #include <SoftwareSerial.h>
Changed lines 131-141 from:
  %color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
    %color=#7e7e7e%//  set the states of the I/O pins:
   
%color=#cc6600%pinMode%%(switchPin, %color=#006699%INPUT%%);
 
  %color=#cc6600%pinMode%%(LEDpin, %color=#006699%OUTPUT%%);
    %color=#7e7e7e%//  Set MIDI baud rate:
  %color=#cc6600%'''Serial'''%%.%color=#cc6600%begin%%(9600);
    %color=#cc6600%blink%%(3);
   
    midiSerial.%color=#cc6600%begin%%(31250);
   
  }
to:
 %color=#cc6600%const%% %color=#cc6600%int%% switchPin = 10;  %color=#7e7e7e%// The switch is on Arduino pin 10
 
%color=#cc6600%const%% %color=#cc6600%int%% LEDpin = 13;    %color=#7e7e7e%//  Indicator LED
Added lines 134-156:
  %color=#7e7e7e%// Variables:
  %color=#cc6600%byte%% note = 0;              %color=#7e7e7e%// The MIDI note value to be played
  %color=#cc6600%int%% AnalogValue = 0;        %color=#7e7e7e%// value from the analog input
  %color=#cc6600%int%% lastNotePlayed = 0;    %color=#7e7e7e%// note turned on when you press the switch
  %color=#cc6600%int%% lastSwitchState = 0;    %color=#7e7e7e%// state of the switch during previous time through the main loop
  %color=#cc6600%int%% currentSwitchState = 0;
 
 %color=#7e7e7e%//software serial
 SoftwareSerial midiSerial(2, 3); %color=#7e7e7e%// digital pins that we'll use for soft serial RX & TX
 
 
  %color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
    %color=#7e7e7e%//  set the states of the I/O pins:
    %color=#cc6600%pinMode%%(switchPin, %color=#006699%INPUT%%);
    %color=#cc6600%pinMode%%(LEDpin, %color=#006699%OUTPUT%%);
    %color=#7e7e7e%//  Set MIDI baud rate:
  %color=#cc6600%'''Serial'''%%.%color=#cc6600%begin%%(9600);
    %color=#cc6600%blink%%(3);
   
    midiSerial.%color=#cc6600%begin%%(31250);
   
  }
 
Changed lines 106-176 from:
(:source lang=c++ :)

//include the software serial library
#include <SoftwareSerial.h>

const int switchPin = 10;  // The switch is on Arduino pin 10
const int LEDpin = 13;    //  Indicator LED

 // Variables:
 byte note = 0;              // The MIDI note value to be played
 int AnalogValue = 0;        // value from the analog input
 int lastNotePlayed = 0;    // note turned on when you press the switch
 int lastSwitchState = 0;    // state of the switch during previous time through the main loop
 int currentSwitchState = 0;

//software serial
SoftwareSerial midiSerial(2, 3); // digital pins that we'll use for soft serial RX & TX


 void setup() {
  //  set the states of the I/O pins:
  pinMode(switchPin, INPUT);
  pinMode(LEDpin, OUTPUT);
  //  Set MIDI baud rate:
  Serial.begin(9600);
  blink(3);
 
    midiSerial.begin(31250);
 
 }

 void loop() {
  //  My potentiometer gave a range from 0 to 1023:
  AnalogValue = analogRead(0);
  //  convert to a range from 0 to 127:
  note = AnalogValue/8;
  currentSwitchState = digitalRead(switchPin);
  // Check to see that the switch is pressed:
  if (currentSwitchState == 1) {
    //  check to see that the switch wasn't pressed last time
    //  through the main loop:
    if (lastSwitchState == 0) {
      // set the note value based on the analog value, plus a couple octaves:
      // note = note + 60;
      // start a note playing:
      noteOn(0x90, note, 0x40);
      // save the note we played, so we can turn it off:
      lastNotePlayed = note;
      digitalWrite(LEDpin, HIGH);
    }
  }
  else {  // if the switch is not pressed:
    //  but the switch was pressed last time through the main loop:
    if (lastSwitchState == 1) {
      //  stop the last note played:
      noteOn(0x90, lastNotePlayed, 0x00);
      digitalWrite(LEDpin, LOW);
    }
  }

  //  save the state of the switch for next time
  //  through the main loop:
  lastSwitchState = currentSwitchState;
 }

 //  plays a MIDI note.  Doesn't check to see that
 //  cmd is greater than 127, or that data values are  less than 127:
 void noteOn(byte cmd, byte data1, byte  data2) {
  midiSerial.write(cmd);
  midiSerial.write(data1);
  midiSerial.write(data2);
to:

(:div class=code :)
Changed lines 109-128 from:
 
  //prints the values in the serial monitor so we can see what note we're playing
  Serial
.print("cmd: ");
    Serial.print(cmd);
      Serial.print(", data1: ");
  Serial.print(data1);
    Serial.print(", data2: ");
  Serial.println(data2);
 }

 // Blinks an LED 3 times
 void blink(int howManyTimes) {
  int i;
  for (i=0; i< howManyTimes; i++) {
    digitalWrite(LEDpin, HIGH);
    delay(100);
    digitalWrite(LEDpin, LOW);
    delay(100);
  }
 }
to:
 #include <SoftwareSerial.h>
Changed lines 111-112 from:

(:sourcend:)
to:
 %color=#cc6600%const%% %color=#cc6600%int%% switchPin = 10;  %color=#7e7e7e%// The switch is on Arduino pin 10
 %color=#cc6600%const%% %color=#cc6600%int%% LEDpin = 13;    %color=#7e7e7e%//  Indicator LED
 
  %color=#7e7e7e%// Variables:
  %color=#cc6600%byte%% note = 0;              %color=#7e7e7e%// The MIDI note value to be played
  %color=#cc6600%int%% AnalogValue = 0;        %color=#7e7e7e%// value from the analog input
  %color=#cc6600%int%% lastNotePlayed = 0;    %color=#7e7e7e%// note turned on when you press the switch
  %color=#cc6600%int%% lastSwitchState = 0;    %color=#7e7e7e%// state of the switch during previous time through the main loop
  %color=#cc6600%int%% currentSwitchState = 0;
 
 %color=#7e7e7e%//software serial
 SoftwareSerial midiSerial(2, 3); %color=#7e7e7e%// digital pins that we'll use for soft serial RX & TX
 
 
  %color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
    %color=#7e7e7e%//  set the states of the I/O pins:
    %color=#cc6600%pinMode%%(switchPin, %color=#006699%INPUT%%);
    %color=#cc6600%pinMode%%(LEDpin, %color=#006699%OUTPUT%%);
    %color=#7e7e7e%//  Set MIDI baud rate:
  %color=#cc6600%'''Serial'''%%.%color=#cc6600%begin%%(9600);
    %color=#cc6600%blink%%(3);
   
    midiSerial.%color=#cc6600%begin%%(31250);
   
  }
 
  %color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
    %color=#7e7e7e%//  My potentiometer gave a range from 0 to 1023:
    AnalogValue = %color=#cc6600%analogRead%%(0);
    %color=#7e7e7e%//  convert to a range from 0 to 127:
    note = AnalogValue/8;
    currentSwitchState = %color=#cc6600%digitalRead%%(switchPin);
    %color=#7e7e7e%// Check to see that the switch is pressed:
    %color=#cc6600%if%% (currentSwitchState == 1) {
      %color=#7e7e7e%//  check to see that the switch wasn't pressed last time
      %color=#7e7e7e%//  through the main loop:
      %color=#cc6600%if%% (lastSwitchState == 0) {
        %color=#7e7e7e%// set the note value based on the analog value, plus a couple octaves:
        %color=#7e7e7e%// note = note + 60;
        %color=#7e7e7e%// start a note playing:
        noteOn(0x90, note, 0x40);
        %color=#7e7e7e%// save the note we played, so we can turn it off:
        lastNotePlayed = note;
        %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%HIGH%%);
      }
    }
    %color=#cc6600%else%% {  %color=#7e7e7e%// if the switch is not pressed:
      %color=#7e7e7e%//  but the switch was pressed last time through the main loop:
      %color=#cc6600%if%% (lastSwitchState == 1) {
        %color=#7e7e7e%//  stop the last note played:
        noteOn(0x90, lastNotePlayed, 0x00);
        %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%LOW%%);
      }
    }
 
    %color=#7e7e7e%//  save the state of the switch for next time
    %color=#7e7e7e%//  through the main loop:
    lastSwitchState = currentSwitchState;
  }
 
  %color=#7e7e7e%//  plays a MIDI note.  Doesn't check to see that
  %color=#7e7e7e%//  cmd is greater than 127, or that data values are  less than 127:
  %color=#cc6600%void%% noteOn(%color=#cc6600%byte%% cmd, %color=#cc6600%byte%% data1, %color=#cc6600%byte%%  data2) {
    midiSerial.%color=#cc6600%write%%(cmd);
  midiSerial.%color=#cc6600%write%%(data1);
    midiSerial.%color=#cc6600%write%%(data2);
 
 
  %color=#7e7e7e%//prints the values in the serial monitor so we can see what note we're playing
    %color=#cc6600%'''Serial'''%%.%color=#cc6600%print%%(%color=#006699%"cmd: "%%);
    %color=#cc6600%'''Serial'''%%.%color=#cc6600%print%%(cmd);
      %color=#cc6600%'''Serial'''%%.%color=#cc6600%print%%(%color=#006699%", data1: "%%);
  %color=#cc6600%'''Serial'''%%.%color=#cc6600%print%%(data1);
      %color=#cc6600%'''Serial'''%%.%color=#cc6600%print%%(%color=#006699%", data2: "%%);
    %color=#cc6600%'''Serial'''%%.%color=#cc6600%println%%(data2);
  }
 
  %color=#7e7e7e%// Blinks an LED 3 times
  %color=#cc6600%void%% %color=#cc6600%blink%%(%color=#cc6600%int%% howManyTimes) {
    %color=#cc6600%int%% i;
    %color=#cc6600%for%% (i=0; i< howManyTimes; i++) {
      %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%HIGH%%);
      %color=#cc6600%delay%%(100);
      %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%LOW%%);
      %color=#cc6600%delay%%(100);
    }
  }
 
(:divend:)

Changed lines 106-107 from:
(:source lang=c++ linenum:)
to:
(:source lang=c++ :)
Changed lines 199-200 from:
(:divend:)
(:sourcend:)
to:

(:sourcend:)
(:divend:)
Changed lines 106-107 from:
(:source lang=c++)
to:
(:source lang=c++ linenum:)
Changed lines 106-107 from:
(:source lang=c++ linenum:)
to:
(:source lang=c++)
Changed lines 106-107 from:
(:source lang=c linenum:)
to:
(:source lang=c++ linenum:)
Changed lines 109-110 from:
[@ # @]include <SoftwareSerial.h>
to:
#include <SoftwareSerial.h>
Added lines 106-107:
(:source lang=c linenum:)
Added line 200:
(:sourcend:)
Added lines 57-58:
(:table:)
Changed lines 55-56 from:
%height=300 alt='arduino midi schematic'%http://www.itp.nyu.edu/physcomp/images/labs/midi_output_breadboard_soft_serial_schem.png
to:
%height=300 alt='arduino midi schematic'%http://itp.nyu.edu/physcomp/uploads/midi_output_breadboard_soft_serial_schem.png
Changed lines 55-57 from:
%height=300 alt='arduino midi schematic'%http://www.itp.nyu.edu/physcomp/images/labs/arduino_midi_schem.png
(:tableend:)
to:
%height=300 alt='arduino midi schematic'%http://www.itp.nyu.edu/physcomp/images/labs/midi_output_breadboard_soft_serial_schem.png
Changed line 52 from:
%width=500 alt='arduino midi circuit on a breadboard.'%[[Attach:midi_output_bb.png | Attach:midi_output_bb.png]]
to:
%width=500 alt='arduino midi circuit on a breadboard.'%[[Attach:midi_out_soft_serial_bb.png | Attach:midi_out_soft_serial_bb.png]]
Changed line 104 from:
 
to:
Changed lines 106-107 from:
#include <SoftwareSerial.h>
to:
[@ # @]include <SoftwareSerial.h>
Added line 105:
//include the software serial library
Changed lines 105-196 from:
 [quote]
#include 
<SoftwareSerial.h>

[color=#CC6600]const[/color] [color=#CC6600]int[/color] switchPin = 10;  [color=#7E7E7E]// The switch is on Arduino pin 10[/color]
[color=#CC6600]
const[/color] [color=#CC6600]int[/color] LEDpin = 13;    [color=#7E7E7E]//  Indicator LED[/color]

 [color=#7E7E7E]
// Variables: [/color]
 [color=#CC6600]
byte[/color] note = 0;              [color=#7E7E7E]// The MIDI note value to be played[/color]
 [color=#CC6600]
int[/color] AnalogValue = 0;        [color=#7E7E7E]// value from the analog input[/color]
 [color=#CC6600]
int[/color] lastNotePlayed = 0;    [color=#7E7E7E]// note turned on when you press the switch[/color]
 [color=#CC6600]
int[/color] lastSwitchState = 0;    [color=#7E7E7E]// state of the switch during previous time through the main loop[/color]
 [color=#CC6600]
int[/color] currentSwitchState = 0;

[color=#7E7E7E]//software serial[/color]
SoftwareSerial midiSerial
(2, 3); [color=#7E7E7E]// digital pins that we'll use for soft serial RX & TX[/color]


 [color=#CC6600]
void[/color] [color=#CC6600][b]setup[/b][/color]() {
   [color=#7E7E7E]//  set the states of the I/O pins:[/color]
   [color=#CC6600]
pinMode[/color](switchPin, [color=#006699]INPUT[/color]);
   [color=#CC6600]pinMode[/color](LEDpin, [color=#006699]OUTPUT[/color]);
   [color=#7E7E7E]//  Set MIDI baud rate:[/color]
  [color=#CC6600][b]
Serial[/b][/color].[color=#CC6600]begin[/color](9600);
   [color=#CC6600]blink[/color](3);
   
    midiSerial
.[color=#CC6600]begin[/color](31250);
   
 
}

 [color=#CC6600]void[/color] [color=#CC6600][b]loop[/b][/color]() {
   [color=#7E7E7E]//  My potentiometer gave a range from 0 to 1023:[/color]
   AnalogValue 
= [color=#CC6600]analogRead[/color](0);
   [color=#7E7E7E]//  convert to a range from 0 to 127:[/color]
   note = AnalogValue/
8;
   currentSwitchState = [color=#CC6600]digitalRead[/color](switchPin);
   [color=#7E7E7E]// Check to see that the switch is pressed:[/color]
   [color=#CC6600]
if[/color] (currentSwitchState == 1) {
     [color=#7E7E7E]//  check to see that the switch wasn't pressed last time[/color]
     [color=#7E7E7E]
//  through the main loop:[/color]
     [color=#CC6600]if[/color] (lastSwitchState == 0) {
       [color
=#7E7E7E]// set the note value based on the analog value, plus a couple octaves:[/color]
       [color=#7E7E7E]
// note = note + 60;[/color]
       [color=#7E7E7E]
// start a note playing:[/color]
       noteOn
(0x90, note, 0x40);
       [color=#7E7E7E]// save the note we played, so we can turn it off:[/color]
       lastNotePlayed 
= note;
       [color=#CC6600]digitalWrite[/color](LEDpin, [color=#006699]HIGH[/color]);
     }
   }
   [color=#CC6600]else[/color][color=#7E7E7E]// if the switch is not pressed:[/color]
     [color=#7E7E7E]
//  but the switch was pressed last time through the main loop:[/color]
     [color=#CC6600]if[/color] (lastSwitchState == 1) {
       [color
=#7E7E7E]//  stop the last note played:[/color]
       noteOn(0x90, lastNotePlayed, 0x00);
       [color=#CC6600]digitalWrite[
/color](LEDpin, [color=#006699]LOW[/color]);
     }
   }

   [color=#7E7E7E]//  save
the state of the switch for next time[/color]
   [color=#7E7E7E]//
  through the main loop:[/color]
   lastSwitchState = currentSwitchState
;
 }

 [color=#7E7E7E]//
  plays a MIDI note.  Doesn't check to see that[/color]
 [color=#7E7E7E]//
  cmd is greater than 127, or that data values are  less than 127:[/color]
 [color=#CC6600]void[
/color] noteOn([color=#CC6600]byte[/color] cmd, [color=#CC6600]byte[/color] data1, [color=#CC6600]byte[/color]  data2) {
   midiSerial.[color=#CC6600]write[
/color](cmd);
  midiSerial.[color=#CC6600]write[
/color](data1);
   midiSerial
.[color=#CC6600]write[/color](data2);
  
  
  [color=#7E7E7E]
//prints the values in the serial monitor so we can see what note we're playing[/color]
   [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]
([color=#006699]"cmd: "[/color]);
    [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](cmd);
      [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]", data1: "[/color]);
  [color=#CC6600][b]Serial[
/b][/color].[color=#CC6600]print[/color](data1);
     [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]", data2: "[/color]);
   [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](data2);
 }

 [color=#7E7E7E]// Blinks an LED 3 times[/color]
 [color=#CC6600]void[/color] [color=#CC6600]blink[/color]([color=#CC6600]int[/color] howManyTimes
) {
   [color=#CC6600]int[/color] i
;
   [color=#CC6600]for[/color] (i=0; i< howManyTimes; i++) {
     [color=#CC6600]digitalWrite[/color](LEDpin, [color=#006699]HIGH[/color]);
     [color=#CC6600]delay[/color](100);
     [color=#CC6600]digitalWrite[/color](LEDpin, [color=#006699]LOW[/color]);
     [color=#CC6600]delay[/color](100);
   }
 }

[/quote]
to:
#include <SoftwareSerial.h>

const int switchPin = 10;  // The switch is on Arduino pin 10
const int LEDpin = 13;    //  Indicator LED

 
// Variables:
 byte note = 0;              // The MIDI note value to be played
 int AnalogValue = 0;        // value from the analog input
 int lastNotePlayed = 0;    // note turned on when you press the switch
 int lastSwitchState = 0;    // state of the switch during previous time through the main loop
 int currentSwitchState = 0;

//software serial
SoftwareSerial midiSerial
(2, 3); // digital pins that we'll use for soft serial RX & TX


 
void setup() {
   //  set the states of the I/O pins:
   pinMode(switchPin, INPUT);
   pinMode(LEDpin, OUTPUT);
   //  Set MIDI baud rate:
  Serial.begin(9600);
   blink(3);
  
    midiSerial
.begin(31250);
  
 
}

 void loop() {
   //  My potentiometer gave a range from 0 to 1023:
   AnalogValue = analogRead(0);
   //  convert to a range from 0 to 127:
   note = AnalogValue/8;
   currentSwitchState = digitalRead(switchPin);
   // Check to see that the switch is pressed:
   if (currentSwitchState == 1) {
     //  check to see that the switch wasn't pressed last time
     //  through the main loop:
    if (lastSwitchState == 0) {
      // set the note value based on the analog value, plus a couple
octaves:
       // note = note + 60;
       // start a note playing:
       noteOn(0x90, note, 0x40);
       // save the note we played, so we can turn it off:
       lastNotePlayed = note;
       digitalWrite(LEDpin, HIGH);
     }
   }
   else {  // if the switch is not pressed:
     //  but the switch was pressed last time through the main loop:
    if (lastSwitchState == 1) {
     
//  stop the last note played:
   
   noteOn(0x90, lastNotePlayed, 0x00);
       digitalWrite(LEDpin, LOW);
 
  }
  }

  //  save the state of the switch for next time
 
//  through the main loop:
  lastSwitchState
= currentSwitchState;
 }

//  plays a MIDI note.  Doesn't check to see that
 
//  cmd is greater than 127, or that data values are  less than 127:
 void noteOn
(byte cmd, byte data1, byte  data2) {
  midiSerial.write(cmd)
;
  midiSerial.write(data1);
   midiSerial.write(data2);
 

 
 
//prints the values in the serial monitor so we can see what note we're playing
  Serial
.print("cmd: ");
   Serial.print(cmd);
      Serial.print(
", data1: ");
  Serial.print(data1);
     Serial.print(", data2: ");
   Serial.println(data2);
 }

 // Blinks an LED 3 times
 void blink(int howManyTimes)
{
   int i;
  for (i
=0; i< howManyTimes; i++) {
    digitalWrite(LEDpin, HIGH)
;
     delay(100);
     digitalWrite(LEDpin, LOW);
    delay(100);
 
}
 }
Added lines 105-196:
 [quote]
#include <SoftwareSerial.h>

[color=#CC6600]const[/color] [color=#CC6600]int[/color] switchPin = 10;  [color=#7E7E7E]// The switch is on Arduino pin 10[/color]
[color=#CC6600]const[/color] [color=#CC6600]int[/color] LEDpin = 13;    [color=#7E7E7E]//  Indicator LED[/color]

 [color=#7E7E7E]// Variables: [/color]
 [color=#CC6600]byte[/color] note = 0;              [color=#7E7E7E]// The MIDI note value to be played[/color]
 [color=#CC6600]int[/color] AnalogValue = 0;        [color=#7E7E7E]// value from the analog input[/color]
 [color=#CC6600]int[/color] lastNotePlayed = 0;    [color=#7E7E7E]// note turned on when you press the switch[/color]
 [color=#CC6600]int[/color] lastSwitchState = 0;    [color=#7E7E7E]// state of the switch during previous time through the main loop[/color]
 [color=#CC6600]int[/color] currentSwitchState = 0;

[color=#7E7E7E]//software serial[/color]
SoftwareSerial midiSerial(2, 3); [color=#7E7E7E]// digital pins that we'll use for soft serial RX & TX[/color]


 [color=#CC6600]void[/color] [color=#CC6600][b]setup[/b][/color]() {
   [color=#7E7E7E]//  set the states of the I/O pins:[/color]
   [color=#CC6600]pinMode[/color](switchPin, [color=#006699]INPUT[/color]);
   [color=#CC6600]pinMode[/color](LEDpin, [color=#006699]OUTPUT[/color]);
   [color=#7E7E7E]//  Set MIDI baud rate:[/color]
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]begin[/color](9600);
   [color=#CC6600]blink[/color](3);
   
    midiSerial.[color=#CC6600]begin[/color](31250);
   
 }

 [color=#CC6600]void[/color] [color=#CC6600][b]loop[/b][/color]() {
   [color=#7E7E7E]//  My potentiometer gave a range from 0 to 1023:[/color]
   AnalogValue = [color=#CC6600]analogRead[/color](0);
   [color=#7E7E7E]//  convert to a range from 0 to 127:[/color]
   note = AnalogValue/8;
   currentSwitchState = [color=#CC6600]digitalRead[/color](switchPin);
   [color=#7E7E7E]// Check to see that the switch is pressed:[/color]
   [color=#CC6600]if[/color] (currentSwitchState == 1) {
     [color=#7E7E7E]//  check to see that the switch wasn't pressed last time[/color]
     [color=#7E7E7E]//  through the main loop:[/color]
     [color=#CC6600]if[/color] (lastSwitchState == 0) {
       [color=#7E7E7E]// set the note value based on the analog value, plus a couple octaves:[/color]
       [color=#7E7E7E]// note = note + 60;[/color]
       [color=#7E7E7E]// start a note playing:[/color]
       noteOn(0x90, note, 0x40);
       [color=#7E7E7E]// save the note we played, so we can turn it off:[/color]
       lastNotePlayed = note;
       [color=#CC6600]digitalWrite[/color](LEDpin, [color=#006699]HIGH[/color]);
     }
   }
   [color=#CC6600]else[/color] {  [color=#7E7E7E]// if the switch is not pressed:[/color]
     [color=#7E7E7E]//  but the switch was pressed last time through the main loop:[/color]
     [color=#CC6600]if[/color] (lastSwitchState == 1) {
       [color=#7E7E7E]//  stop the last note played:[/color]
       noteOn(0x90, lastNotePlayed, 0x00);
       [color=#CC6600]digitalWrite[/color](LEDpin, [color=#006699]LOW[/color]);
     }
   }

   [color=#7E7E7E]//  save the state of the switch for next time[/color]
   [color=#7E7E7E]//  through the main loop:[/color]
   lastSwitchState = currentSwitchState;
 }

 [color=#7E7E7E]//  plays a MIDI note.  Doesn't check to see that[/color]
 [color=#7E7E7E]//  cmd is greater than 127, or that data values are  less than 127:[/color]
 [color=#CC6600]void[/color] noteOn([color=#CC6600]byte[/color] cmd, [color=#CC6600]byte[/color] data1, [color=#CC6600]byte[/color]  data2) {
   midiSerial.[color=#CC6600]write[/color](cmd);
  midiSerial.[color=#CC6600]write[/color](data1);
   midiSerial.[color=#CC6600]write[/color](data2);
  
  
  [color=#7E7E7E]//prints the values in the serial monitor so we can see what note we're playing[/color]
   [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]"cmd: "[/color]);
    [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](cmd);
      [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]", data1: "[/color]);
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](data1);
     [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]", data2: "[/color]);
   [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](data2);
 }

 [color=#7E7E7E]// Blinks an LED 3 times[/color]
 [color=#CC6600]void[/color] [color=#CC6600]blink[/color]([color=#CC6600]int[/color] howManyTimes) {
   [color=#CC6600]int[/color] i;
   [color=#CC6600]for[/color] (i=0; i< howManyTimes; i++) {
     [color=#CC6600]digitalWrite[/color](LEDpin, [color=#006699]HIGH[/color]);
     [color=#CC6600]delay[/color](100);
     [color=#CC6600]digitalWrite[/color](LEDpin, [color=#006699]LOW[/color]);
     [color=#CC6600]delay[/color](100);
   }
 }

[/quote]
Deleted lines 197-289:
 <pre>
#include&nbsp;&lt;SoftwareSerial.h&gt;

<span style="color: #CC6600;">const</span> <span style="color: #CC6600;">int</span> switchPin = 10;  <span style="color: #7E7E7E;">// The switch is on Arduino pin 10</span>
<span style="color: #CC6600;">const</span> <span style="color: #CC6600;">int</span> LEDpin = 13;    <span style="color: #7E7E7E;">//  Indicator LED</span>

&nbsp;<span style="color: #7E7E7E;">// Variables: </span>
&nbsp;<span style="color: #CC6600;">byte</span> note = 0;              <span style="color: #7E7E7E;">// The MIDI note value to be played</span>
&nbsp;<span style="color: #CC6600;">int</span> AnalogValue = 0;        <span style="color: #7E7E7E;">// value from the analog input</span>
&nbsp;<span style="color: #CC6600;">int</span> lastNotePlayed = 0;    <span style="color: #7E7E7E;">// note turned on when you press the switch</span>
&nbsp;<span style="color: #CC6600;">int</span> lastSwitchState = 0;    <span style="color: #7E7E7E;">// state of the switch during previous time through the main loop</span>
&nbsp;<span style="color: #CC6600;">int</span> currentSwitchState = 0;

<span style="color: #7E7E7E;">//software&nbsp;serial</span>
SoftwareSerial&nbsp;midiSerial(2,&nbsp;3);&nbsp;<span style="color: #7E7E7E;">// digital pins that we'll use for soft serial RX &amp; TX</span>


&nbsp;<span style="color: #CC6600;">void</span> <span style="color: #CC6600;"><b>setup</b></span>() {
&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">//  set the states of the I/O pins:</span>
&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">pinMode</span>(switchPin, <span style="color: #006699;">INPUT</span>);
&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">pinMode</span>(LEDpin, <span style="color: #006699;">OUTPUT</span>);
&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">//  Set MIDI baud rate:</span>
&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">begin</span>(9600);
&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">blink</span>(3);
&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;midiSerial.<span style="color: #CC6600;">begin</span>(31250);
&nbsp;&nbsp;&nbsp;
&nbsp;}

&nbsp;<span style="color: #CC6600;">void</span> <span style="color: #CC6600;"><b>loop</b></span>() {
&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">//  My potentiometer gave a range from 0 to 1023:</span>
&nbsp;&nbsp;&nbsp;AnalogValue&nbsp;=&nbsp;<span style="color: #CC6600;">analogRead</span>(0);
&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">//  convert to a range from 0 to 127:</span>
&nbsp;&nbsp;&nbsp;note&nbsp;=&nbsp;AnalogValue/8;
&nbsp;&nbsp;&nbsp;currentSwitchState&nbsp;=&nbsp;<span style="color: #CC6600;">digitalRead</span>(switchPin);
&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// Check to see that the switch is pressed:</span>
&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">if</span> (currentSwitchState == 1) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">//  check to see that the switch wasn't pressed last time</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">//  through the main loop:</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">if</span> (lastSwitchState == 0) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// set the note value based on the analog value, plus a couple octaves:</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// note = note + 60;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// start a note playing:</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;noteOn(0x90,&nbsp;note,&nbsp;0x40);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// save the note we played, so we can turn it off:</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lastNotePlayed&nbsp;=&nbsp;note;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">digitalWrite</span>(LEDpin, <span style="color: #006699;">HIGH</span>);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">else</span> {  <span style="color: #7E7E7E;">// if the switch is not pressed:</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">//  but the switch was pressed last time through the main loop:</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">if</span> (lastSwitchState == 1) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">//  stop the last note played:</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;noteOn(0x90,&nbsp;lastNotePlayed,&nbsp;0x00);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">digitalWrite</span>(LEDpin, <span style="color: #006699;">LOW</span>);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">//  save the state of the switch for next time</span>
&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">//  through the main loop:</span>
&nbsp;&nbsp;&nbsp;lastSwitchState&nbsp;=&nbsp;currentSwitchState;
&nbsp;}

&nbsp;<span style="color: #7E7E7E;">//  plays a MIDI note.  Doesn't check to see that</span>
&nbsp;<span style="color: #7E7E7E;">//  cmd is greater than 127, or that data values are  less than 127:</span>
&nbsp;<span style="color: #CC6600;">void</span> noteOn(<span style="color: #CC6600;">byte</span> cmd, <span style="color: #CC6600;">byte</span> data1, <span style="color: #CC6600;">byte</span>  data2) {
&nbsp;&nbsp;&nbsp;midiSerial.<span style="color: #CC6600;">write</span>(cmd);
&nbsp;&nbsp;midiSerial.<span style="color: #CC6600;">write</span>(data1);
&nbsp;&nbsp;&nbsp;midiSerial.<span style="color: #CC6600;">write</span>(data2);
&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;<span style="color: #7E7E7E;">//prints the values in the serial monitor so we can see what note we're playing</span>
&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">print</span>(<span style="color: #006699;">"cmd: "</span>);
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">print</span>(cmd);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">print</span>(<span style="color: #006699;">", data1: "</span>);
&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">print</span>(data1);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">print</span>(<span style="color: #006699;">", data2: "</span>);
&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">println</span>(data2);
&nbsp;}

&nbsp;<span style="color: #7E7E7E;">// Blinks an LED 3 times</span>
&nbsp;<span style="color: #CC6600;">void</span> <span style="color: #CC6600;">blink</span>(<span style="color: #CC6600;">int</span> howManyTimes) {
&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">int</span> i;
&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">for</span> (i=0; i&lt; howManyTimes; i++) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">digitalWrite</span>(LEDpin, <span style="color: #006699;">HIGH</span>);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">delay</span>(100);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">digitalWrite</span>(LEDpin, <span style="color: #006699;">LOW</span>);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">delay</span>(100);
&nbsp;&nbsp;&nbsp;}
&nbsp;}

</pre>
 
Changed lines 106-108 from:
 const %color=#cc6600%int%% switchPin = 10;  %color=#7e7e7e%// The switch is on Arduino pin 10
 const %color=#cc6600%int%% middleC = 60;    %color=#7e7e7e%// Middle C (MIDI note value 60) is the lowest note we'll play
 const %color=#cc6600%int%% LEDpin = 13;    %color=#7e7e7e%//  Indicator LED
to:
 <pre>
#include&nbsp;&lt;SoftwareSerial.h&gt;

<span style="color: #CC6600;">const</span> <span style="color: #CC6600;">int</span> switchPin = 10;  <span style="color: #7E7E7E;">// The switch is on Arduino pin 10</span>
<span style="color: #CC6600;">const</span> <span style="color: #CC6600;">int</span> LEDpin = 13;    <span style="color: #7E7E7E;">//  Indicator LED</span>

&nbsp;<span style="color: #7E7E7E;">// Variables: </span>
&nbsp;<span style="color: #CC6600;">byte</span> note = 0;              <span style="color: #7E7E7E;">// The MIDI note value to be played</span>
&nbsp;<span style="color: #CC6600;">int</span> AnalogValue = 0;        <span style="color: #7E7E7E;">// value from the analog input</span>
&nbsp;<span style="color: #CC6600;">int</span> lastNotePlayed = 0;    <span style="color: #7E7E7E;">// note turned on when you press the switch</span>
&nbsp;<span style="color: #CC6600;">int</span> lastSwitchState = 0;    <span style="color: #7E7E7E;">// state of the switch during previous time through the main loop</span>
&nbsp;<span style="color: #CC6600;">int</span> currentSwitchState = 0;

<span style="color: #7E7E7E;">//software&nbsp;serial</span>
SoftwareSerial&nbsp;midiSerial(2,&nbsp;3);&nbsp;<span style="color: #7E7E7E;">// digital pins that we'll use for soft serial RX &amp; TX</span>


&nbsp;<span style="color: #CC6600;">void</span> <span style="color: #CC6600;"><b>setup</b></span>() {
&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">//  set the states of the I/O pins:</span>
&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">pinMode</span>(switchPin, <span style="color: #006699;">INPUT</span>);
&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">pinMode</span>(LEDpin, <span style="color: #006699;">OUTPUT</span>);
&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">//  Set MIDI baud rate:</span>
&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">begin</span>(9600);
&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">blink</span>(3);
&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;midiSerial.<span style="color: #CC6600;">begin</span>(31250);
&nbsp;&nbsp;&nbsp;
&nbsp;}

&nbsp;<span style="color: #CC6600;">void</span> <span style="color: #CC6600;"><b>loop</b></span>() {
&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">//  My potentiometer gave a range from 0 to 1023:</span>
&nbsp;&nbsp;&nbsp;AnalogValue&nbsp;=&nbsp;<span style="color: #CC6600;">analogRead</span>(0);
&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">//  convert to a range from 0 to 127:</span>
&nbsp;&nbsp;&nbsp;note&nbsp;=&nbsp;AnalogValue/8;
&nbsp;&nbsp;&nbsp;currentSwitchState&nbsp;=&nbsp;<span style="color: #CC6600;">digitalRead</span>(switchPin);
&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// Check to see that the switch is pressed:</span>
&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">if</span> (currentSwitchState == 1) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">//  check to see that the switch wasn't pressed last time</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">//  through the main loop:</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">if</span> (lastSwitchState == 0) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// set the note value based on the analog value, plus a couple octaves:</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// note = note + 60;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// start a note playing:</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;noteOn(0x90,&nbsp;note,&nbsp;0x40);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">// save the note we played, so we can turn it off:</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lastNotePlayed&nbsp;=&nbsp;note;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">digitalWrite</span>(LEDpin, <span style="color: #006699;">HIGH</span>);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">else</span> {  <span style="color: #7E7E7E;">// if the switch is not pressed:</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">//  but the switch was pressed last time through the main loop:</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">if</span> (lastSwitchState == 1) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">//  stop the last note played:</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;noteOn(0x90,&nbsp;lastNotePlayed,&nbsp;0x00);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">digitalWrite</span>(LEDpin, <span style="color: #006699;">LOW</span>);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">//  save the state of the switch for next time</span>
&nbsp;&nbsp;&nbsp;<span style="color: #7E7E7E;">//  through the main loop:</span>
&nbsp;&nbsp;&nbsp;lastSwitchState&nbsp;=&nbsp;currentSwitchState;
&nbsp;}

&nbsp;<span style="color: #7E7E7E;">//  plays a MIDI note.  Doesn't check to see that</span>
&nbsp;<span style="color: #7E7E7E;">//  cmd is greater than 127, or that data values are  less than 127:</span>
&nbsp;<span style="color: #CC6600;">void</span> noteOn(<span style="color: #CC6600;">byte</span> cmd, <span style="color: #CC6600;">byte</span> data1, <span style="color: #CC6600;">byte</span>  data2) {
&nbsp;&nbsp;&nbsp;midiSerial.<span style="color: #CC6600;">write</span>(cmd);
&nbsp;&nbsp;midiSerial.<span style="color: #CC6600;">write</span>(data1);
&nbsp;&nbsp;&nbsp;midiSerial.<span style="color: #CC6600;">write</span>(data2);
&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;<span style="color: #7E7E7E;">//prints the values in the serial monitor so we can see what note we're playing</span>
&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">print</span>(<span style="color: #006699;">"cmd: "</span>);
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">print</span>(cmd);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">print</span>(<span style="color: #006699;">", data1: "</span>);
&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">print</span>(data1);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">print</span>(<span style="color: #006699;">", data2: "</span>);
&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">println</span>(data2);
&nbsp;}

&nbsp;<span style="color: #7E7E7E;">// Blinks an LED 3 times</span>
&nbsp;<span style="color: #CC6600;">void</span> <span style="color: #CC6600;">blink</span>(<span style="color: #CC6600;">int</span> howManyTimes) {
&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">int</span> i;
&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">for</span> (i=0; i&lt; howManyTimes; i++) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">digitalWrite</span>(LEDpin, <span style="color: #006699;">HIGH</span>);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">delay</span>(100);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">digitalWrite</span>(LEDpin, <span style="color: #006699;">LOW</span>);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #CC6600;">delay</span>(100);
&nbsp;&nbsp;&nbsp;}
&nbsp;}

</pre>
Deleted lines 198-266:
 %color=#7e7e7e%// Variables:
 %color=#cc6600%byte%% note = 0;              %color=#7e7e7e%// The MIDI note value to be played
 %color=#cc6600%int%% AnalogValue = 0;        %color=#7e7e7e%// value from the analog input
 %color=#cc6600%int%% lastNotePlayed = 0;    %color=#7e7e7e%// note turned on when you press the switch
 %color=#cc6600%int%% lastSwitchState = 0;    %color=#7e7e7e%// state of the switch during previous time through the main loop
 %color=#cc6600%int%% currentSwitchState = 0;
 
 %color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
  %color=#7e7e7e%//  set the states of the I/O pins:
  %color=#cc6600%pinMode%%(switchPin, %color=#006699%INPUT%%);
  %color=#cc6600%pinMode%%(LEDpin, %color=#006699%OUTPUT%%);
  %color=#7e7e7e%//  Set MIDI baud rate:
  %color=#cc6600%Serial%%.%color=#cc6600%begin%%(31250);
  %color=#cc6600%blink%%(3);
 }
 
 %color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
  %color=#7e7e7e%//  My potentiometer gave a range from 0 to 1023:
  AnalogValue = %color=#cc6600%analogRead%%(0);
  %color=#7e7e7e%//  convert to a range from 0 to 127:
  note = AnalogValue/8;
  currentSwitchState = %color=#cc6600%digitalRead%%(switchPin);
  %color=#7e7e7e%// Check to see that the switch is pressed:
  %color=#cc6600%if%% (currentSwitchState == 1) {
    %color=#7e7e7e%//  check to see that the switch wasn't pressed last time
    %color=#7e7e7e%//  through the main loop:
    %color=#cc6600%if%% (lastSwitchState == 0) {
      %color=#7e7e7e%// set the note value based on the analog value, plus a couple octaves:
      %color=#7e7e7e%// note = note + 60;
      %color=#7e7e7e%// start a note playing:
      noteOn(0x90, note, 0x40);
      %color=#7e7e7e%// save the note we played, so we can turn it off:
      lastNotePlayed = note;
      %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%HIGH%%);
    }
  }
  %color=#cc6600%else%% {  %color=#7e7e7e%// if the switch is not pressed:
    %color=#7e7e7e%//  but the switch was pressed last time through the main loop:
    %color=#cc6600%if%% (lastSwitchState == 1) {
      %color=#7e7e7e%//  stop the last note played:
      noteOn(0x90, lastNotePlayed, 0x00);
      %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%LOW%%);
    }
  }
 
  %color=#7e7e7e%//  save the state of the switch for next time
  %color=#7e7e7e%//  through the main loop:
  lastSwitchState = currentSwitchState;
 }
 
 %color=#7e7e7e%//  plays a MIDI note.  Doesn't check to see that
 %color=#7e7e7e%//  cmd is greater than 127, or that data values are  less than 127:
 %color=#cc6600%void%% noteOn(%color=#cc6600%byte%% cmd, %color=#cc6600%byte%% data1, %color=#cc6600%byte%%  data2) {
  %color=#cc6600%Serial%%.%color=#cc6600%write%%(cmd);
  %color=#cc6600%Serial%%.%color=#cc6600%write%%(data1);
  %color=#cc6600%Serial%%.%color=#cc6600%write%%(data2);
 }
 
 %color=#7e7e7e%// Blinks an LED 3 times
 %color=#cc6600%void%% %color=#cc6600%blink%%(%color=#cc6600%int%% howManyTimes) {
  %color=#cc6600%int%% i;
  %color=#cc6600%for%% (i=0; i< howManyTimes; i++) {
    %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%HIGH%%);
    %color=#cc6600%delay%%(100);
    %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%LOW%%);
    %color=#cc6600%delay%%(100);
  }
 }
 
Changed lines 36-38 from:
%width=600 alt='Arduino connected to a breadboard' align=top valign=center%[[Attach:arduino_and_breadboard_bb.png|Attach:arduino_and_breadboard_bb.png]]
[--(Diagram made with [[http://fritzing.org | Fritzing]] - [[Attach:arduino_and_breadboard.zip | download]])--]
to:
%width=600 alt='Arduino connected to a breadboard' align=top valign=center%[[Attach:Arduino and breadboard_bb.png|Attach:Arduino and breadboard_bb.png]]
[--(Diagram made with [[http://fritzing.org | Fritzing]] - [[Attach:Arduino and breadboard.fzz.zip | download]])--]
Changed line 53 from:
[--(Diagram made with [[http://fritzing.org | Fritzing]] - [[Attach:MIDI_output.zip | download]])--]
to:
[--(Diagram made with [[http://fritzing.org | Fritzing]] - [[Attach:midi_output_breadboard.zip| download]])--]
Changed line 52 from:
%width=500 alt='arduino midi circuit on a breadboard.'%[[Attach:MIDI_output_bb | Attach:midi_out_bb]]
to:
%width=500 alt='arduino midi circuit on a breadboard.'%[[Attach:midi_output_bb.png | Attach:midi_output_bb.png]]
Changed line 52 from:
%width=500 alt='arduino midi circuit on a breadboard.'%[[Attach:MIDI_output_bb.png | Attach:midi_out_bb.png]]
to:
%width=500 alt='arduino midi circuit on a breadboard.'%[[Attach:MIDI_output_bb | Attach:midi_out_bb]]
Changed line 52 from:
%width=500 alt='arduino midi circuit on a breadboard.'%[[Attach:MIDI_output_bb.png | Attach:MIDI_output_bb.png]]
to:
%width=500 alt='arduino midi circuit on a breadboard.'%[[Attach:MIDI_output_bb.png | Attach:midi_out_bb.png]]
Changed lines 163-165 from:
   %color=#cc6600%Serial%%.%color=#cc6600%print%%(cmd, %color=#006699%BYTE%%);
 
%color=#cc6600%Serial%%.%color=#cc6600%print%%(data1, %color=#006699%BYTE%%);
 
%color=#cc6600%Serial%%.%color=#cc6600%print%%(data2, %color=#006699%BYTE%%);
to:
   %color=#cc6600%Serial%%.%color=#cc6600%write%%(cmd);
 
%color=#cc6600%Serial%%.%color=#cc6600%write%%(data1);
 
%color=#cc6600%Serial%%.%color=#cc6600%write%%(data2);
Changed lines 91-93 from:
   %color=#cc6600%Serial%%.%color=#cc6600%print%%(cmd, %color=#006699%BYTE%%);
 
%color=#cc6600%Serial%%.%color=#cc6600%print%%(data1, %color=#006699%BYTE%%);
 
%color=#cc6600%Serial%%.%color=#cc6600%print%%(data2, %color=#006699%BYTE%%);
to:
   %color=#cc6600%Serial%%.%color=#cc6600%write%%(cmd);
 
%color=#cc6600%Serial%%.%color=#cc6600%write%%(data1);
 
%color=#cc6600%Serial%%.%color=#cc6600%write%%(data2);
Changed lines 37-38 from:
to:
[--(Diagram made with [[http://fritzing.org | Fritzing]] - [[Attach:arduino_and_breadboard.zip | download]])--]
Added lines 52-54:
%width=500 alt='arduino midi circuit on a breadboard.'%[[Attach:MIDI_output_bb.png | Attach:MIDI_output_bb.png]]
[--(Diagram made with [[http://fritzing.org | Fritzing]] - [[Attach:MIDI_output.zip | download]])--]
(:cell:)
Deleted lines 55-56:
(:cell:)
%width=600 alt='arduino midi circuit on a breadboard.'%[[Attach:MIDI_output_bb.png | Attach:MIDI_output_bb.png]]
Changed lines 36-37 from:
%alt='Arduino connected to a breadboard' align=top valign=center%http://itp.nyu.edu/physcomp/images/labs/arduino_bboard_power.jpg
to:
%width=600 alt='Arduino connected to a breadboard' align=top valign=center%[[Attach:arduino_and_breadboard_bb.png|Attach:arduino_and_breadboard_bb.png]]
Changed line 53 from:
%height=300 alt='arduino midi circuit on a breadboard.'%http://www.itp.nyu.edu/physcomp/images/labs/bboard_midi_sensors.JPG
to:
%width=600 alt='arduino midi circuit on a breadboard.'%[[Attach:MIDI_output_bb.png | Attach:MIDI_output_bb.png]]
Added lines 180-182:

''This is a suggestion for the Media Controller assignment.  You can do any project you wish as long as it demonstrates your mastery of the lab exercises and good physical interaction. This is just one suggestion.''

Deleted lines 62-94:
// Variables:
byte note = 0;            // The MIDI note value to be played

void setup() {
  //  Set MIDI baud rate:
  Serial.begin(31250);
}

void loop() {
  // play notes from F#-0 (30) to F#-5 (90):
  for (note = 30; note < 90; note ++) {
    //Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
    noteOn(0x90, note, 0x45);
    delay(100);
    //Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
    noteOn(0x90, note, 0x00); 
    delay(100);
  }
}

//  plays a MIDI note.  Doesn't check to see that
//  cmd is greater than 127, or that data values are  less than 127:
void noteOn(byte cmd, byte data1, byte data2) {
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);
}


!!! Allow a Person to Play Notes

The previous example will just play notes, no interactivity.  The example below uses an analog input to set the pitch, and a digital input (a switch) to start and stop the note:

Deleted lines 65-68:
 const %color=#cc6600%int%% switchPin = 10;  %color=#7e7e7e%// The switch is on Arduino pin 10
 const %color=#cc6600%int%% middleC = 60;    %color=#7e7e7e%// Middle C (MIDI note value 60) is the lowest note we'll play
 const %color=#cc6600%int%% LEDpin = 13;    %color=#7e7e7e%//  Indicator LED
 
Changed lines 67-71 from:
 %color=#cc6600%byte%% note = 0;              %color=#7e7e7e%// The MIDI note value to be played
 %color=#cc6600%int%% AnalogValue = 0;        %color=#7e7e7e%// value from the analog input
 %color=#cc6600%int%% lastNotePlayed = 0;    %color=#7e7e7e%// note turned on when you press the switch
 %color=#cc6600%int%% lastSwitchState = 0;    %color=#7e7e7e%// state of the switch during previous time through the main loop
 %color=#cc6600%int%% currentSwitchState = 0;
to:
 %color=#cc6600%byte%% note = 0;            %color=#7e7e7e%// The MIDI note value to be played
Deleted lines 69-71:
   %color=#7e7e7e%//  set the states of the I/O pins:
  %color=#cc6600%pinMode%%(switchPin, %color=#006699%INPUT%%);
  %color=#cc6600%pinMode%%(LEDpin, %color=#006699%OUTPUT%%);
Deleted line 71:
   %color=#cc6600%blink%%(3);
Changed lines 75-92 from:
   %color=#7e7e7e%//  My potentiometer gave a range from 0 to 1023:
  AnalogValue
= %color=#cc6600%analogRead%%(0);
   %color=#7e7e7e%//  convert to a range from 0 to 127:
  note
= AnalogValue/8;
  currentSwitchState = %color=#cc6600%digitalRead%%
(switchPin);
  %color=#7e7e7e%// Check to see that the switch is pressed:
  %color=#cc6600%if%% (currentSwitchState == 1) {
     %color=#7e7e7e%//  check to see that the switch wasn't pressed last time
    %color=#7e7e7e%//  through the main loop:
    %color=#cc6600%if%% (lastSwitchState == 0) {
      %color=#7e7e7e%// set the note value based on the analog value, plus a couple octaves:
      %color=#7e7e7e%// note = note + 60
;
      %color=#7e7e7e%// start a note playing:
      noteOn(0x90, note, 0x40);
      %color=#7e7e7e%// save the note we played, so we can turn it off:
      lastNotePlayed = note;
      %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%HIGH%%);
    }
to:
   %color=#7e7e7e%// play notes from F#-0 (30) to F#-5 (90):
  %color
=#cc6600%for%% (note = 30; note < 90; note ++) {
    %color
=#7e7e7e%//Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
    noteOn(0x90, note, 0x45);
 
   %color=#cc6600%delay%%(100);
    %color=#7e7e7e%//Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
    noteOn(0x90, note, 0x00); 
    %color=#cc6600%delay%%(100);
Changed lines 84-91 from:
   %color=#cc6600%else%% {  %color=#7e7e7e%// if the switch is not pressed:
    %color=#7e7e7e%//  but the switch was pressed last time through the main loop:
    %color=#cc6600%if%% (lastSwitchState == 1) {
      %color=#7e7e7e%//  stop the last note played:
      noteOn(0x90, lastNotePlayed, 0x00);
      %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%LOW%%);
    }
 
}
to:
 }
Deleted lines 85-89:
   %color=#7e7e7e%//  save the state of the switch for next time
  %color=#7e7e7e%//  through the main loop:
  lastSwitchState = currentSwitchState;
 }
 
Changed line 88 from:
 %color=#cc6600%void%% noteOn(%color=#cc6600%byte%% cmd, %color=#cc6600%byte%% data1, %color=#cc6600%byte%%  data2) {
to:
 %color=#cc6600%void%% noteOn(%color=#cc6600%byte%% cmd, %color=#cc6600%byte%% data1, %color=#cc6600%byte%% data2) {
Deleted lines 93-102:
 %color=#7e7e7e%// Blinks an LED 3 times
 %color=#cc6600%void%% %color=#cc6600%blink%%(%color=#cc6600%int%% howManyTimes) {
  %color=#cc6600%int%% i;
  %color=#cc6600%for%% (i=0; i< howManyTimes; i++) {
    %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%HIGH%%);
    %color=#cc6600%delay%%(100);
    %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%LOW%%);
    %color=#cc6600%delay%%(100);
  }
 }
Added lines 96-177:

!!! Allow a Person to Play Notes

The previous example will just play notes, no interactivity.  The example below uses an analog input to set the pitch, and a digital input (a switch) to start and stop the note:

(:div class=code :)
 
 
 const %color=#cc6600%int%% switchPin = 10;  %color=#7e7e7e%// The switch is on Arduino pin 10
 const %color=#cc6600%int%% middleC = 60;    %color=#7e7e7e%// Middle C (MIDI note value 60) is the lowest note we'll play
 const %color=#cc6600%int%% LEDpin = 13;    %color=#7e7e7e%//  Indicator LED
 
 %color=#7e7e7e%// Variables:
 %color=#cc6600%byte%% note = 0;              %color=#7e7e7e%// The MIDI note value to be played
 %color=#cc6600%int%% AnalogValue = 0;        %color=#7e7e7e%// value from the analog input
 %color=#cc6600%int%% lastNotePlayed = 0;    %color=#7e7e7e%// note turned on when you press the switch
 %color=#cc6600%int%% lastSwitchState = 0;    %color=#7e7e7e%// state of the switch during previous time through the main loop
 %color=#cc6600%int%% currentSwitchState = 0;
 
 %color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
  %color=#7e7e7e%//  set the states of the I/O pins:
  %color=#cc6600%pinMode%%(switchPin, %color=#006699%INPUT%%);
  %color=#cc6600%pinMode%%(LEDpin, %color=#006699%OUTPUT%%);
  %color=#7e7e7e%//  Set MIDI baud rate:
  %color=#cc6600%Serial%%.%color=#cc6600%begin%%(31250);
  %color=#cc6600%blink%%(3);
 }
 
 %color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
  %color=#7e7e7e%//  My potentiometer gave a range from 0 to 1023:
  AnalogValue = %color=#cc6600%analogRead%%(0);
  %color=#7e7e7e%//  convert to a range from 0 to 127:
  note = AnalogValue/8;
  currentSwitchState = %color=#cc6600%digitalRead%%(switchPin);
  %color=#7e7e7e%// Check to see that the switch is pressed:
  %color=#cc6600%if%% (currentSwitchState == 1) {
    %color=#7e7e7e%//  check to see that the switch wasn't pressed last time
    %color=#7e7e7e%//  through the main loop:
    %color=#cc6600%if%% (lastSwitchState == 0) {
      %color=#7e7e7e%// set the note value based on the analog value, plus a couple octaves:
      %color=#7e7e7e%// note = note + 60;
      %color=#7e7e7e%// start a note playing:
      noteOn(0x90, note, 0x40);
      %color=#7e7e7e%// save the note we played, so we can turn it off:
      lastNotePlayed = note;
      %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%HIGH%%);
    }
  }
  %color=#cc6600%else%% {  %color=#7e7e7e%// if the switch is not pressed:
    %color=#7e7e7e%//  but the switch was pressed last time through the main loop:
    %color=#cc6600%if%% (lastSwitchState == 1) {
      %color=#7e7e7e%//  stop the last note played:
      noteOn(0x90, lastNotePlayed, 0x00);
      %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%LOW%%);
    }
  }
 
  %color=#7e7e7e%//  save the state of the switch for next time
  %color=#7e7e7e%//  through the main loop:
  lastSwitchState = currentSwitchState;
 }
 
 %color=#7e7e7e%//  plays a MIDI note.  Doesn't check to see that
 %color=#7e7e7e%//  cmd is greater than 127, or that data values are  less than 127:
 %color=#cc6600%void%% noteOn(%color=#cc6600%byte%% cmd, %color=#cc6600%byte%% data1, %color=#cc6600%byte%%  data2) {
  %color=#cc6600%Serial%%.%color=#cc6600%print%%(cmd, %color=#006699%BYTE%%);
  %color=#cc6600%Serial%%.%color=#cc6600%print%%(data1, %color=#006699%BYTE%%);
  %color=#cc6600%Serial%%.%color=#cc6600%print%%(data2, %color=#006699%BYTE%%);
 }
 
 %color=#7e7e7e%// Blinks an LED 3 times
 %color=#cc6600%void%% %color=#cc6600%blink%%(%color=#cc6600%int%% howManyTimes) {
  %color=#cc6600%int%% i;
  %color=#cc6600%for%% (i=0; i< howManyTimes; i++) {
    %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%HIGH%%);
    %color=#cc6600%delay%%(100);
    %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%LOW%%);
    %color=#cc6600%delay%%(100);
  }
 }
 
(:divend:)
Added lines 63-95:
// Variables:
byte note = 0;            // The MIDI note value to be played

void setup() {
  //  Set MIDI baud rate:
  Serial.begin(31250);
}

void loop() {
  // play notes from F#-0 (30) to F#-5 (90):
  for (note = 30; note < 90; note ++) {
    //Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
    noteOn(0x90, note, 0x45);
    delay(100);
    //Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
    noteOn(0x90, note, 0x00); 
    delay(100);
  }
}

//  plays a MIDI note.  Doesn't check to see that
//  cmd is greater than 127, or that data values are  less than 127:
void noteOn(byte cmd, byte data1, byte data2) {
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);
}


!!! Allow a Person to Play Notes

The previous example will just play notes, no interactivity.  The example below uses an analog input to set the pitch, and a digital input (a switch) to start and stop the note:

Deleted lines 97-98:
 %color=#7e7e7e%// Variables:
 %color=#cc6600%char%% note = 0;            %color=#7e7e7e%// The MIDI note value to be played
Changed lines 99-102 from:
 %color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
 
%color=#7e7e7e%//  Set MIDI baud rate:
   %color=#cc6600%Serial%%.%color=#cc6600%begin%%(31250);
 }
to:
 const %color=#cc6600%int%% switchPin = 10;  %color=#7e7e7e%// The switch is on Arduino pin 10
 const
%color=#cc6600%int%% middleC = 60;    %color=#7e7e7e%// Middle C (MIDI note value 60) is the lowest note we'll play
 const %color=#cc6600%int%% LEDpin = 13;    %color=#7e7e7e%//  Indicator LED
Added lines 103-118:
 %color=#7e7e7e%// Variables:
 %color=#cc6600%byte%% note = 0;              %color=#7e7e7e%// The MIDI note value to be played
 %color=#cc6600%int%% AnalogValue = 0;        %color=#7e7e7e%// value from the analog input
 %color=#cc6600%int%% lastNotePlayed = 0;    %color=#7e7e7e%// note turned on when you press the switch
 %color=#cc6600%int%% lastSwitchState = 0;    %color=#7e7e7e%// state of the switch during previous time through the main loop
 %color=#cc6600%int%% currentSwitchState = 0;
 
 %color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
  %color=#7e7e7e%//  set the states of the I/O pins:
  %color=#cc6600%pinMode%%(switchPin, %color=#006699%INPUT%%);
  %color=#cc6600%pinMode%%(LEDpin, %color=#006699%OUTPUT%%);
  %color=#7e7e7e%//  Set MIDI baud rate:
  %color=#cc6600%Serial%%.%color=#cc6600%begin%%(31250);
  %color=#cc6600%blink%%(3);
 }
 
Changed lines 120-127 from:
   %color=#7e7e7e%// play notes from F#-0 (30) to F#-5 (90):
  %color
=#cc6600%for%% (note = 30; note < 90; note ++) {
    %color
=#7e7e7e%//Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
    noteOn(0x90, note, 0x45);
 
   %color=#cc6600%delay%%(100);
    %color=#7e7e7e%//Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
    noteOn(0x90, note, 0x00); 
    %color=#cc6600%delay%%(100);
to:
   %color=#7e7e7e%//  My potentiometer gave a range from 0 to 1023:
  AnalogValue
= %color=#cc6600%analogRead%%(0);
   %color=#7e7e7e%//  convert to a range from 0 to 127:
  note
= AnalogValue/8;
  currentSwitchState = %color=#cc6600%digitalRead%%
(switchPin);
  %color=#7e7e7e%// Check to see that the switch is pressed:
  %color=#cc6600%if%% (currentSwitchState == 1) {
     %color=#7e7e7e%//  check to see that the switch wasn't pressed last time
    %color=#7e7e7e%//  through the main loop:
    %color=#cc6600%if%% (lastSwitchState == 0) {
      %color=#7e7e7e%// set the note value based on the analog value, plus a couple octaves:
      %color=#7e7e7e%// note = note + 60
;
      %color=#7e7e7e%// start a note playing:
      noteOn(0x90, note, 0x40);
      %color=#7e7e7e%// save the note we played, so we can turn it off:
      lastNotePlayed = note;
      %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%HIGH%%);
    }
Changed lines 139-146 from:
 }
to:
   %color=#cc6600%else%% {  %color=#7e7e7e%// if the switch is not pressed:
    %color=#7e7e7e%//  but the switch was pressed last time through the main loop:
    %color=#cc6600%if%% (lastSwitchState == 1) {
      %color=#7e7e7e%//  stop the last note played:
      noteOn(0x90, lastNotePlayed, 0x00);
      %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%LOW%%);
    }
 
}
Added lines 148-152:
   %color=#7e7e7e%//  save the state of the switch for next time
  %color=#7e7e7e%//  through the main loop:
  lastSwitchState = currentSwitchState;
 }
 
Changed line 155 from:
 %color=#cc6600%void%% noteOn(%color=#cc6600%char%% cmd, %color=#cc6600%char%% data1, %color=#cc6600%char%% data2) {
to:
 %color=#cc6600%void%% noteOn(%color=#cc6600%byte%% cmd, %color=#cc6600%byte%% data1, %color=#cc6600%byte%%  data2) {
Added lines 161-170:
 %color=#7e7e7e%// Blinks an LED 3 times
 %color=#cc6600%void%% %color=#cc6600%blink%%(%color=#cc6600%int%% howManyTimes) {
  %color=#cc6600%int%% i;
  %color=#cc6600%for%% (i=0; i< howManyTimes; i++) {
    %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%HIGH%%);
    %color=#cc6600%delay%%(100);
    %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%LOW%%);
    %color=#cc6600%delay%%(100);
  }
 }
Deleted lines 172-252:
!!! Allow a Person to Play Notes

The previous example will just play notes, no interactivity.  The example below uses an analog input to set the pitch, and a digital input (a switch) to start and stop the note:

(:div class=code :)
 
 
 const %color=#cc6600%int%% switchPin = 10;  %color=#7e7e7e%// The switch is on Arduino pin 10
 const %color=#cc6600%int%% middleC = 60;    %color=#7e7e7e%// Middle C (MIDI note value 60) is the lowest note we'll play
 const %color=#cc6600%int%% LEDpin = 13;    %color=#7e7e7e%//  Indicator LED
 
 %color=#7e7e7e%// Variables:
 %color=#cc6600%byte%% note = 0;              %color=#7e7e7e%// The MIDI note value to be played
 %color=#cc6600%int%% AnalogValue = 0;        %color=#7e7e7e%// value from the analog input
 %color=#cc6600%int%% lastNotePlayed = 0;    %color=#7e7e7e%// note turned on when you press the switch
 %color=#cc6600%int%% lastSwitchState = 0;    %color=#7e7e7e%// state of the switch during previous time through the main loop
 %color=#cc6600%int%% currentSwitchState = 0;
 
 %color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
  %color=#7e7e7e%//  set the states of the I/O pins:
  %color=#cc6600%pinMode%%(switchPin, %color=#006699%INPUT%%);
  %color=#cc6600%pinMode%%(LEDpin, %color=#006699%OUTPUT%%);
  %color=#7e7e7e%//  Set MIDI baud rate:
  %color=#cc6600%Serial%%.%color=#cc6600%begin%%(31250);
  %color=#cc6600%blink%%(3);
 }
 
 %color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
  %color=#7e7e7e%//  My potentiometer gave a range from 0 to 1023:
  AnalogValue = %color=#cc6600%analogRead%%(0);
  %color=#7e7e7e%//  convert to a range from 0 to 127:
  note = AnalogValue/8;
  currentSwitchState = %color=#cc6600%digitalRead%%(switchPin);
  %color=#7e7e7e%// Check to see that the switch is pressed:
  %color=#cc6600%if%% (currentSwitchState == 1) {
    %color=#7e7e7e%//  check to see that the switch wasn't pressed last time
    %color=#7e7e7e%//  through the main loop:
    %color=#cc6600%if%% (lastSwitchState == 0) {
      %color=#7e7e7e%// set the note value based on the analog value, plus a couple octaves:
      %color=#7e7e7e%// note = note + 60;
      %color=#7e7e7e%// start a note playing:
      noteOn(0x90, note, 0x40);
      %color=#7e7e7e%// save the note we played, so we can turn it off:
      lastNotePlayed = note;
      %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%HIGH%%);
    }
  }
  %color=#cc6600%else%% {  %color=#7e7e7e%// if the switch is not pressed:
    %color=#7e7e7e%//  but the switch was pressed last time through the main loop:
    %color=#cc6600%if%% (lastSwitchState == 1) {
      %color=#7e7e7e%//  stop the last note played:
      noteOn(0x90, lastNotePlayed, 0x00);
      %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%LOW%%);
    }
  }
 
  %color=#7e7e7e%//  save the state of the switch for next time
  %color=#7e7e7e%//  through the main loop:
  lastSwitchState = currentSwitchState;
 }
 
 %color=#7e7e7e%//  plays a MIDI note.  Doesn't check to see that
 %color=#7e7e7e%//  cmd is greater than 127, or that data values are  less than 127:
 %color=#cc6600%void%% noteOn(%color=#cc6600%byte%% cmd, %color=#cc6600%byte%% data1, %color=#cc6600%byte%%  data2) {
  %color=#cc6600%Serial%%.%color=#cc6600%print%%(cmd, %color=#006699%BYTE%%);
  %color=#cc6600%Serial%%.%color=#cc6600%print%%(data1, %color=#006699%BYTE%%);
  %color=#cc6600%Serial%%.%color=#cc6600%print%%(data2, %color=#006699%BYTE%%);
 }
 
 %color=#7e7e7e%// Blinks an LED 3 times
 %color=#cc6600%void%% %color=#cc6600%blink%%(%color=#cc6600%int%% howManyTimes) {
  %color=#cc6600%int%% i;
  %color=#cc6600%for%% (i=0; i< howManyTimes; i++) {
    %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%HIGH%%);
    %color=#cc6600%delay%%(100);
    %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%LOW%%);
    %color=#cc6600%delay%%(100);
  }
 }
 
(:divend:)
Changed lines 102-107 from:
 %color=#7e7e7e%// The switch is on Arduino pin 10:
 const
%color=#cc6600%int%% switchPin = 10;
 
%color=#7e7e7e%// Middle C (MIDI note value 60) is the lowest note we'll play:
 const %color=#cc6600%int%% middleC = 60;
 
%color=#7e7e7e%//  Indicator LED:
 const
%color=#cc6600%int%% LEDpin = 13;
to:
 const %color=#cc6600%int%% switchPin = 10;  %color=#7e7e7e%// The switch is on Arduino pin 10
 const
%color=#cc6600%int%% middleC = 60;    %color=#7e7e7e%// Middle C (MIDI note value 60) is the lowest note we'll play
 const
%color=#cc6600%int%% LEDpin = 13;    %color=#7e7e7e%//  Indicator LED
Changed lines 107-110 from:
 %color=#cc6600%byte%% note = 0;            %color=#7e7e7e%// The MIDI note value to be played
 %color=#cc6600%int%% AnalogValue = 0;           %color=#7e7e7e%// value from the analog input
 %color=#cc6600%int%% lastNotePlayed = 0;  %color=#7e7e7e%// note turned on when you press the switch
 %color=#cc6600%int%% lastSwitchState = 0;  %color=#7e7e7e%// state of the switch during previous time through the main loop
to:
 %color=#cc6600%byte%% note = 0;              %color=#7e7e7e%// The MIDI note value to be played
 %color=#cc6600%int%% AnalogValue = 0;        %color=#7e7e7e%// value from the analog input
 %color=#cc6600%int%% lastNotePlayed = 0;    %color=#7e7e7e%// note turned on when you press the switch
 %color=#cc6600%int%% lastSwitchState = 0;    %color=#7e7e7e%// state of the switch during previous time through the main loop
Changed lines 99-104 from:
 // The switch is on Arduino pin 10:
 const int switchPin = 10;
 // Middle C (MIDI note value 60) is the lowest note we'll play:
 const int middleC = 60;
 //  Indicator LED:
 const int LEDpin = 13;
to:
(:div class=code :)
 
Changed lines 102-107 from:
 // Variables:
 byte note = 0; 
          // The MIDI note value to be played
 int AnalogValue
= 0;          // value from the analog input
 int lastNotePlayed = 0;  // note turned on when you press the switch
 int lastSwitchState = 0;  // state of the switch during previous time through the main loop
 int currentSwitchState = 0
;
to:
 %color=#7e7e7e%// The switch is on Arduino pin 10:
 const %color=#cc6600%int%% switchPin = 10;
 %color=#7e7e7e%// Middle C (MIDI note value 60) is the lowest note we'll play:
 const %color
=#cc6600%int%% middleC = 60;
 %color=#7e7e7e%//
  Indicator LED:
 const %color=#cc6600%int%% LEDpin = 13
;
Changed lines 109-116 from:
 void setup() {
   //  set the states of the I/O pins:
  pinMode(switchPin, INPUT);
   pinMode(LEDpin, OUTPUT);
  //  Set MIDI baud rate:
  Serial.begin(31250);
  blink(3);
 }
to:
 %color=#7e7e7e%// Variables:
 %color=#cc6600%byte%% note = 0;          %color=#7e7e7e%// The MIDI note value to be played
 %color=#cc6600%int%% AnalogValue = 0;
         %color=#7e7e7e%// value from the analog input
 %color=#cc6600%int%% lastNotePlayed = 0;  %color=#7e7e7e%// note turned on when you press the switch
 %color=#cc6600%int%% lastSwitchState = 0;  %color=#7e7e7e%// state of the switch during previous time through the main loop
 %color=#cc6600%int%% currentSwitchState = 0;
Changed lines 116-119 from:
 void loop() {
  //  My potentiometer gave a range from 0 to 1023:
  AnalogValue
= analogRead(0);
  //  convert to a range from 0 to 127:
to:
 %color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
  %color=#7e7e7e%//  set the states of the I/O pins:
  %color
=#cc6600%pinMode%%(switchPin, %color=#006699%INPUT%%);
   %color=#cc6600%pinMode%%(LEDpin, %color=#006699%OUTPUT%%);
  %color=#7e7e7e%//  Set MIDI baud rate:
  %color=#cc6600%Serial%%.%color=#cc6600%begin%%(31250);
  %color=#cc6600%blink%%(3);
 }
 
 %color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
  %color=#7e7e7e%//  My potentiometer gave a range from 0 to 1023:
  AnalogValue = %color=#cc6600%analogRead%%(0);
  %color=#7e7e7e%
//  convert to a range from 0 to 127:
Changed lines 130-138 from:
   currentSwitchState = digitalRead(switchPin);
  // Check to see that the switch is pressed:
  if (currentSwitchState == 1) {
    //  check to see that the switch wasn't pressed last time
    //  through the main loop:
    if (lastSwitchState == 0) {
      // set the note value based on the analog value, plus a couple octaves:
      // note = note + 60;
      // start a note playing:
to:
   currentSwitchState = %color=#cc6600%digitalRead%%(switchPin);
  %color=#7e7e7e%// Check to see that the switch is pressed:
  %color=#cc6600%if%% (currentSwitchState == 1) {
    %color=#7e7e7e%//  check to see that the switch wasn't pressed last time
    %color=#7e7e7e%//  through the main loop:
    %color=#cc6600%if%% (lastSwitchState == 0) {
      %color=#7e7e7e%// set the note value based on the analog value, plus a couple octaves:
      %color=#7e7e7e%// note = note + 60;
      %color=#7e7e7e%// start a note playing:
Changed line 140 from:
       // save the note we played, so we can turn it off:
to:
       %color=#7e7e7e%// save the note we played, so we can turn it off:
Changed line 142 from:
       digitalWrite(LEDpin, HIGH);
to:
       %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%HIGH%%);
Changed lines 145-148 from:
   else {  // if the switch is not pressed:
    //  but the switch was pressed last time through the main loop:
    if (lastSwitchState == 1) {
      //  stop the last note played:
to:
   %color=#cc6600%else%%%color=#7e7e7e%// if the switch is not pressed:
    %color=#7e7e7e%//  but the switch was pressed last time through the main loop:
    %color=#cc6600%if%% (lastSwitchState == 1) {
      %color=#7e7e7e%//  stop the last note played:
Changed line 150 from:
       digitalWrite(LEDpin, LOW);
to:
       %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%LOW%%);
Changed lines 154-155 from:
   //  save the state of the switch for next time
  //  through the main loop:
to:
   %color=#7e7e7e%//  save the state of the switch for next time
  %color=#7e7e7e%//  through the main loop:
Changed lines 159-164 from:
 //  plays a MIDI note.  Doesn't check to see that
 //  cmd is greater than 127, or that data values are  less than 127:
 void noteOn(byte cmd, byte data1, byte  data2) {
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);
to:
 %color=#7e7e7e%//  plays a MIDI note.  Doesn't check to see that
 %color=#7e7e7e%//  cmd is greater than 127, or that data values are  less than 127:
 %color=#cc6600%void%% noteOn(%color=#cc6600%byte%% cmd, %color=#cc6600%byte%% data1, %color=#cc6600%byte%%  data2) {
  %color=#cc6600%Serial%%.%color=#cc6600%print%%(cmd, %color=#006699%BYTE%%);
  %color=#cc6600%Serial%%.%color=#cc6600%print%%(data1, %color=#006699%BYTE%%);
  %color=#cc6600%Serial%%.%color=#cc6600%print%%(data2, %color=#006699%BYTE%%);
Changed lines 167-174 from:
 // Blinks an LED 3 times
 void blink(int howManyTimes) {
  int i;
  for (i=0; i< howManyTimes; i++) {
    digitalWrite(LEDpin, HIGH);
    delay(100);
    digitalWrite(LEDpin, LOW);
    delay(100);
to:
 %color=#7e7e7e%// Blinks an LED 3 times
 %color=#cc6600%void%% %color=#cc6600%blink%%(%color=#cc6600%int%% howManyTimes) {
  %color=#cc6600%int%% i;
  %color=#cc6600%for%% (i=0; i< howManyTimes; i++) {
    %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%HIGH%%);
    %color=#cc6600%delay%%(100);
    %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%LOW%%);
    %color=#cc6600%delay%%(100);
Changed line 178 from:
to:
(:divend:)
Added line 62:
Changed lines 98-99 from:
(:div class=code :)
 
to:

 // The switch is on Arduino pin 10:
 const int switchPin = 10;
 // Middle C (MIDI note value 60) is the lowest note we'll play:
 const int middleC = 60;
 //  Indicator LED:
 const int LEDpin = 13;
Changed lines 106-111 from:
 %color=#7e7e7e%// The switch is on Arduino pin 10:
 #define switchPin 10
 %color=#7e7e7e%// Middle C (MIDI note value 60) is the lowest
note we'll play:
 #define middleC 60
 %color
=#7e7e7e%//  Indicator LED:
 #define LEDpin 13
to:
 // Variables:
 byte note = 0;
           // The MIDI note value to be played
 int AnalogValue
= 0;           // value from the analog input
 int lastNotePlayed = 0;  // note turned on when you press the switch
 int lastSwitchState = 0;  // state of the switch during previous time through the main loop
 int currentSwitchState = 0;
Changed lines 113-118 from:
 %color=#7e7e7e%// Variables:
 %color=#cc6600%char%% note = 0;          %color=#7e7e7e%// The MIDI note value to be played
 %color=#cc6600%int%% AnalogValue = 0;
         %color=#7e7e7e%// value from the analog input
 %color=#cc6600%int%% lastNotePlayed = 0;  %color=#7e7e7e%// note turned on when you press the switch
 %color=#cc6600%int%% lastSwitchState = 0;  %color=#7e7e7e%// state of the switch during previous time through the main loop
 %color=#cc6600%int%% currentSwitchState = 0;
to:
 void setup() {
   //  set the states of the I/O pins:
  pinMode(switchPin, INPUT);
   pinMode(LEDpin, OUTPUT);
  //  Set MIDI baud rate:
  Serial.begin(31250);
  blink(3);
 }
Changed lines 122-134 from:
 %color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
  %color=#7e7e7e%//  set the states of the I/O pins:
  %color
=#cc6600%pinMode%%(switchPin, %color=#006699%INPUT%%);
   %color=#cc6600%pinMode%%(LEDpin, %color=#006699%OUTPUT%%);
  %color=#7e7e7e%//  Set MIDI baud rate:
  %color=#cc6600%Serial%%.%color=#cc6600%begin%%(31250);
  %color=#cc6600%blink%%(3);
 }
 
 %color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
  %color=#7e7e7e%//  My potentiometer gave a range from 0 to 1023:
  AnalogValue = %color=#cc6600%analogRead%%(0);
  %color=#7e7e7e%
//  convert to a range from 0 to 127:
to:
 void loop() {
  //  My potentiometer gave a range from 0 to 1023:
  AnalogValue
= analogRead(0);
  //  convert to a range from 0 to 127:
Changed lines 127-135 from:
   currentSwitchState = %color=#cc6600%digitalRead%%(switchPin);
  %color=#7e7e7e%// Check to see that the switch is pressed:
  %color=#cc6600%if%% (currentSwitchState == 1) {
    %color=#7e7e7e%//  check to see that the switch wasn't pressed last time
    %color=#7e7e7e%//  through the main loop:
    %color=#cc6600%if%% (lastSwitchState == 0) {
      %color=#7e7e7e%// set the note value based on the analog value, plus a couple octaves:
      %color=#7e7e7e%// note = note + 60;
      %color=#7e7e7e%// start a note playing:
to:
   currentSwitchState = digitalRead(switchPin);
  // Check to see that the switch is pressed:
  if (currentSwitchState == 1) {
    //  check to see that the switch wasn't pressed last time
    //  through the main loop:
    if (lastSwitchState == 0) {
      // set the note value based on the analog value, plus a couple octaves:
       // note = note + 60;
      // start a note playing:
Changed line 137 from:
       %color=#7e7e7e%// save the note we played, so we can turn it off:
to:
       // save the note we played, so we can turn it off:
Changed line 139 from:
       %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%HIGH%%);
to:
       digitalWrite(LEDpin, HIGH);
Changed lines 142-145 from:
     %color=#cc6600%else%% {   %color=#7e7e7e%// if the switch is not pressed:
    %color=#7e7e7e%//  but the switch was pressed last time through the main loop:
    %color=#cc6600%if%% (lastSwitchState == 1) {
      %color=#7e7e7e%//  stop the last note played:
to:
   else {  // if the switch is not pressed:
    //  but the switch was pressed last time through the main loop:
    if (lastSwitchState == 1) {
      //  stop the last note played:
Changed line 147 from:
       %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%LOW%%);
to:
       digitalWrite(LEDpin, LOW);
Changed line 149 from:
 }
to:
  }
Changed lines 151-152 from:
   %color=#7e7e7e%//  save the state of the switch for next time
  %color=#7e7e7e%//  through the main loop:
to:
   //  save the state of the switch for next time
  //  through the main loop:
Changed lines 156-161 from:
 %color=#7e7e7e%//  plays a MIDI note.  Doesn't check to see that
 %color=#7e7e7e%//  cmd is greater than 127, or that data values are  less than 127:
 %color=#cc6600%void%% noteOn(%color=#cc6600%char%% cmd, %color=#cc6600%char%% data1, %color=#cc6600%char%% data2) {
  %color=#cc6600%Serial%%.%color=#cc6600%print%%(cmd, %color=#006699%BYTE%%);
  %color=#cc6600%Serial%%.%color=#cc6600%print%%(data1, %color=#006699%BYTE%%);
  %color=#cc6600%Serial%%.%color=#cc6600%print%%(data2, %color=#006699%BYTE%%);
to:
 //  plays a MIDI note.  Doesn't check to see that
 //  cmd is greater than 127, or that data values are  less than 127:
 void noteOn(byte cmd, byte data1, byte  data2) {
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);
Changed lines 164-171 from:
 %color=#7e7e7e%// Blinks an LED 3 times
 %color=#cc6600%void%% %color=#cc6600%blink%%(%color=#cc6600%int%% howManyTimes) {
  %color=#cc6600%int%% i;
  %color=#cc6600%for%% (i=0; i< howManyTimes; i++) {
    %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%HIGH%%);
    %color=#cc6600%delay%%(100);
    %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%LOW%%);
    %color=#cc6600%delay%%(100);
to:
 // Blinks an LED 3 times
 void blink(int howManyTimes) {
  int i;
  for (i=0; i< howManyTimes; i++) {
    digitalWrite(LEDpin, HIGH);
    delay(100);
    digitalWrite(LEDpin, LOW);
    delay(100);
Changed lines 175-176 from:
(:divend:)
to:
Changed lines 62-92 from:
[@

// Variables
: 
char note = 0;
          // The MIDI note value to be played

void setup() {
  //  Set MIDI baud rate:
  Serial.begin(31250);
}

void loop() {
 
// play notes from F#-0 (30) to F#-5 (90):
  for (note
= 30; note < 90; note ++) {
   //Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
    noteOn(0x90, note, 0x45);
    delay(100);
   //Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
    noteOn(0x90, note, 0x00); 
    delay(100);
  }
}

//
  plays a MIDI note.  Doesn't check to see that
//  cmd is greater than 127, or that data values are
  less than 127:
void noteOn(char cmd, char data1, char data2) {
  Serial.print(cmd, BYTE);
  Serial
.print(data1, BYTE);
  Serial.print(data2, BYTE
);
}

@]
to:
(:div class=code :)
 
 %color=#7e7e7e%// Variables:

 %color=#cc6600%char%% note = 0;           %color=#7e7e7e%// The MIDI note value to be played
 
 %color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
 
  %color=#7e7e7e%//  Set MIDI baud rate:
  %color
=#cc6600%Serial%%.%color=#cc6600%begin%%(31250);
 }

 %color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
  %color=#7e7e7e%// play notes from F#-0 (30) to F#-5 (90):
  %color=#cc6600%for%%
(note = 30; note < 90; note ++) {
    %color=#7e7e7e%//Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
     noteOn(0x90, note, 0x45);
    %color=#cc6600%delay%%(100);
     %color=#7e7e7e%//Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
    noteOn(0x90, note, 0x00);
 
    %color=#cc6600%delay%%(100);
  }
 }

 %color=#7e7e7e%//  plays a MIDI note.  Doesn't check to see that
 %color=#7e7e7e%//  cmd is greater than 127, or that data values are  less than 127:
 %color=#cc6600%void%% noteOn(%color=#cc6600%char%% cmd, %color=#cc6600%char%% data1, %color=#cc6600%char%% data2
) {
  %color=#cc6600%Serial%%.%color=#cc6600%print%%(cmd, %color=#006699%BYTE%%)
;
   %color=#cc6600%Serial%%.%color=#cc6600%print%%(data1, %color=#006699%BYTE%%);
  %color=#cc6600%Serial%%.%color=#cc6600%print%%(data2, %color=#006699%BYTE%%);
 
}
 
 
(:divend:)
Changed lines 97-177 from:

[@

// The switch is on Arduino pin 10:
#define switchPin 10
// Middle C (MIDI note value 60) is the lowest note we'll play:
#define middleC
60
//  Indicator LED:
#define LEDpin 13

// Variables
:
char note = 0;          // The MIDI note value to be played
int AnalogValue = 0;
         // value from the analog input
int lastNotePlayed = 0;
  // note turned on when you press the switch
int lastSwitchState
= 0;  // state of the switch during previous time through the main loop
int currentSwitchState = 0;

void setup() {
  //  set the states of
the I/O pins:
  pinMode(switchPin, INPUT);
  pinMode(LEDpin, OUTPUT);
  //  Set MIDI baud rate:
  Serial.begin(31250);
  blink(3);
}

void loop() {
  //  My potentiometer gave a range from 0 to 1023:
  AnalogValue = analogRead(0);
  //  convert to a range from 0 to 127:
  note
= AnalogValue/8;
  currentSwitchState
= digitalRead(switchPin);
  // Check to see that the switch is pressed:
  if (currentSwitchState
== 1) {
   //  check to see that the switch wasn't pressed last time
    //  through the main loop:
    if (lastSwitchState == 0) {
      // set the note value based on the analog value, plus a couple octaves:
 
  // note = note + 60;
      // start a note playing:
      noteOn(0x90, note, 0x40);
      // save the note we played, so we can turn it off:
 
    lastNotePlayed = note;
     digitalWrite(LEDpin, HIGH);
    }
  }
 
  else {   // if the switch is not pressed:
    //  but the switch was pressed last time through the main loop:
    if (lastSwitchState == 1) {
     //  stop the last note played:
      noteOn(0x90, lastNotePlayed, 0x00);
      digitalWrite(LEDpin, LOW);
    }
}

  //  save the state of the switch for next time
  //  through the main loop
:
  lastSwitchState = currentSwitchState;
}

//
  plays a MIDI note.  Doesn't check to see that
//  cmd is greater than 127, or that data values are
  less than 127:
void noteOn(char cmd, char data1, char data2) {
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);
}

// Blinks an LED 3 times
void blink(int howManyTimes) {
  int i;
  for (i=0; i< howManyTimes; i++) {
    digitalWrite(LEDpin, HIGH);
    delay(100);
 
  digitalWrite(LEDpin, LOW);
    delay(100);
  }
}

@]
to:
(:div class=code :)
 
 
 %color=
#7e7e7e%// The switch is on Arduino pin 10:
 #define switchPin 10
 %color=#7e7e7e%// Middle C (MIDI note value
60) is the lowest note we'll play:
 #define middleC 60
 %color=#7e7e7e%//
  Indicator LED:
 #define LEDpin 13

 %color=#7e7e7e%// Variables:
 %color=#cc6600%char%% note = 0;            %color=#7e7e7e%// The MIDI note value to be played
 %color=#cc6600%int%% AnalogValue = 0; 
         %color=#7e7e7e%// value from the analog input
 %color
=#cc6600%int%% lastNotePlayed = 0;  %color=#7e7e7e%// note turned on when you press the switch
 %color=#cc6600%
int%% lastSwitchState = 0;  %color=#7e7e7e%// state of the switch during previous time through the main loop
 %color=#cc6600%int%% currentSwitchState = 0
;
 
 %color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
  %color=#7e7e7e%//  set the states of the I/O pins:
  %color=#cc6600%pinMode%%(switchPin, %color=#006699%INPUT%%);
  %color=#cc6600%pinMode%%(LEDpin, %color=#006699%OUTPUT%%);
  %color=#7e7e7e%//  Set MIDI baud rate:
 
  %color=#cc6600%Serial%%.%color=#cc6600%begin%%(31250);
  %color=#cc6600%blink%%(3);
 }
 
 %color=#cc6600%void%% %color
=#cc6600%'''loop'''%%() {
  %color=#7e7e7e%//  My potentiometer gave a range from 0 to 1023:
 
  AnalogValue = %color=#cc6600%analogRead%%(0);
   %color=#7e7e7e%//  convert to a range from 0 to 127:
  note = AnalogValue/8;
  currentSwitchState = %color=#cc6600%digitalRead%%(switchPin);
  %color=#7e7e7e%// Check to see that the switch is pressed:
  %color=#cc6600%if%% (currentSwitchState == 1) {
    %color=#7e7e7e%//  check to see that the switch wasn't pressed last time
 
    %color=#7e7e7e%//  through the main loop:
    %color=#cc6600%if%% (lastSwitchState == 0) {
       %color=#7e7e7e%// set the note value based on the analog value, plus a couple octaves:
    %color=#7e7e7e%// note = note + 60;
 
      %color=#7e7e7e%// start a note playing:
      noteOn(0x90, note, 0x40);
 
    %color=#7e7e7e%// save the note we played, so we can turn it off:
      lastNotePlayed = note;
       %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%HIGH%%);
    }
  }
    %color=#cc6600%else%% {  %color=#7e7e7e%// if the switch is not pressed
:
     %color=#7e7e7e%//  but the switch was pressed last time through the main loop:
    %color=#cc6600%if%% (lastSwitchState == 1) {
       %color=#7e7e7e%//  stop the last note played:
 
      noteOn(0x90, lastNotePlayed, 0x00);
      %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%LOW%%);
     }
 }
 
  %color=#7e7e7e%//
  save the state of the switch for next time
  %color=#7e7e7e%//  through the main loop:
   lastSwitchState = currentSwitchState;
 
}
 
 %color=#7e7e7e%//  plays a MIDI note.  Doesn't check to see that
 %color=#7e7e7e%//  cmd is greater than 127, or that data values are  less than 127:
 %color=#cc6600%void%% noteOn(%color=#cc6600%char%% cmd, %color=#cc6600%char%% data1, %color=#cc6600%char%% data2) {
  %color=#cc6600%Serial%%.%color=#cc6600%print%%(cmd, %color=#006699%BYTE%%);
  %color=#cc6600%Serial%%.%color=#cc6600%print%%(data1, %color=#006699%BYTE%%);
  %color=#cc6600%Serial%%.%color=#cc6600%print%%(data2, %color=#006699%BYTE%%);
 
}
 
 %color=#7e7e7e%// Blinks an LED 3 times
 %color=#cc6600%void%% %color=#cc6600%blink%%(%color=#cc6600%int%% howManyTimes) {
  %color=#cc6600%int%% i;
  %color=#cc6600%for%% (i=0; i< howManyTimes; i++) {
    %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%HIGH%%);
    %color=#cc6600%delay%%(100);
    %color=#cc6600%digitalWrite%%(LEDpin, %color=#006699%LOW%%);
    %color=#cc6600%delay%%(100);
  }
 }
 
(:divend:)

Added lines 3-4:
!!!Overview
Changed lines 11-12 from:
!!! Parts
to:
!!!Parts
Added lines 1-2:
(:title MIDI Output using an Arduino:)
Added lines 7-10:
(:toc Table of Contents:)

!!! Parts

Deleted lines 29-32:
If you're using an Arduino breadboard shield, there is a row of sockets connected to 5V on the analog in side of the breadboard, and a row connected to ground on the digital in side of the board:

%alt='Arduino breadboard shield' align=top valign=center%http://itp.nyu.edu/physcomp/images/labs/breadboard_shield.jpg

Changed line 42 from:
(:cellnr colspan=2:)
to:
(:cell:)
Deleted lines 43-45:
(:cellnr:)
Breadboard version
%height=300 alt='arduino midi circuit on a breadboard.'%http://www.itp.nyu.edu/physcomp/images/labs/bboard_midi_sensors.JPG
Changed lines 45-46 from:
Breadboard shield version
%height=300 alt='arduino midi circuit on an Arduino
breadboard shield'%http://www.itp.nyu.edu/physcomp/images/labs/bboard_shield_midi_sensors.JPG
to:
%height=300 alt='arduino midi circuit on a breadboard.'%http://www.itp.nyu.edu/physcomp/images/labs/bboard_midi_sensors.JPG
Added lines 41-44:
!!! Build the MIDI Circuit

The MIDI out schematic for Arduino looks like this:

Changed line 47 from:
%height=300 alt='arduino midi schematic'%http://www.itp.nyu.edu/physcomp/images/labs/arduino_midi_sensors_schem.png
to:
%height=300 alt='arduino midi schematic'%http://www.itp.nyu.edu/physcomp/images/labs/arduino_midi_schem.png
Changed line 50 from:
%height=300 alt='arduino midi circuit on a breadboard.'%http://www.itp.nyu.edu/physcomp/images/labs/bboard_midi_sensors_2.JPG
to:
%height=300 alt='arduino midi circuit on a breadboard.'%http://www.itp.nyu.edu/physcomp/images/labs/bboard_midi_sensors.JPG
Changed line 53 from:
%height=300 alt='arduino midi circuit on an Arduino breadboard shield'%http://www.itp.nyu.edu/physcomp/images/labs/bboard_shield_midi_sensors_2.JPG
to:
%height=300 alt='arduino midi circuit on an Arduino breadboard shield'%http://www.itp.nyu.edu/physcomp/images/labs/bboard_shield_midi_sensors.JPG
Deleted lines 55-69:
!!! Build the MIDI Circuit

The MIDI out schematic for Arduino looks like this:

(:table:)
(:cellnr colspan=2:)
%height=300 alt='arduino midi schematic'%http://www.itp.nyu.edu/physcomp/images/labs/arduino_midi_schem.png
(:cellnr:)
Breadboard version
%height=300 alt='arduino midi circuit on a breadboard.'%http://www.itp.nyu.edu/physcomp/images/labs/bboard_midi_sensors.JPG
(:cell:)
Breadboard shield version
%height=300 alt='arduino midi circuit on an Arduino breadboard shield'%http://www.itp.nyu.edu/physcomp/images/labs/bboard_shield_midi_sensors.JPG
(:tableend:)

Added line 152:
  }
Changed lines 159-160 from:
    }
  }
to:
   }
Deleted line 66:
Changed line 153 from:
   else    // if the switch is not pressed:
to:
   else {  // if the switch is not pressed:
Changed lines 161-162 from:
to:
}
Changed line 12 from:
%lframe width=100px% [[http://itp.nyu.edu/physcomp/images/labs/midi_connector.jpg | http://itp.nyu.edu/physcomp/images/labs/midi_connector.jpg"MIDi connector"]] | [-MIDI connector-]
to:
%lframe width=100px% [[http://itp.nyu.edu/physcomp/images/labs/midi_connector.JPG | http://itp.nyu.edu/physcomp/images/labs/midi_connector.JPG"MIDI connector"]] | [-MIDI connector-]
Added lines 11-12:

%lframe width=100px% [[http://itp.nyu.edu/physcomp/images/labs/midi_connector.jpg | http://itp.nyu.edu/physcomp/images/labs/midi_connector.jpg"MIDi connector"]] | [-MIDI connector-]
Added lines 17-18:
%lframe width=100px valign=center% [[http://itp.nyu.edu/physcomp/images/labs/resistors.jpg | http://itp.nyu.edu/physcomp/images/labs/resistors.jpg"resistors"]] | [-220 ohm resistors-]
Changed line 42 from:
to:
%height=300 alt='arduino midi circuit on a breadboard.'%http://www.itp.nyu.edu/physcomp/images/labs/bboard_midi_sensors_2.JPG
Changed line 45 from:
to:
%height=300 alt='arduino midi circuit on an Arduino breadboard shield'%http://www.itp.nyu.edu/physcomp/images/labs/bboard_shield_midi_sensors_2.JPG
Added lines 52-53:
(:table:)
(:cellnr colspan=2:)
Changed lines 55-63 from:
to:
(:cellnr:)
Breadboard version
%height=300 alt='arduino midi circuit on a breadboard.'%http://www.itp.nyu.edu/physcomp/images/labs/bboard_midi_sensors.JPG
(:cell:)
Breadboard shield version
%height=300 alt='arduino midi circuit on an Arduino breadboard shield'%http://www.itp.nyu.edu/physcomp/images/labs/bboard_shield_midi_sensors.JPG
(:tableend:)

Changed line 39 from:
to:
%height=300 alt='arduino midi schematic'%http://www.itp.nyu.edu/physcomp/images/labs/arduino_midi_sensors_schem.png
Changed line 39 from:
%height=300 alt='schematic: arduino, 2 analog sensors, 1 switch'%http://itp.nyu.edu/physcomp/images/labs/arduino_2_analog_1_dig_schem.png
to:
Changed line 42 from:
http://itp.nyu.edu/physcomp/images/labs/bboard_3_sensors.JPG
to:
Changed line 45 from:
http://itp.nyu.edu/physcomp/images/labs/bboard_shield_3_sensors.JPG
to:
Added lines 5-47:
For this lab you'll need:

%lframe width=100px% [[http://itp.nyu.edu/physcomp/images/labs/breadboard.jpg | http://itp.nyu.edu/physcomp/images/labs/breadboard.jpg"Solderless breadboard"]] | [-Solderless breadboard-]
%lframe width=100px% [[http://itp.nyu.edu/physcomp/images/labs/hookup_wire.jpg | http://itp.nyu.edu/physcomp/images/labs/hookup_wire.jpg"hookup wire"]] | [-22-AWG hookup wire-]
%lframe width=100px% [[http://itp.nyu.edu/physcomp/images/labs/arduino.jpg | http://itp.nyu.edu/physcomp/images/labs/arduino.jpg"Arduino module"]] | [-Arduino Microcontroller \\
module-]
[[<<]]

%lframe width=100px valign=center% [[http://itp.nyu.edu/physcomp/images/labs/resistors.jpg | http://itp.nyu.edu/physcomp/images/labs/resistors.jpg"resistors"]] | [-10Kohm resistors-]

%lframe width=90px valign=center% [[http://itp.nyu.edu/physcomp/images/labs/flex_sensors.jpg | http://itp.nyu.edu/physcomp/images/labs/flex_sensors.jpg"flex sensor"]] | [-Flex sensors\\
(or a different\\
 form of variable resistor)-]
%lframe width=100px valign=center% [[http://itp.nyu.edu/physcomp/images/labs/switch.jpg | http://itp.nyu.edu/physcomp/images/labs/switch.jpg"resistors"]] | [-switch-]
[[<<]]

!!! Prepare the breadboard
Connect power and ground on the breadboard to power and ground from the microcontroller. On the Arduino module, use the 5V and any of the ground connections:

%alt='Arduino connected to a breadboard' align=top valign=center%http://itp.nyu.edu/physcomp/images/labs/arduino_bboard_power.jpg

If you're using an Arduino breadboard shield, there is a row of sockets connected to 5V on the analog in side of the breadboard, and a row connected to ground on the digital in side of the board:

%alt='Arduino breadboard shield' align=top valign=center%http://itp.nyu.edu/physcomp/images/labs/breadboard_shield.jpg

[[<<]]

!!! Connect the sensors

Connect an analog sensor to analog pins 0 like you did in the [[Labs/AnalogIn |analog lab]].  Connect a switch to digital pin 10 like you did in the [[Labs/DigitalInOut |digital lab]].


(:table:)
(:cellnr colspan=2:)
%height=300 alt='schematic: arduino, 2 analog sensors, 1 switch'%http://itp.nyu.edu/physcomp/images/labs/arduino_2_analog_1_dig_schem.png
(:cellnr:)
Breadboard version
http://itp.nyu.edu/physcomp/images/labs/bboard_3_sensors.JPG
(:cell:)
Breadboard shield version
http://itp.nyu.edu/physcomp/images/labs/bboard_shield_3_sensors.JPG
(:tableend:)

Added lines 5-6:
!!! Build the MIDI Circuit
Changed lines 11-12 from:
This circuit doesn't actually match the MIDI specification, but it works with all the MIDI devices we've tried it with. This circuit insludes an analog and a digital sensor to allow for physical interactivity, but those aren't necessary to send MIDI data.
to:
This circuit doesn't actually match the MIDI specification, but it works with all the MIDI devices we've tried it with. This circuit includes an analog and a digital sensor to allow for physical interactivity, but those aren't necessary to send MIDI data.
Changed lines 9-10 from:
This circuit doesn't actually match the MIDI specification, but it works with all the MIDI devices we've tried it with.
to:
This circuit doesn't actually match the MIDI specification, but it works with all the MIDI devices we've tried it with. This circuit insludes an analog and a digital sensor to allow for physical interactivity, but those aren't necessary to send MIDI data.
Changed lines 7-8 from:
http://www.itp.nyu.edu/physcomp/images/labs/arduino_midi_schem.png
to:
%height=300 alt='arduino midi schematic'%http://www.itp.nyu.edu/physcomp/images/labs/arduino_midi_schem.png
Changed lines 7-8 from:
http://www.itp.nyu.edu/physcomp/labs/arduino_midi_schem.png
to:
http://www.itp.nyu.edu/physcomp/images/labs/arduino_midi_schem.png
Changed lines 7-8 from:
MIDI schematic
to:
http://www.itp.nyu.edu/physcomp/labs/arduino_midi_schem.png
Added lines 11-12:
!!! Play Notes
Added lines 14-137:

[@

// Variables:
char note = 0;            // The MIDI note value to be played

void setup() {
  //  Set MIDI baud rate:
  Serial.begin(31250);
}

void loop() {
  // play notes from F#-0 (30) to F#-5 (90):
  for (note = 30; note < 90; note ++) {
    //Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
    noteOn(0x90, note, 0x45);
    delay(100);
    //Note on channel 1 (0x90), some note value (note), silent velocity (0x00):
    noteOn(0x90, note, 0x00); 
    delay(100);
  }
}

//  plays a MIDI note.  Doesn't check to see that
//  cmd is greater than 127, or that data values are  less than 127:
void noteOn(char cmd, char data1, char data2) {
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);
}

@]
!!! Allow a Person to Play Notes

The previous example will just play notes, no interactivity.  The example below uses an analog input to set the pitch, and a digital input (a switch) to start and stop the note:

[@

// The switch is on Arduino pin 10:
#define switchPin 10
// Middle C (MIDI note value 60) is the lowest note we'll play:
#define middleC 60
//  Indicator LED:
#define LEDpin 13

// Variables:
char note = 0;            // The MIDI note value to be played
int AnalogValue = 0;          // value from the analog input
int lastNotePlayed = 0;  // note turned on when you press the switch
int lastSwitchState = 0;  // state of the switch during previous time through the main loop
int currentSwitchState = 0;

void setup() {
  //  set the states of the I/O pins:
  pinMode(switchPin, INPUT);
  pinMode(LEDpin, OUTPUT);
  //  Set MIDI baud rate:
  Serial.begin(31250);
  blink(3);
}

void loop() {
  //  My potentiometer gave a range from 0 to 1023:
  AnalogValue = analogRead(0);
  //  convert to a range from 0 to 127:
  note = AnalogValue/8;
  currentSwitchState = digitalRead(switchPin);
  // Check to see that the switch is pressed:
  if (currentSwitchState == 1) {
    //  check to see that the switch wasn't pressed last time
    //  through the main loop:
    if (lastSwitchState == 0) {
      // set the note value based on the analog value, plus a couple octaves:
    // note = note + 60;
      // start a note playing:
      noteOn(0x90, note, 0x40);
      // save the note we played, so we can turn it off:
      lastNotePlayed = note;
      digitalWrite(LEDpin, HIGH);
    }
    else    // if the switch is not pressed:
    //  but the switch was pressed last time through the main loop:
    if (lastSwitchState == 1) {
      //  stop the last note played:
      noteOn(0x90, lastNotePlayed, 0x00);
      digitalWrite(LEDpin, LOW);
    }
  }

  //  save the state of the switch for next time
  //  through the main loop:
  lastSwitchState = currentSwitchState;
}

//  plays a MIDI note.  Doesn't check to see that
//  cmd is greater than 127, or that data values are  less than 127:
void noteOn(char cmd, char data1, char data2) {
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);
}

// Blinks an LED 3 times
void blink(int howManyTimes) {
  int i;
  for (i=0; i< howManyTimes; i++) {
    digitalWrite(LEDpin, HIGH);
    delay(100);
    digitalWrite(LEDpin, LOW);
    delay(100);
  }
}

@]

!!!Make an Instrument

Now that you've got the basics, make a musical instrument.  Consider a few things in designing yor instrument:
* Do you want to play discrete notes (like a piano), or sliding pitches (like a theremin)? How do you program to achieve these effects?
* Do you want to control the tempo and duration of a note?
* Do you want the same physical action to set both the pitch and the velocity (volume) of a note?
* Do you want to be able to play more than one note at a time (e.g. chords)?

All of these questions, and many more, will affect what sensors you use, how you read them, and how you design both the physical interface and the software. 
Deleted line 10:
Added lines 1-12:
This page covers only the details of MIDI communication on the Arduino module. For a more general introduction to MIDI on a microprocessor, see the [[http://www.tigoe.net/pcomp/midi.shtml|MIDI notes]] on Tom's physical computing site.

MIDI, the Musical Instrument Digital Interface, is a useful protocol for controlling synthesizers, sequencers, and other musical devices.  MIDI devices are generally grouped in to two broad classes: controllers (i.e. devices that generate MIDI signals based on human actions) and synthesizers (including samplers, sequencers, and so forth). The latter take MIDI data in and make sound, light, or some other effect.

The MIDI out schematic for Arduino looks like this:

MIDI schematic

This circuit doesn't actually match the MIDI specification, but it works with all the MIDI devices we've tried it with.


Once you're connected, sending MIDI is just a matter of sending the appropriate bytes. The bytes have to be sent as binary values, but you can format them in your code as decimal or hexadecimal values.  The example below uses hexadecimal format for any fixed values, and a variable for changing values.  All values are sent serially as raw binary values, using the BYTE modifier to Serial.print() (Many MIDI tables give the command values in hex, so this was done in hex for the sake of convenience):
  Edit | View | History | Print | Recent Changes | Search Page last modified on December 12, 2012, at 05:30 PM