Serial error

My code functions as intended and I'm getting an error in the serial monitor that is more of an annoyance than anything, but I still would like to figure out why it's happening.

The program is designed to switch between turning the brightness of an LED up or down ('2'), or to adjust the delay time between a blinking LED ('1'). The serial monitor asks which mode I would like to select, and if I select '2', the second if condition, the 'invalid choice' message from the else condition is displayed on the serial monitor, which is followed by the message from the 2nd if condition and the program functions as expected. Why is the message from the else condition popping up?

const byte pot = A1;
const int LED = 13;
int dt = 0;
String choice;

void setup(){
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
  Serial.println("Which function would you like to perform?");
  Serial.println("1 for blink, 2 for brightness");
  }


void loop() {
  while (Serial.available() == 0){}
  choice = Serial.readString();
  
  if (choice == "1"){
    Serial.println("Turn the pot to adjust the blinking speed");
    while(Serial.available() == 0){

      int val = analogRead(pot); // reads pot value and stores it
      val = map(val, 0, 1023, 0, 255); // re-maps reading to 0-255
      dt = val + 30; // adds 30ms so bottom range of dt still blinks
    
      digitalWrite(LED, HIGH);
      delay(dt);
      digitalWrite(LED, LOW);
      delay(dt);
    }
  }

    if (choice == "2"){
    Serial.println("Turn the pot to adjust brightness");
    while (Serial.available() == 0) {

      int val = analogRead(pot); // reads pot value and stores it
      val = map(val, 0, 1023, 0, 255); // re-maps reading to 0-255
    
      analogWrite(LED, val);
    }
  }
  
  else {
    digitalWrite(LED,LOW);
    Serial.println("Invalid Choice. 1 for blink, 2 for brightness");
    
  }
}

Like Delta_G mentioned, that second 'if' should probably be an 'else if'.

Don't use Strings. You can just read a char, and if you need more than one character, use a null-terminated character array.
It will also allow you to write non-blocking code (by avoiding Serial.readString()).

Take a look at Blink Without Delay.

const byte pot = A1;
const byte LED = 13;

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
  while(!Serial);
  Serial.println("Which function would you like to perform?");
  Serial.println("1 for blink, 2 for brightness");
}

void loop() {
  static byte choice = 0;
  if (Serial.available() > 0) {
    char serialChar = Serial.read();
    if (isspace(serialChar)) // ignore white space
      return;
    if (serialChar == '1') {
      Serial.println("Turn the pot to adjust the blinking speed");
      choice = 1;
    } else if (serialChar == '2') {
      Serial.println("Turn the pot to adjust brightness");
      choice = 2;
    } else {
      digitalWrite(LED, LOW);
      Serial.println("Invalid Choice. 1 for blink, 2 for brightness");
      choice = 0;
    }
  }
  static bool LED_state = HIGH;
  static unsigned long previousMillis = 0;
  unsigned int val = analogRead(pot); // reads pot value and stores it

  if (choice == 1) {
    unsigned int dt = val >> 2; // re-maps reading from [0, 1023] to  [0, 255]  (10 bits - 2 bits = 8 bits)
    dt += 30; // adds 30ms so bottom range of dt still blinks
    if (millis() - previousMillis > dt) {
      LED_state = !LED_state;
      digitalWrite(LED, LED_state);
      previousMillis += dt;
    }
  } else if (choice == 2) {
    val = val >> 2; // re-maps reading from [0, 1023] to  [0, 255]
    analogWrite(LED, val);
  }
}

Pieter

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

And there is a simple user-input example in Planning and Implementing a Program

...R