comment convertir le code suivant en un seul en utilisant la manipulation directe du port

Aug 19 2020

Voici le programme de numérisation d'un clavier pour le transfert MIDI. À l'heure actuelle, le code fonctionne bien, mais il pourrait être plus rapide. J'ai besoin de savoir comment utiliser la manipulation directe de port à l'intérieur de la boucle for.

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

 }

Réponses

1 SaeedMirshams Aug 19 2020 at 18:27

Il est préférable d'utiliser des interruptions au lieu de regrouper les broches. mais cela est lié à votre contrôleur arduino. quel est votre contrôleur de base arduino? si vous pouvez sélectionner je préfère "Nano 33 IoT" qui a 9 broches (2, 3, 9, 10, 11, 13, 15, A5, A7) disponibles pour l'interruption. cette page peut être utilisable :https://playground.arduino.cc/Main/PinChangeInterrupt/