So I'm using an arduino to retrofit an old casio to communicate with MIDI, that way I can control it with ableton etc.
I'm pretty sure this code will work but its not very elegant and I KNOW theres GOTTA be a way to make it play nicer (I'm assuming arrays? I just don't know how to implement that in this situation).
So far I have only included four notes, I would need to continue the if/else statements similarly for all 40 some odd notes....would get very ugly.
I'd love to see if any of you have any suggestions....and if so if you could explain why so I can break the cut/paste cycle and learn something!
Here is a link to describe the keyboard matrixing on the casio for further reference
http://www.maxmidi.com/diy/sk1/article.html
thanks again
byte incomingByte;
byte note;
byte velocity;
int statusLed = 13;
int action = 2; //0 =note off ; 1=note on ; 2= nada
int data1 = 1; // data input lines//
int data2 = 2;
int data3 = 3;
int data4 = 4;
int S0 = 5; //switch input lines//
int S1 = 6;
int S2 = 7;
int S3 = 8;
int S4 = 9;
int S5 = 10;
int S6 = 11;
int S7 = 12;
int S0Val = 0;
int S1Val = 0;
int S2Val = 0;
int S3Val = 0;
int S4Val = 0;
int S5Val = 0;
int S6Val = 0;
int S7Val = 0;
void setup(){
Serial.begin(31250);
pinMode(data1, OUTPUT); //data input lines//
pinMode(data2, OUTPUT);
pinMode(data3, OUTPUT);
pinMode(data4, OUTPUT);
pinMode(statusLed, OUTPUT);
pinMode(S0, INPUT); //switch input lines//
pinMode(S1, INPUT);
pinMode(S2, INPUT);
pinMode(S3, INPUT);
pinMode(S4, INPUT);
pinMode(S5, INPUT);
pinMode(S6, INPUT);
pinMode(S7, INPUT);
}
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
action=1;
}else if (incomingByte== 128){ // note off message starting
action=0;
}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 playNote(byte note, byte velocity){
int value=LOW;
if (velocity >10){
value=HIGH;
}else{
value=LOW;
}
S0Val = digitalRead(S0); //switch lines//
S1Val = digitalRead(S1);
S2Val = digitalRead(S2);
S3Val = digitalRead(S3);
S4Val = digitalRead(S4);
S5Val = digitalRead(S5);
S6Val = digitalRead(S6);
S7Val = digitalRead(S7);
if (note == 36 && S0Val > 0){ //if note is middle C and switch0 is HIGH//
delayMicroseconds(100);
digitalWrite(data1, HIGH); //write high to data line 1//
delayMicroseconds(100);
digitalWrite(data1, LOW);
}
else {
digitalWrite(data1, LOW); }
if (note == 37 && S0Val > 0){
delayMicroseconds(100);
digitalWrite(data2, HIGH);
delayMicroseconds(100);
digitalWrite(data2, LOW);
}
else {
digitalWrite(data2, LOW); }
if (note == 38 && S0Val > 0){
delayMicroseconds(100);
digitalWrite(data3, HIGH);
delayMicroseconds(100);
digitalWrite(data3, LOW);
}
else {
digitalWrite(data3, LOW); }
if (note == 39 && S0Val > 0){
delayMicroseconds(100);
digitalWrite(data4, HIGH);
delayMicroseconds(100);
digitalWrite(data4, LOW);
}
else {
digitalWrite(data4, LOW); }
}