Interfacing arduino +processing: Lagging response

I am running a program that uses both arduino +processing together. Processing creates a GUI with two buttons called F and L. Whenever either button is pressed, it should write either a “F” or “L” string to the port depending on the button pressed.

//processing code
if(mousePressed) {
if(button1.pressed()) {
port.write(‘F’);
}
else if(button2.pressed()) {
count=0;//reset count for user input’s sake
port.write(‘L’);
}
}

Simultaneously, arduino shd be in the background listening to the port, if a “F” is called, the ensuing code should be executed. This code basically reads an RFID tag

if (Serial.available()){ //If data is available to read,
val = Serial.read(); //read it and store it as val
}
switch(val){
case ‘F’:

if((val = RFID.read()) == 10)
{ // check for header
bytesread = 0;
//Serial.println("byte read: "+bytesread);
while(bytesread<10)
{ // read 10 digit code
val = RFID.read();
if((val == 10)||(val == 13))
{ // if header or stop bytes before the 10 digit reading
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
//Serial.println(“three”);
}
}

However, if ‘L’ is received, the ensuing code should be excecuted:
case 'L: Serial.println(“L”); //just to be simplistic

The problem I’m having is that: If I press the F button first, and then press the ‘L’ button, the F button is gonna keep looping for like 3 times before it executes the ‘L’ case. So, I think there’s a lag somewhere. How can I overcome this lag so that immediately I press the L button, the L case should be executed

its difficult to follow the logic in your Arduino case statement. Every case needs a break and I am not sure your break is executed when it should be. I suggest you replace the case with a set if if statements.

If that doesn't fix it, remove the rfid code and just verify that you can detect the incoming bytes . You may want to put these if statements inside the curley bracket used for the if(Serial.available()) code.

Start out simple and when you have the basics working add functionality in small steps.

Good luck