Control Led with one key

Hello, how to proggram it to control on/off led with one key

Exam

void setup()
{
    pinMode(13, OUTPUT);
    Serial.begin(9600);
}

void loop()
{
    int val; 
    if(Serial.available()){
    delay(100);
    
    while (Serial.available() > 0){
    val=Serial.read();
      
    if(val == '1'){digitalWrite(13, HIGH); }
    else if(val=='0'){digitalWrite(13,LOW);}
    }
    }
}

I just want to control it with one key.. How to do that?

how to proggram it

That depends on what "it" is.

I just want to control it with one key.. How to do that?

Define your requirements. Which key?

Do you mean that the arrival of a value at the serial port should turn the LED on if it is off and off if it is on?

If so, create a global or static variable called ledState. Then,

   if(val == valOfInterest)
      ledState = !ledState;
   digitalWrite(ledPin, ledState);

You need a boolean flag, called say ledIsOn.

Then your test for the arrival of the "1" will invert the flag and write the flag value to the pin, something like this... Not tested or compiled.

bool ledIsOn = false;

void setup()
{
    pinMode(13, OUTPUT);
    Serial.begin(9600);
}

void loop()
{
    int val; 
    if(Serial.available()){
    delay(100);
    
    while (Serial.available() > 0){
    val=Serial.read();
      
    if(val == '1')
   {
     ledIsOn = !ledIsOn;
       digitalWrite(13, ledIsOn);
   }
    }
}
}

edit... as PaulS said.

Isn't that example inside the arduino basics tutorials ? Control LED 's with key, or switch, pushing or clicking and so on ?