/*Midi to relay // Wapata 02.2012
*Based on: Midi In Basic 0.2 // kuki 8.2007
*
* -----------------
* listen for midi serial data, and active outputs for individual notes
IMPORTANT:
your arduino might not start if it receives data directly after a reset, because the bootloader thinks you want to uplad a new progam.
you might need to unplug the midi-hardware RX pin until the board is running your program. that is when that statusLed turns on.
#####################################################################################################################################################
SOMETHING ABOUT MIDI MESSAGES
midi messages start with one status byte followed by 1 _or_ 2 data bytes, depending on the command
example midi message: 144-60-100
the status byte "144" tells us what to do. "144" means "note on".
in this case the second bytes tells us which note to play (60=middle C)
the third byte is the velocity for that note (that is how powerful the note was struck= 100)
example midi message: 128-36
this message is a "note off" message (status byte = 128). it is followed by the note (data byte = 36)
since "note off" messages don't need a velocity value (it's just OFF) there will be no third byte in this case
NOTE: some midi keyboards will never send a "note off" message, but rather a "note on with zero velocity"
do a web search for midi messages to learn more about aftertouch, poly-pressure, midi time code, midi clock and more interesting things.
have to talk about how midi message realy is.
#####################################################################################################################################################
HARDWARE NOTE:
The Midi Socket is connected to arduino RX through an opto-isolator to invert the midi signal and seperate the circuits of individual instruments.
connect outputs to pin2-pin9 pinA0-pinA5 on your arduino.
####################################################################################################################################################
*/
//variables setup
byte incomingByte;
byte note;
byte velocity;
int statusLed = 13; // select the pin for the LED
int action=2; //0 =note off ; 1=note on ; 2= nada
//setup: declaring iputs and outputs and begin serial
void setup() {
pinMode(statusLed,OUTPUT); // declare the LED's pin as output
pinMode(2,OUTPUT); //R1
pinMode(3,OUTPUT); //R2
pinMode(4,OUTPUT); //R3
pinMode(5,OUTPUT); //R4
pinMode(6,OUTPUT); //R5
pinMode(7,OUTPUT); //R6
pinMode(8,OUTPUT); //R7
pinMode(9,OUTPUT); //R8
pinMode(A0,OUTPUT); //R9
pinMode(A1,OUTPUT); //R10
pinMode(A2,OUTPUT); //R11
pinMode(A3,OUTPUT); //R12
pinMode(A4,OUTPUT); //R13 air 1
pinMode(A5,OUTPUT); //R14 air 2
//start serial with midi baudrate 31250 or 38400 for debugging
Serial.begin(31250);
digitalWrite(statusLed,HIGH);
}
//loop: wait for serial data, and interpret the message
void loop () {
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// wait for as status-byte, channel 1, note on or off
if (incomingByte== 144){ // note on message starting starting
action=1;
}else if (incomingByte== 128){ // note off message starting
action=0;
}else if (incomingByte== 208){ // aftertouch message starting
//not implemented yet
}else if (incomingByte== 160){ // polypressure message starting
//not implemented yet
}else if ( (action==0)&&(note==0) ){ // if we received a "note off", we wait for which note (databyte)
note=incomingByte;
playNote(note, 0);
note=0;
velocity=0;
action=2;
}else if ( (action==1)&&(note==0) ){ // if we received a "note on", we wait for the note (databyte)
note=incomingByte;
}else if ( (action==1)&&(note!=0) ){ // ...and then the velocity
velocity=incomingByte;
playNote(note, velocity);
note=0;
velocity=0;
action=0;
}else{
//nada
}
}
}
void blink(){
digitalWrite(statusLed, HIGH);
delay(100);
digitalWrite(statusLed, LOW);
delay(100);
}
void playNote(byte note, byte velocity){
int value=LOW;
if (velocity >10){
value=HIGH;
}else{
value=LOW;
}
if(note= 72){
digitalWrite(A4, value);
digitalWrite(A5, value);
//digitalWrite(2, value);
//digitalWrite(3, value);
digitalWrite(4, value);
digitalWrite(5, value);
digitalWrite(6, value);
digitalWrite(7, value);
digitalWrite(8, value);
digitalWrite(9, value);
digitalWrite(A1, value);
digitalWrite(A0, value);
digitalWrite(A3, value);
digitalWrite(A2, value);
}
if(note= 73){
digitalWrite(A4, value);
digitalWrite(A5, value);
//digitalWrite(2, value);
//digitalWrite(3, value);
digitalWrite(4, value);
digitalWrite(5, value);
digitalWrite(6, value);
digitalWrite(7, value);
digitalWrite(8, value);
digitalWrite(9, value);
digitalWrite(A1, value);
digitalWrite(A0, value);
digitalWrite(A3, value);
//digitalWrite(A2, value);
}
if(note= 74){
digitalWrite(A4, value);
digitalWrite(A5, value);
//digitalWrite(2, value);
//digitalWrite(3, value);
digitalWrite(4, value);
digitalWrite(5, value);
digitalWrite(6, value);
digitalWrite(7, value);
digitalWrite(8, value);
digitalWrite(9, value);
digitalWrite(A1, value);
digitalWrite(A0, value);
//digitalWrite(A3, value);
//digitalWrite(A2, value);
}
if(note= 75){
digitalWrite(A4, value);
digitalWrite(A5, value);
//digitalWrite(2, value);
//digitalWrite(3, value);
digitalWrite(4, value);
digitalWrite(5, value);
digitalWrite(6, value);
digitalWrite(7, value);
digitalWrite(8, value);
digitalWrite(9, value);
digitalWrite(A1, value);
//digitalWrite(A0, value);
//digitalWrite(A3, value);
//digitalWrite(A2, value);
}
if(note= 76){
digitalWrite(A4, value);
digitalWrite(A5, value);
//digitalWrite(2, value);
//digitalWrite(3, value);
digitalWrite(4, value);
digitalWrite(5, value);
digitalWrite(6, value);
digitalWrite(7, value);
digitalWrite(8, value);
digitalWrite(9, value);
//digitalWrite(A1, value);
//digitalWrite(A0, value);
//digitalWrite(A3, value);
//digitalWrite(A2, value);
}
if(note= 77){
digitalWrite(A4, value);
digitalWrite(A5, value);
//digitalWrite(2, value);
//digitalWrite(3, value);
digitalWrite(4, value);
digitalWrite(5, value);
digitalWrite(6, value);
digitalWrite(7, value);
digitalWrite(8, value);
//digitalWrite(9, value);
//digitalWrite(A1, value);
//digitalWrite(A0, value);
//digitalWrite(A3, value);
//digitalWrite(A2, value);
}
if(note= 78){
digitalWrite(A4, value);
digitalWrite(A5, value);
//digitalWrite(2, value);
//digitalWrite(3, value);
digitalWrite(4, value);
digitalWrite(5, value);
digitalWrite(6, value);
digitalWrite(7, value);
//digitalWrite(8, value);
digitalWrite(9, value);
digitalWrite(A1, value);
digitalWrite(A0, value);
digitalWrite(A3, value); //OPTION
digitalWrite(A2, value); //OPTION
}
if(note= 79){
digitalWrite(A4, value);
digitalWrite(A5, value);
//digitalWrite(2, value);
//digitalWrite(3, value);
digitalWrite(4, value);
digitalWrite(5, value);
digitalWrite(6, value);
digitalWrite(7, value);
//digitalWrite(8, value);
//digitalWrite(9, value);
//digitalWrite(A1, value);
//digitalWrite(A0, value);
//digitalWrite(A3, value);
//digitalWrite(A2, value);
}
if(note= 80){
digitalWrite(A4, value);
digitalWrite(A5, value);
//digitalWrite(2, value);
//digitalWrite(3, value);
digitalWrite(4, value);
digitalWrite(5, value);
digitalWrite(6, value);
//digitalWrite(7, value);
digitalWrite(8, value);
digitalWrite(9, value);
digitalWrite(A1, value);
//digitalWrite(A0, value);
//digitalWrite(A3, value);
//digitalWrite(A2, value);
}
if(note= 81){
digitalWrite(A4, value);
digitalWrite(A5, value);
//digitalWrite(2, value);
//digitalWrite(3, value);
digitalWrite(4, value);
digitalWrite(5, value);
digitalWrite(6, value);
//digitalWrite(7, value);
//digitalWrite(8, value);
//digitalWrite(9, value);
//digitalWrite(A1, value);
//digitalWrite(A0, value);
//digitalWrite(A3, value);
//digitalWrite(A2, value);
}
if(note= 82){
digitalWrite(A4, value);
digitalWrite(A5, value);
//digitalWrite(2, value);
//digitalWrite(3, value);
digitalWrite(4, value);
//digitalWrite(5, value);
//digitalWrite(6, value);
digitalWrite(7, value);
digitalWrite(8, value);
//digitalWrite(9, value);
//digitalWrite(A1, value);
//digitalWrite(A0, value);
//digitalWrite(A3, value);
//digitalWrite(A2, value);
}
if(note= 83){
digitalWrite(A4, value);
digitalWrite(A5, value);
//digitalWrite(2, value);
//digitalWrite(3, value);
digitalWrite(4, value);
//digitalWrite(5, value);
//digitalWrite(6, value);
//digitalWrite(7, value);
//digitalWrite(8, value);
//digitalWrite(9, value);
//digitalWrite(A1, value);
//digitalWrite(A0, value);
//digitalWrite(A3, value);
//digitalWrite(A2, value);
}
if(note= 84){
digitalWrite(A4, value);
digitalWrite(A5, value);
//digitalWrite(2, value);
//digitalWrite(3, value);
//digitalWrite(4, value);
digitalWrite(5, value);
digitalWrite(6, value);
//digitalWrite(7, value);
//digitalWrite(8, value);
//digitalWrite(9, value);
//digitalWrite(A1, value);
//digitalWrite(A0, value);
//digitalWrite(A3, value);
//digitalWrite(A2, value);
}
if(note= 85){
digitalWrite(A4, value);
digitalWrite(A5, value);
digitalWrite(2, value);
digitalWrite(3, value);
digitalWrite(4, value);
digitalWrite(5, value);
digitalWrite(6, value);
//digitalWrite(7, value);
//digitalWrite(8, value);
//digitalWrite(9, value);
//digitalWrite(A1, value);
//digitalWrite(A0, value);
//digitalWrite(A3, value);
//digitalWrite(A2, value);
}
if(note= 86){
digitalWrite(A4, value);
digitalWrite(A5, value);
digitalWrite(2, value);
digitalWrite(3, value);
//digitalWrite(4, value);
digitalWrite(5, value);
digitalWrite(6, value);
//digitalWrite(7, value);
//digitalWrite(8, value);
//digitalWrite(9, value);
//digitalWrite(A1, value);
//digitalWrite(A0, value);
//digitalWrite(A3, value);
//digitalWrite(A2, value);
}
if(note= 87){
digitalWrite(A4, value);
digitalWrite(A5, value);
digitalWrite(2, value);
digitalWrite(3, value);
//digitalWrite(4, value);
digitalWrite(5, value);
digitalWrite(6, value);
digitalWrite(7, value);
digitalWrite(8, value);
digitalWrite(9, value);
digitalWrite(A1, value);
digitalWrite(A0, value);
//digitalWrite(A3, value);
//digitalWrite(A2, value);
}
if(note= 88){
digitalWrite(A4, value);
digitalWrite(A5, value);
digitalWrite(2, value);
//digitalWrite(3, value);
digitalWrite(4, value);
digitalWrite(5, value);
digitalWrite(6, value);
digitalWrite(7, value);
digitalWrite(8, value);
digitalWrite(9, value);
//digitalWrite(A1, value);
//digitalWrite(A0, value);
//digitalWrite(A3, value);
//digitalWrite(A2, value);
}
if(note= 89){
digitalWrite(A4, value);
digitalWrite(A5, value);
digitalWrite(2, value);
//digitalWrite(3, value);
digitalWrite(4, value);
digitalWrite(5, value);
digitalWrite(6, value);
digitalWrite(7, value);
digitalWrite(8, value);
//digitalWrite(9, value);
//digitalWrite(A1, value);
//digitalWrite(A0, value);
//digitalWrite(A3, value);
//digitalWrite(A2, value);
}
if(note= 90){
digitalWrite(A4, value);
digitalWrite(A5, value);
digitalWrite(2, value);
//digitalWrite(3, value);
digitalWrite(4, value);
digitalWrite(5, value);
digitalWrite(6, value);
digitalWrite(7, value);
//digitalWrite(8, value);
digitalWrite(9, value);
//digitalWrite(A1, value);
//digitalWrite(A0, value);
digitalWrite(A3, value);
digitalWrite(A2, value);
}
if(note= 91){
digitalWrite(A4, value);
digitalWrite(A5, value);
digitalWrite(2, value);
//digitalWrite(3, value);
digitalWrite(4, value);
digitalWrite(5, value);
digitalWrite(6, value);
digitalWrite(7, value);
//digitalWrite(8, value);
//digitalWrite(9, value);
//digitalWrite(A1, value);
//digitalWrite(A0, value);
//digitalWrite(A3, value);
//digitalWrite(A2, value);
}
if(note= 92){
digitalWrite(A4, value);
digitalWrite(A5, value);
digitalWrite(2, value);
//digitalWrite(3, value);
digitalWrite(4, value);
digitalWrite(5, value);
digitalWrite(6, value);
digitalWrite(7, value);
//digitalWrite(8, value);
digitalWrite(9, value);
digitalWrite(A1, value);
digitalWrite(A0, value);
digitalWrite(A3, value);
digitalWrite(A2, value);
}
if(note= 93){
digitalWrite(A4, value);
digitalWrite(A5, value);
digitalWrite(2, value);
//digitalWrite(3, value);
digitalWrite(4, value);
digitalWrite(5, value);
digitalWrite(6, value);
//digitalWrite(7, value);
//digitalWrite(8, value);
//digitalWrite(9, value);
//digitalWrite(A1, value);
//digitalWrite(A0, value);
//digitalWrite(A3, value);
//digitalWrite(A2, value);
}
if(note= 94){
digitalWrite(A4, value);
digitalWrite(A5, value);
digitalWrite(2, value);
//digitalWrite(3, value);
digitalWrite(4, value);
digitalWrite(5, value);
digitalWrite(6, value);
//digitalWrite(7, value);
digitalWrite(8, value);
digitalWrite(9, value);
digitalWrite(A1, value);
digitalWrite(A0, value);
//digitalWrite(A3, value);
//digitalWrite(A2, value);
}
if(note= 95){
digitalWrite(A4, value);
digitalWrite(A5, value);
digitalWrite(2, value);
//digitalWrite(3, value);
digitalWrite(4, value);
digitalWrite(5, value);
digitalWrite(6, value);
//digitalWrite(7, value);
digitalWrite(8, value);
digitalWrite(9, value);
//digitalWrite(A1, value);
//digitalWrite(A0, value);
//digitalWrite(A3, value);
//digitalWrite(A2, value);
}
if(note= 96){
digitalWrite(A4, value);
digitalWrite(A5, value);
digitalWrite(2, value);
//digitalWrite(3, value);
digitalWrite(4, value);
//digitalWrite(5, value);
//digitalWrite(6, value);
//digitalWrite(7, value);
digitalWrite(8, value);
digitalWrite(9, value);
//digitalWrite(A1, value);
//digitalWrite(A0, value);
//digitalWrite(A3, value);
//digitalWrite(A2, value);
}
if(note= 97){
digitalWrite(A4, value);
digitalWrite(A5, value);
digitalWrite(2, value);
//digitalWrite(3, value);
digitalWrite(4, value);
//digitalWrite(5, value);
digitalWrite(6, value);
digitalWrite(7, value);
digitalWrite(8, value);
//digitalWrite(9, value);
digitalWrite(A1, value);
digitalWrite(A0, value);
digitalWrite(A3, value);
digitalWrite(A2, value);
}
if(note= 98){
digitalWrite(A4, value);
digitalWrite(A5, value);
digitalWrite(2, value);
//digitalWrite(3, value);
digitalWrite(4, value);
//digitalWrite(5, value);
//digitalWrite(6, value);
digitalWrite(7, value);
digitalWrite(8, value);
//digitalWrite(9, value);
digitalWrite(A1, value);
digitalWrite(A0, value);
digitalWrite(A3, value);
//digitalWrite(A2, value);
}
/*since we don't want to "play" all notes we wait for a note between 36 & 44
if(note>=36 && note<44){
byte myPin=note-34; // to get a pinnumber between 2 and 9
digitalWrite(myPin, value);*/
}