Firstly, I have no prior experience related to any midi projects, nay keyboard matrix scanning projects. I came up with this code on my own, so please point out all and any pitfalls and options to improve this program.
byte portd[] = {4,8,16,32,64,128};
boolean keypressed[6][6]; //for storing key states
byte midinote[6][6] ={ 60 ,61 ,62 ,63 ,64 ,65
,66 ,67 ,68 ,69 ,70 ,71
,72 ,73 ,74 ,75 ,76 ,77
,78 ,79 ,80 ,81 ,82 ,83
,84 ,85 ,86 ,87 ,88 ,89
,90 ,91 ,92 ,93 ,94 ,95 }; //code for various midi notes C3 to C6
byte i,j;
int main (void) {
init();
Serial.begin(38400);
DDRD |= 0b11111100;
PORTB &= 0b0000001;
DDRB &= 0b11000000;
PORTB |= 0b00111111;
for (i=0; i<6; i++){
for(j=0; j<6; j++){
keypressed[i][j] = false;
}
}
while(1) {
for(i=0; i<6; i++){
PORTD &= ~portd[i];
for(j =0; j<6; j++){
if (((PINB & (1<<j)) == 0) && !keypressed[i][j]){
Serial.write(0x91);
Serial.write(midinote[i][j]);
Serial.write(127);
keypressed[i][j] = true;
}
if (((PINB & (1<<j)) != 0) && keypressed[i][j]){
Serial.write(0x91);
Serial.write(midinote[i][j]);
Serial.write(0);
keypressed[i][j] =false;
}
}
PORTD |= portd[i];
}
}
return 0;
}