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?