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