header
Back to Tutorials Page
  

 
 
ANALOG IN / MIDI OUT::

This tutorial uses a potentiometer to generate note information and a button to trigger the Arduino to send MIDI data out. The potentiometer should be connected as in the potentiometer tutorial and the switch as in the digital input tutorial. We are using the hardware switch debounce scheme from the switch debounce tutorial. The MIDI out circuit is simple...

Click HERE for video clip.

CODE:

//ANALOG INPUT / MIDI OUTPUT (SERIAL)

//GENERATE MIDI DATA USING A POTENTIOMETER
//TO SET MIDI NOTE VAULE
//AND BUTTON TO SEND MIDI DATA

//DECLARE VARIABLES

int potPIN = 0; //declare pot input pin
int buttonPIN = 12; //declare button input pin
int buttonValue = 0; //variable to read state of button
int note = 0; //variable for note value (from pot value)
int ledPin = 13; //LED indicator (on when button pressed)

void setup() {
//set baud rate for MIDI
Serial.begin(31250);
pinMode (ledPin, OUTPUT);
pinMode (buttonPIN, INPUT);
}
void loop (){
note = analogRead (potPIN); //read value of potentiometer
note = (note/4); //scale value to a useful range
buttonValue = digitalRead (buttonPIN);
if (buttonValue == 1) //do this if button is pressed
{
sendMidi (0x90, note, 0x7F); //send MIDI note on information
digitalWrite (ledPin, HIGH);
do
{
buttonValue = digitalRead (buttonPIN); //read state of button
}while (buttonValue == 1); //wait for button to be released
sendMidi (0x80, note, 0x7F); //send MIDI off information
digitalWrite (ledPin, LOW);
}
}

//THIS FUNCTION PLAYS A MIDI NOTE WHEN midiCommand = 0x90
//THIS FUNCTION TURNS OFF THE NOTE WHEN midiCommand = 0x80
void sendMidi (byte midiCommand, byte noteValue, byte velocityValue)
{
Serial.print(midiCommand, BYTE);
Serial.print(noteValue, BYTE);
Serial.print(velocityValue, BYTE);
}


REFERENCES:


 

ANALOG IN - MIDI OUT CIRCUIT

 

ANALOG IN / MIDI OUT SCHEMATIC

 

BASIC MIDI BREADBOARD CIRCUIT

 

MIDI PIN ADAPTER LAYOUT