Serial Monitor: Waiting for input values from user by serial monitor

Hello,
I am asking for some help with the serial monitor on how I can make the monitor wait until a user inputs a value before it continues on through the loop. It's the part in the code where I am trying to get a value for long amt; in the switch case. The while loop with Serial.available does stop, but only for second then continues on through the rest of the loop.

void loop(){
  char decision = Serial.read();
  
  switch(decision){
    case '1':
      long amt = 0;
      Serial.println("In milliliters, how much gas would you like to capture? Enter below.");
      while (Serial.available()) {
        amt = Serial.parseInt();
        break;
        }
      O2Collection(electrodes,pump,amt);
      Serial.println("Oxygen collection finished. Would you like to do anything else?");
      break;
    case '2':
      break;
    case '3':
      break;
    case '4':
      break;
    case '5':
      break;
  }
  Serial.println("didnt work");
}

void O2Collection(int electrodes, int pump, long amt){
    tim = millis();
    Serial.println("Oxygen Collection Initiated. Type 'stop' to quit if necessary.");
    digitalWrite(electrodes, HIGH);
    digitalWrite(pump, HIGH);
    while(millis()<amt){
        if(Serial.readStringUntil('\n') == "stop"){
            goto finish;
          }
      }
    finish:
    digitalWrite(pump, LOW);
    digitalWrite(electrodes, LOW);
  }

The first thing you do in loop() is read a character without checking to see if there is one, using Serial.available(). If you do that, you will just be reading garbage.

read Robin2's tutorial Serial Input Basics for a much better way to gather user input

while(!Serial.available()); // This'll stop it.

-jimlee

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

Writing code for an effective user interface is one of the trickier parts of programming. You have to envisage, and allow for, all the stupid things that users do.

...R

    while(millis()<amt){

if(Serial.readStringUntil('\n') == "stop"){
            goto finish;
          }
      }
    finish:
    digitalWrite(pump, LOW);

Don't use goto's; it will result in spaghetti code. In your code, you coukd use break to break out of the while-loop.

The opinions are slightly divided, but in 30-plus years programming I have never had a need for it. Plan your code properly and you will not have a need for it.