So Im stuck in the mud here.
I have a mega2560 and Ive reprogrammed the mega8U2 firmware to be a midi port.
I have a button matrix, that outputs a midi note on each button that is pressed. That works fine.
What I am trying to do, is store the buttons that are pressed (or the notes that represent the buttons) in a matrix, and play them continuously - i.e the user can hold three buttons down, but they will be played as midi notes one after the other - I am using an internal timer to set the delay between playing each one.
So using a couple of loops that check each button in the matrix for a state change from last known state, if the button is pressed the function:
addToArray(note); is called. When it is released, removeFromArray(note); is called. They look like this:
void addToArray(int Key){
for(byte i = 0; i<sizeof(pressedNoteOrder); i++){
if (pressedNoteOrder[i] == 0){
pressedNoteOrder[i] = Key;
break;
}
}
}
void removeFromArray(int Key){
for (byte i = 0; i<sizeof(pressedNoteOrder); i++){//go through each item in the array
if(pressedNoteOrder[i] == Key){//look for a match to the note that was released
for (byte j=i+1; j<sizeof(pressedNoteOrder); j++){
pressedNoteOrder[j-1] = pressedNoteOrder[j];//when found, shift each note after down one, i.e to clear it out of the array
}
pressedNoteOrder[sizeof(pressedNoteOrder)-1]=0;//set the end note in the array to 0
break;
}
}
}
I.e when a button is released any notes after that are shifted down the array.
Then each time the timer 'ticks' this function is called:
void playArray(){
static int i = 0;
do{
i++;
if (i > sizeof(pressedNoteOrder)){
i = 0;
}
}while(pressedNoteOrder[i] == 0);
Play(pressedNoteOrder[i]);
}
i.e loops through the array playing the notes on each tick.
Well thats what I thought it does. However that doesnt work
The idea is to play the notes in the order the user presses them, however the user can hold them down, or change the notes they are holding and it will continue to play them in order.
I have struggled to debug this in midi mode, and now my mega2560 wont play ball and I cant update the firmware back to serial.
Can anyone see my error - or simulate for me what im after, to see where Im going wrong.
Thanks Guys