How to set delay from processing ?

Hello i am trying to set delay from processing knob. But when i set the delay on the processing knob the LED never goes off it stays ON forever .

This is my arduino code:

int theValue;
int theValue1;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(13,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
while(Serial.available() >0);
char val=Serial.read();
switch(val)
{
  case 'c':
  theValue=Serial.read();
  break;
  case 'd':
  theValue1=Serial.read();
  break;
  
case 'r':
digitalWrite(13,HIGH);
delay(theValue);
digitalWrite(13,LOW);
delay(theValue1);

break;
}
}

This is my processing code:

import controlP5.*;

ControlP5 cp5a;
ControlP5 cp5b;
ControlP5 cp5c;
Knob myKnobA;
Knob myKnobB;
float n,n1;

import processing.serial.*;

Serial myPort;  // Create object from Serial class
String val;     // Data received from the serial port

void setup()
{
 size(400,600);
 noStroke();
  
  String portName = Serial.list()[1]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
  cp5a = new ControlP5(this);
  
   cp5a.addButton("Blink")
     .setValue(0)
     .setPosition(100,100)
     .setSize(200,19)
     ;
     cp5b=new ControlP5(this);
      myKnobA = cp5b.addKnob("knobON")
               .setRange(0,1000)
               .setValue(50)
               .setPosition(150,200)
               .setRadius(50)
               .setDragDirection(Knob.VERTICAL)
               ;
               
               
                    cp5c=new ControlP5(this);
      myKnobB = cp5b.addKnob("knobOFF")
               .setRange(0,1000)
               .setValue(50)
               .setPosition(150,360)
               .setRadius(50)
               .setDragDirection(Knob.VERTICAL)
               ;
}

void draw()
{
  background(0);

} 
 
 
 
 public void Blink(){
   myPort.write('r');
 }
 
 public void knobON(int theValue){
   myPort.write('c');
   myPort.write(theValue);
 }
 
 public void knobOFF(int theValue1){
   myPort.write(298);
   myPort.write(theValue1);
 }

Thank you.

while(Serial.available() >0);
char val=Serial.read();

As soon as there is something to read, loop forever.
If there's nothing to read, read it anyway.

How can i do that ?
I tried moving code to void setup but now it doesn't work.

Thing is, Serial.available() >0 also returns true if there is one ONE char. So you can only safely read a single char. And Serial is slow compared to the microcontroller going through the code, so probably there is only a single char in.

Or wait until you have received as many chars as you want to read, for example Serial.available() > 3.

Or make the code even better by making it more a state machine. See Serial Input Basics.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

...R