Full Code:
const int LT = 1;
const int LI = 2;
const int LM = 3;
const int LR = 4;
const int RI = 5;
const int RM = 6;
const int RRB = 7;
const int RRS = 8;
const int RPB = 9;
const int RPS = 10;
const int AIR = 13;
const int wait = 500;
const int fingeringMatrix [] [10] =
{
//C, Thumb to RPS
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},//72 = C5 (one octave aboute middle C)
{1, 1, 1, 1, 1, 1, 1, 1, 1, 0},//note: Middle C is 60
{1, 1, 1, 1, 1, 1, 1, 1, 0, 0},
{1, 1, 1, 1, 1, 1, 1, 0, 0, 0},
{1, 1, 1, 1, 1, 1, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 0, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 0, 1, 1, 1, 0, 0, 0},
{1, 1, 1, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 0, 1, 1, 0, 0, 0, 0, 0},
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 0},//C6?
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
//NO THUMB NOW!!!!!!!
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 1, 1, 1, 1, 0, 0},
{0, 1, 1, 1, 1, 1, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 0, 1, 0, 0, 1, 1},
{0, 1, 1, 1, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 0, 1, 0, 0, 0, 0, 0},
{0, 1, 1, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 0, 1, 1, 1, 1, 0, 0},//94
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}//OFF ROW
};
// MIDI data: https://learn.sparkfun.com/tutorials/midi-tutorial/all
byte incomingByte;
byte note;
byte velocity;
int action; //0=NONE; 1=NOTE ON; 2= NOTE OFF 3= OTHER, -1 = NOTE ON VELOCITY, -2 = NOTE OFF VELOCITY
void setup()
{
Serial.begin(38400); //38400 debug, 31250 real (matches MIDI)
//set up the pins
for (int i = 1; i <= 10; i++)
{
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
}
pinMode(AIR, OUTPUT);
//set up vars
action = 0;
note = 0;
velocity = 0;
}
void loop() {
if (Serial.available() > 0) // if theres data
{
incomingByte = Serial.read();//get data
/*long asdf;
asdf = Serial.parseInt();
incomingByte = LTB(asdf);
Serial.println(asdf);
Serial.println(action);
Serial.println(incomingByte);*/
if (159 >= incomingByte && incomingByte >= 144) //NOTE ON status
{
action = 1;
}
else if (143 >= incomingByte && incomingByte >= 128) //NOTE OFF status
{
action = 2;
}
else if (incomingByte >= 128) //some other status byte
{
action = 3;
}
else if (incomingByte < 128) //data byte
{
if (action == 1)
{
note = incomingByte;
action = -1;
}
else if (action == 2)
{
note = incomingByte;
action = -2;
}
else if (action == -1)
{
velocity = incomingByte;
playNote(note, velocity);
action = 1;
}
else if (action == -2)
{
velocity = incomingByte;
playNote(note, 0);
action = 2;
}
}
}
}
void playNote(byte n, byte v)
{
if(note <72 || note >94)
{
return;
}
if (v == 0) //if OFF
{
note = 95;//OFF note
}
if (note >= 72 && note <= 95) //WHEN ON
{
note -= 72;
for (int i = 0; i < 10; i++)
{
digitalWrite(i + 2, fingeringMatrix[note][i]);
}
}
if(v==0)
{
digitalWrite(AIR, LOW);
}
else
{
delay(30);
digitalWrite(AIR, HIGH);
}
return;
}
byte LTB(long e)
{
byte ans = 0;
int ansArr[8];
for (int i = 0; i < 8; i++)
{
ansArr[i] = (e % 2);
e /= 2;
}
for (int i = 7; i >= 0; i--)
{
ans += ansArr[i];
if (i != 0)
{
ans *= 2;
}
}
return ans;
}