need help with midi snes controler mainly the button press

hello everyone and i hope every ones haning a good night, i have this problem with my code let me explain what im trying to do first i want to use a snes contoler to send midi on notes to the computer via serial when a selected button eg. "A" "B" is pressed down and a midi off signal when the button is released however i only need one message sent when i hold down a button so i was trying to getr a if (buttonState != lastButtonState) type of thing going but im having a lot of trouble getting each button to send the notes here is my code so far

#include

SNESpad nintendo = SNESpad(2,3,4);

int buttonState = LOW;
int lastButtonState = LOW;

int state = 0;
int noteON = 144;//144 = 10010000 in binary, note on command
int noteOFF = 128;//128 = 10000000 in binary, note off command
void setup() {

Serial.begin(115200);
}

void loop() {
state = nintendo.buttons();

buttonState = (state);

if (buttonState != lastButtonState) {
if (state & SNES_B == 256 ) {
MIDImessage(noteON, 50);
}
else {
MIDImessage(noteOFF, 50);
}

lastButtonState = buttonState;
}
}

//send MIDI message
void MIDImessage(int command, int MIDInote) {
Serial.write(command);//send note on or note off command
Serial.write(MIDInote);//send pitch data
Serial.write(100);//send velocity data

}

thank you in advance for all your help

  state = nintendo.buttons();
 
    buttonState = (state);

Why are you using two variables for the same thing? Why are the parentheses used in the second statement?

Put each { on a new line. Use Tools + Auto Format
to correct that horrid
indenting. See if that doesn't
make the problem more obvious.

state will have a different value when the A switch is pressed, when the A switch is released, when the B switch is pressed, when the B switch is released, and when the A and B switches are both pressed.

You will have a hard time dealing with the A and B switches separately, when you use only one variable to remember the previous state.

You need to separate the A and B components of state into buttonStateA and buttonStateB, and have lastButtonStateA and lastButtonStateB variables, too.

Terminate whatever application is currently receiving the serial data. Open the Serial Monitor, instead. Debug the program, then comment out the debug statements. Then, you can re-run the application that is currently receiving data and have it receive good data.