2

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);
   }

 }
4
  • 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. Aug 19 '20 at 7:26
  • 1
    I 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. Aug 19 '20 at 9:04
  • 1
    Ah, 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/PortManipulation Aug 19 '20 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
    Aug 19 '20 at 14:48
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/

3
  • I'm not sure how useful interrupts are on a button matrix.
    – Gerben
    Aug 19 '20 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. Aug 22 '20 at 8:32
  • That’s a very common newbie trap. But go ahead.
    – Delta_G
    Sep 18 '20 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.