oh, here is the code i'm working on.
/* ################################ Basic Midi-in for arduino 0.1
* reads midi-data from the serial line and lights individual LEDs for "Note on" messages
* i am still working on the best wiring:
* it's based on a 4N28 optocoupler to seperate circuits and invert the logic levels
* the main problem here is the speed of the optocoupler to retain a clear digital signal.
BUT IT WORKS!
22/09/2006, kuk
kuki_AT_milksoup.net
*/
//######################### DEFINE PINS ON ARDUINO
int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;
int led5 = 6;
int led6 = 7;
int led7 = 8;
int led8 = 9;
int midiEnable = 10; //this one "powers" the 4N28 to avoid having it on all the time, which interferes with bootloading
int statusLed = 13;
//######################### DECLARE VARIABLES
byte incomingByte;
byte action = 2; // 0=note off , 1= note on
byte note;
byte velocity;
//######################### SETUP EVERYTHING
void setup() {
pinMode(statusLed,OUTPUT); // declare the LED's pin as output
pinMode(midiEnable,OUTPUT);
Serial.begin(31250); //midi baudrate
//turn midi input on
digitalWrite(midiEnable, HIGH);
//do fancy startup signal
blink();
blink();
}
//######################### MAIN LOOP
// read serial data, and collect bytes that belong to the same midi message
void loop () {
if (Serial.available() > 0) {
//blinkfast();
incomingByte = Serial.read();
// collect data: we receive at least 3 bytes when a key gets hit: 1 statusbyte(channel & on/off/etc), and 2 databytes (key + velocity)
if (incomingByte== 144){ // note on message channel 1
action=1;
blink();
}else if (incomingByte== 128){ // note off message channel 1
action=0;
//blink();
}else if (incomingByte== 208){ // aftertouch message channel 1
// not implemented yet
}else if (incomingByte== 160){ // polypressure message channel 1
// 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); //turns off the corresponding LED
note=0;
velocity=0;
action=2;
}else if ( (action==1)&&(note==0) ){ // if we received a "note on", we wait for the note (next databyte)
note=incomingByte;
}else if ( (action==1)&&(note!=0) ){ // ...and then the velocity data byte
velocity=incomingByte;
// now we have our bytes complete, let's make some visual noise
playNote(note, velocity);
//reset values
note=0;
velocity=0;
action=0;
}else{
//nada
}
}
}
void blink(){
digitalWrite(statusLed, LOW);
delay(500);
digitalWrite(statusLed, HIGH);
delay(500);
}
void blinkfast(){
digitalWrite(statusLed, LOW);
delay(200);
digitalWrite(statusLed, HIGH);
delay(100);
}
void playNote(byte note, byte velocity){
int value=LOW;
if (velocity >10){
value=HIGH;
}else{
value=LOW;
}
switch (note){
case 60: //mid-C
digitalWrite(led1, value);
break;
case 62: // D
digitalWrite(led2, value);
break;
}
//blink();
}