Improve code, Interrupt? [ADVICE]

Hello dear members,

Im making myself a midi fighter, just to have som fun with it. Its a box with buttons, that sends midi values to to music programs and plays the tone, sound.

The code is done, and the prototype is done. Right now Im up for improving the code.

The problem I see right now is that, when you do press a button, it feels like it takes lika a ms for the sound to be played.
This is not good enough for me, I want the code to be perfect.

So my question is, how can I make the reaction faster, is it due to the code?
Should I change my code for interrupts instead for letting it loop through everything over and over again?

It simply checks for button changes, if it notices a change it will register it, if its high it will keep playing the note, until it goes low

Here is the code.

#define xButtons 11

int ValdKanal = 144;

int State[xButtons]              =  {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};
int CurrentState[xButtons]       =  {LOW, LOW, LOW ,LOW, LOW, LOW, LOW, LOW, LOW, LOW};
int Noter[xButtons]              =  {49,41,39,44,46,42,43,51,40,37};
const int buttonPin[xButtons]    = {3,4,5,6,7,8,9,10,11,12};     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

int buttonState = 0;         // variable for reading the pushbutton status



void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  Serial.begin(57600); 
}


void loop(){
  
  for(int x = 0 ; x < xButtons ; x++){
    buttonState = digitalRead(buttonPin[x]);
       
    if(buttonState == LOW){
      CurrentState[x] = LOW;
    }
    else {      
       CurrentState[x] = HIGH;
    }
  
    if(CurrentState[x] != State[x]){
      if(CurrentState[x] == HIGH){
         Serial.print("ON"); Serial.println();
         digitalWrite(ledPin, HIGH); 
         noteOn(ValdKanal, Noter[x], 0x7F);
      }
      else{
         digitalWrite(ledPin, LOW); 
         Serial.print("OFF"); Serial.println();Serial.println(); 
         noteOn(ValdKanal, Noter[x], 0x00);   
      }      
      State[x] = !State[x];
    }
  }
      
     
}

void noteOn(int cmd, int pitch, int velocity) {
   Serial.write(cmd);
   Serial.write(pitch);
   Serial.write(velocity);
 }

Should I change my code for interrupts

No.

Remove the Serial.prints that is interfering with the MIDI out.
Also don't look at the input and change a variable just use the input directly, that won't save much though.

Serial prints, removed = CHECK.

The reason why I am registrating the values of the buttons if variables, is because I do not want it to send midi values over and over again. So, if Ive pressed a button, it will send a midi value, if it still is pressed it will not enter that function again.

Or do you mean something else?

Cheers

Why have you defined xButtons to be 11 when your arrays only have 10 elements?

Pete

Thanks for notifying it. I actually have 12 buttons, but 2 of em are are defect. Will change that 2.

The reason why I am registrating the values of the buttons if variables, is because I do not want it to send midi values over and over again. So, if Ive pressed a button, it will send a midi value, if it still is pressed it will not enter that function again.

This whole bit is redundant

buttonState = digitalRead(buttonPin[x]);
       
    if(buttonState == LOW){
      CurrentState[x] = LOW;
    }
    else {      
       CurrentState[x] = HIGH;
    }

replace it with

CurrentState[x] = digitalRead(buttonPin[x]);

Well well well, now I feel stupid hehe. It has been replaced now.

So actually, when should I use interrupts then. Only when it comes to codes that is long, and you need like an emergency button?

Cheers

So actually, when should I use interrupts then.

When you need to do something and you can't poll often enough. Not very often it turns out.

I see, well I guess Im done here now.
Now its only the prototype that has to be transformed to eye candy ^^

Cheers, and thanks