Searial input within a function

Hello,
i am wondering if there is a good way to get the serial.read() to work inside of a function. I am looking to provide an input to the serial monitor which will then execute a function. then inside that function I would like to be able to send another input through the serial monitor. I am running two stepper motors and would like to set the number of steps per motor.

#define IN1 8

#define IN2 9

#define IN3 10

#define IN4 11

#define IN5 2

#define IN6 3

#define IN7 4

#define IN8 5

int Steps = 0;

void setup()

{

Serial.begin(9600);

pinMode(IN1, OUTPUT);

pinMode(IN2, OUTPUT);

pinMode(IN3, OUTPUT);

pinMode(IN4, OUTPUT);

pinMode(IN5, OUTPUT);

pinMode(IN6, OUTPUT);

pinMode(IN7, OUTPUT);

pinMode(IN8, OUTPUT);

 delay(1000);

}

void loop() {
  while (Serial.available()>0){
    if(Serial.read()== '1'){
      Serial.println("Left side");
      stepperLeft();
    }
    
    if(Serial.read()== '2'){
      Serial.println("Right side");
      stepperRight();
    }
    

    digitalWrite(IN1, LOW);

    digitalWrite(IN2, LOW);

    digitalWrite(IN3, LOW);

    digitalWrite(IN4, LOW);



    digitalWrite(IN5, LOW);

    digitalWrite(IN6, LOW);

    digitalWrite(IN7, LOW);

    digitalWrite(IN8, LOW);

  }
}

void stepperLeft(){
  delay(1000);
  while (Serial.available()>0){
float st = Serial.parseInt();
  
if(Serial.read()=='\n'){

String message = "Milimeters ";

message+=st;

Serial.println(message);

int doSteps = -st*4096;

delay(500);

int xw =abs(doSteps);
for (int x=0;x<xw;x++){

switch(Steps){

case 0:

digitalWrite(IN1, LOW);

digitalWrite(IN2, LOW);

digitalWrite(IN3, LOW);

digitalWrite(IN4, HIGH);

break;

case 1:

digitalWrite(IN1, LOW);

digitalWrite(IN2, LOW);

digitalWrite(IN3, HIGH);

digitalWrite(IN4, HIGH);

break;

case 2:

digitalWrite(IN1, LOW);

digitalWrite(IN2, LOW);

digitalWrite(IN3, HIGH);

digitalWrite(IN4, LOW);

break;

case 3:

digitalWrite(IN1, LOW);

digitalWrite(IN2, HIGH);

digitalWrite(IN3, HIGH);

digitalWrite(IN4, LOW);

break;

case 4:

digitalWrite(IN1, LOW);

digitalWrite(IN2, HIGH);

digitalWrite(IN3, LOW);

digitalWrite(IN4, LOW);

break;

case 5:

digitalWrite(IN1, HIGH);

digitalWrite(IN2, HIGH);

digitalWrite(IN3, LOW);

digitalWrite(IN4, LOW);

break;

case 6:

digitalWrite(IN1, HIGH);

digitalWrite(IN2, LOW);

digitalWrite(IN3, LOW);

digitalWrite(IN4, LOW);

break;

case 7:

digitalWrite(IN1, HIGH);

digitalWrite(IN2, LOW);

digitalWrite(IN3, LOW);

digitalWrite(IN4, HIGH);

break;


default:

digitalWrite(IN1, LOW);

digitalWrite(IN2, LOW);

digitalWrite(IN3, LOW);

digitalWrite(IN4, LOW);

break;
}

delay(1);

if(doSteps>=0){ Steps++;}

if(doSteps<0){ Steps--; }

if(Steps>7){Steps=0;}

if(Steps<0){Steps=7; }

      }
    }
  }
  Serial.end();
}

void stepperRight(){
  Serial.begin(9600);
  while (Serial.available()>0){
float st = Serial.parseInt();
  
if(Serial.read()=='\n'){

String message = "Milimeters ";

message+=st;

Serial.println(message);

int doSteps = -st*4096;

delay(500);
int yz =abs(doSteps);

for (int y=10;y<yz;y++){

switch(Steps){
case 10:

digitalWrite(IN5, LOW);

digitalWrite(IN6, LOW);

digitalWrite(IN7, LOW);

digitalWrite(IN8, HIGH);

break;

case 11:

digitalWrite(IN5, LOW);

digitalWrite(IN6, LOW);

digitalWrite(IN7, HIGH);

digitalWrite(IN8, HIGH);

break;

case 12:

digitalWrite(IN5, LOW);

digitalWrite(IN6, LOW);

digitalWrite(IN7, HIGH);

digitalWrite(IN8, LOW);

break;

case 13:

digitalWrite(IN5, LOW);

digitalWrite(IN6, HIGH);

digitalWrite(IN7, HIGH);

digitalWrite(IN8, LOW);

break;

case 14:

digitalWrite(IN5, LOW);

digitalWrite(IN6, HIGH);

digitalWrite(IN7, LOW);

digitalWrite(IN8, LOW);

break;

case 15:

digitalWrite(IN5, HIGH);

digitalWrite(IN6, HIGH);

digitalWrite(IN7, LOW);

digitalWrite(IN8, LOW);

break;

case 16:

digitalWrite(IN5, HIGH);

digitalWrite(IN6, LOW);

digitalWrite(IN7, LOW);

digitalWrite(IN8, LOW);

break;

case 17:

digitalWrite(IN5, HIGH);

digitalWrite(IN6, LOW);

digitalWrite(IN7, LOW);

digitalWrite(IN8, HIGH);

break;

default:

digitalWrite(IN5, LOW);

digitalWrite(IN6, LOW);

digitalWrite(IN7, LOW);

digitalWrite(IN8, LOW);

break;

}

delay(1);

if(doSteps>=10) { Steps++;}

if(doSteps<10){ Steps--;}

if(Steps>17){Steps=10;}

if(Steps<10){Steps=17;}

      }
    }
  }
  Serial.end();
}

I have used switch statements to call the function but I can not get that to start the serial monitor once inside that function. I am currently trying to use the if statements but an not having much luck with that either. i am not sure is i need to call Serial.end() in my loop function and restart it in each individual function. this is on an Arduino Uno board. Any assistance would be amazing. Any questions please about the code or project please ask.
thank you

No, you don't need to call end. One begin per program is enough.

The problem is that the function gets called and checks for serial data way way way faster than you can enter more. You have to think in terms of a state machine.

So when you get the first '1' or '2' set a variable, let's call it state, to 1 or 2. Then design the program so that if state is 1 or 2 you skip the first read in loop and go straight to your function. In your function check for serial input. If you got none then just return, nothing is happening yet. Loop runs again and calls your function again because state is still set to something. This is going to happen over and over a few thousand times while you are typing. Once you get some serial data, read it and do what you want with your stepper and set state back to 0 so the loop can run the initial check for serial instead of going to the function.

Also posted at: https://stackoverflow.com/questions/46700259/arduino-serial-red-inside-of-a-function-java

Hey, if you're going to just pepper the same question all over the internet, , at least do us the courtesy of linking to the other places you asked so we don't end up wasting time here giving you answers you already got there.

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

Notice how the collection of serial data takes place in its own function and then the data can be used anywhere in the program. This division of activity may be more obvious in Planning and Implementing a Program

Even if the serial data is only used in one of your functions I would still keep the serial input in a separate function because that makes debugging and testing very much easier.

...R

Thank you both This was very helpful. And I apologize for not linking to the stack social post.
Here is the code I used in case anyone else needs it. It is not the most elegant code but it does the ob quite nicely.

void loop() {
  if(state == 0){
  while (Serial.available()>0){
    int state = Serial.parseInt();
   if(Serial.read()== '\n'){
        if(state == 1){
       Serial.println("Left side");
       stepperLeft();
       }
    if(state == 2){
    Serial.println("Right side");
    stepperRight(); 
  } 
    
 }

    digitalWrite(IN1, LOW);

    digitalWrite(IN2, LOW);

    digitalWrite(IN3, LOW);

    digitalWrite(IN4, LOW);



    digitalWrite(IN5, LOW);

    digitalWrite(IN6, LOW);

    digitalWrite(IN7, LOW);

    digitalWrite(IN8, LOW);
    }
  } 
}

It is not the most elegant code

I’ll say.

  if(state == 0){
  while (Serial.available()>0){
    int state = Serial.parseInt();
   if(Serial.read()== '\n'){
        if(state == 1){

You now have two variables named state. Which one is the last line referring to?

They are supposed to be the same variable. I didn't need to declare it an int when i set

state = Serial.parseInt();

I have fixed this in my code so it is hopefully no longer confusing. I am also working n simplifying it down.