I have a sound circuit that has worked fine with the code I had "thrown" together.
Now I have a new MIDI sequencer, that sends Note On, Velocity = 0, in stead of Note Off messages.
Here's the code I have used until now
byte incomingByte;
byte note;
int action=2; //0 =note off ; 1=note on ; 2= nada
Serial.begin(31250);
}
//loop: wait for serial data, and interpret the message
void loop () {
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
//Serial.print(incomingByte, BYTE); //debugging over midi out
// wait for a 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 ( (action==0)&&(note==0) ){ // if we received a "note off", we wait for which note (databyte)
note=incomingByte;
playAl(0); //play allophone number 0, a pause to make it shut up :)
note=0;
action=2;
}else if ( (action==1)&&(note==0) ){ // if we received a "note on", we wait for the note (databyte)
note=incomingByte;
playAl(note);
note=0;
action=0;
}else{
//nada
}
// deal with cc data
if((incomingByte >= 0xb0) && (incomingByte < 0xc0) && (flag_previous == 0)) {
channel = incomingByte & B00001111;
flag_previous = 3;
}
else if((incomingByte < 0x80) && (flag_previous == 3)) {
ccnumber = incomingByte;
flag_previous = 4;
}
else if((incomingByte < 0x80) && (flag_previous == 4)) {
ccvalue = incomingByte;
doCC(channel, ccnumber, ccvalue);
flag_previous = 0;
}
// done with cc data
}
}
void playAl(byte note)
{
digitalWrite(ALD, LOW);
delayMicroseconds(2);
digitalWrite(ALD, HIGH);
PORTB = note; //PORTB is digital pins 8-13,6&7 not in use
while (digitalRead(SBY) == 0);
}
The MIDI code is copied from somewhere, and I have seen it with the Velocity part as well.
My question is how to implement the Velocity in my code.
The sound circuit can not make use of the velocity, but the code needs to work for both Note Off and Note On with velocity = 0.
So Note On velocity >0 will start playAl (note) and Note On velocity ==0 will start playAl (0) which is a pause or mute command.
The code works now with other sequencers, and I don't want to mess up what works, but the different approaches I have tried has not worked, so I wonder if I need to look at the whole MIDI IN code with new eyes.
There's a lot of "else if" there. Maybe a "cleaner" approach
I tried this:
byte incomingByte;
byte note;
byte velocity;
int action=2; //0 =note off ; 1=note on ; 2= nada
void loop () {
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
//Serial.print(incomingByte, BYTE); //debugging over midi out
// wait for a 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 ( (action==0)&&(note==0) ){ // if we received a "note off", we wait for which note (databyte)
note=incomingByte;
playAl(0); //play allophone number 0, a pause to make it shut up :)
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;
}else if ( (action==1)&&(note!=0)&&(velocity==0) ){
playAl(0);
note=0;
velocity=0;
action=2;
}else if ( (action==1)&&(note!=0)&&(velocity!=0) ){
playAl(note);
note=0;
velocity=0;
action=0;
}else{
//nada
}
This did not work
Any suggestions?