Only read the serial monitor if there is no function running

So I have a function running a motor controller and I wanted to make it so that the function only runs when the motor is not running. Currently I have it set up that whenever a function is called a variable “e” is set to 1, and when the function is done “e” is set to 0. The code is set to only check what function to run if “e” = 0. For some reason, even if 1 is pressed while “e” = 1, it still runs the function after the function is done. Any ideas? I just want it so that it ignores any input while the wheel movement function is running

–CODE BELOW–
//loop code
void loop() {
if (e == 0) {
while(Serial.available()){
user_input = Serial.read(); //Read user input and trigger appropriate function

if (user_input ==‘1’)
{

StepForwardDefault();

}
else if(user_input ==‘2’)
{
ReverseStepDefault();
}
else if(user_input ==‘3’)
{
Step2ForwardDefault();
}
else if(user_input ==‘4’)
{
Reverse2StepDefault();
}
else if(user_input ==‘a’)
{
MoveForward();
}
else if(user_input ==‘b’)
{
MoveBackward();
}
else if(user_input ==‘c’)
{
TurnLeft();
}
else if(user_input ==‘d’)
{
TurnRight();
}
else
{
Serial.println(“Invalid option entered.”);
}
resetBEDPins();
}
}
}

//sample function

void StepForwardDefault()
{
e = 1;
Serial.println(“Moving forward at default step mode.”);
digitalWrite(dir, LOW); //Pull direction pin low to move “forward”
for(x= 0; x<3200; x++) //Loop the forward stepping enough times for motion to be visible
{
digitalWrite(stp,HIGH); //Trigger one step forward
delayMicroseconds(100);
digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
delayMicroseconds(100);
}
Serial.println(“Enter new option”);
Serial.println();
e = 0;
}

–END CODE–
Any idea why this doesn’t work?

First of all, to make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to my text editor.

Secondly, in response to your Title, an Arduino has no means to know when there is no function running because it only has a single CPU. Have a look at how the code is organized in Several Things at a Time. Note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete a task in one call. And there may be dozens of calls to a function before it is actually time for it to do anything.

Then have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data.

...R

The Arduino does not have an operating system so everything is executed in a serial fashion. In other words the StepForwardDefault() function does not execute at the same time as the loop() function. loop() calls StepForwardDefault() and when StepForwardDefault() finishes execution the next code that is executed is the code that follows the StepForwardDefault() call.

What is likely happening is that if you press any key again while any of those functions are running the serial library will buffer the input until you can read it and it will be waiting when you return to loop(). Therefore Serial.available() will indicate there is serial data available. You can clear the serial input buffer by executing the following before returning:

while(Serial.available()) Serial.read();

Todd's solution seemed to work. Thanks for the help. And robin, I will be sure to put my code in the code button next time.