Following is the program for scanning a keyboard for MIDI transfer. At present the code works fine, but it could be faster. I need to know how to use direct port manipulation inside the for loop.
boolean keypressed[6][6]; //for storing key states
int 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
void setup() {
Serial.begin(38400);
for(int i=0; i<6; i++){
pinMode(i+2,OUTPUT);
digitalWrite(i+8,HIGH);
}
for(int i=0; i<6; i++){
pinMode(i+8,INPUT);
digitalWrite(i+8,HIGH);
}
for(int i=0; i<6; i++){
for(int j=0; j<6; j++){
keypressed[i][j] = false;
}
}
}
void loop() {
for(int i=0; i<6; i++){
digitalWrite(i+2, LOW);
for(int j =0; j<6; j++){
if ((digitalRead(j+8) == LOW) && !keypressed[i][j]){
Serial.write(0x91);
Serial.write(midinote[i][j]);
Serial.write(127);
keypressed[i][j] = true;
}
if ((digitalRead(j+8) == HIGH) && keypressed[i][j]){
Serial.write(0x91);
Serial.write(midinote[i][j]);
Serial.write(0);
keypressed[i][j] =false;
}
}
digitalWrite(i+2, HIGH);
}
}
-
You need to download the data sheet of the AVR and look up the port registers. Then you can access (read or write) all bits of a port with a single instruction. You will need to change your keyboard scanning algorithm accordingly. -- Having said this, I'm not sure that the Arduino libraries don't have methods to do this already.the busybee– the busybee08/19/2020 07:26:56Commented Aug 19, 2020 at 7:26
-
1I do not think that direct port access increases the speed of your program remarkably. If you natively read a PORT you have to store the value in a CPU register and then check its bits in the loop. There is a difference in reading a IO register compared to an internal CPU register, but its not that much compared to the time you need to send the midi note. And that's limited to the midi speed. (Wich is 31250 bps, as far as i know - not 38400 as I saw in your code). But if the receiver understands, it's OK ;-). So, I would say the midi speed is he bottleneck not the keyboard scanner algorithm.Peter Paul Kiefer– Peter Paul Kiefer08/19/2020 09:04:07Commented Aug 19, 2020 at 9:04
-
1Ah, if you don't need any wise ( dumb ;-) ) talk. Here is a link that answers yout question. So you can decide whether it is worth it, to change your program. arduino.cc/en/Reference/PortManipulationPeter Paul Kiefer– Peter Paul Kiefer08/19/2020 09:18:49Commented Aug 19, 2020 at 9:18
-
I doubt if you gain anything with it, as Serial will be the bottleneck. But if you must, I'd suggest something like the digitalWriteFast library.Gerben– Gerben08/19/2020 14:48:01Commented Aug 19, 2020 at 14:48
1 Answer 1
It is better to use interrupts instead of pooling pins. but this is related to your arduino controller. what is you arduino core controller? if you can select I prefer "Nano 33 IoT " which has 9 pins (2, 3, 9, 10, 11, 13, 15, A5, A7) available for interrupt. this page may be usable: https://playground.arduino.cc/Main/PinChangeInterrupt/
-
I'm not sure how useful interrupts are on a button matrix.Gerben– Gerben08/19/2020 14:42:21Commented Aug 19, 2020 at 14:42
-
polling force cpu to check i/o ports in loop that cause waste cpu time and code is a cascaded for loop. where using interrupts lets cpu doing its normal code and interrupt with its input Vector make every thing ready for you, by some bit-wise operations you can find changed port and send correct output.Saeed Mirshams– Saeed Mirshams08/22/2020 08:32:46Commented Aug 22, 2020 at 8:32
-
That’s a very common newbie trap. But go ahead.Delta_G– Delta_G09/18/2020 17:07:48Commented Sep 18, 2020 at 17:07