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