//MIDI experiment //takes midi input and converts to analog voltage to communicate with tuning module //cycles through each channel repeatedly to prevent last channel from dominating //remember to disconnect rx pin before uploading (unplug sheild) cause it is used for midi and downloading //variables int chan0=5; int chan1=6; int channelNumber; int midiNote=0; int remappedNote=0; int midiVolume=0; byte incomingByte=0; int midiValue=0; int statevariable=0; //functions //pin 10 display note is sharp int checkMidi () { remappedNote=-1; if (Serial.available() > 0) { // read the incoming byte: incomingByte = Serial.read(); if ((incomingByte & 0xF0) == 0x90) // note on message starting { statevariable=1; channelNumber=incomingByte & 0x0F; } else if (statevariable==1) //"note on", could be real "note on" or effective "note off" { if (incomingByte!=0) //real "note on" { midiNote=incomingByte; remappedNote= (midiNote+8) % 12; //+8 maps c=0 to e=0 midiNote=0; //reset for next new event statevariable=3; //a velocity will follow } else statevariable=0; //effective note off, done } else if (statevariable==3) //get velocity, done { midiVolume=incomingByte; statevariable=0; } else if (statevariable==3) //real note off, done { statevariable=0; midiNote=0; } } return remappedNote; } void setup () { //start serial with Midi baudrate 31250 or 38400 for debugging Serial.begin(31250); pinMode (chan0,OUTPUT); //digitalWrite (chan0,LOW); pinMode (chan1,OUTPUT); //digitalWrite (chan1,LOW); } void loop () { midiValue=checkMidi(); if (midiValue!=-1) //store pending midi values { if (channelNumber==0) analogWrite(chan0, midiValue*23); //map 0-11 to 0-253 if (channelNumber==1) analogWrite(chan1, midiValue*23); } }