How to stop a function part way through and do something else?

I am writing a program to implement different functions for motor control where the commands to initiate a particular function is received via Ethernet.

Is there a way to send and implement a "stop" command so that if a function is mid way through will be stopped so that a different command can be sent to call a different function?

I thought about interrupts, but how can an interrupt be initiated via an Ethernet command while a function is being processed?

You could return from the function, allowing another function to be called.

No, you can not stop a function. You could use variables and flags to control the function.
If you have a function with many delays you might have to rewrite it. With a sequence from a table or with millis() it is easier to stop that.

The trick is not to use functions in such a way that they are "mid way through" when you want something else to happen.

Have a look at how the functions work in several things at a time. Each function is called repeatedly many times per second so it is easy to change the way they behave at any time. Because each function is quick there is no problem allowing it to complete.

You may also find some useful stuff in planning and implementing a program

...R

No, you can not stop a function.

Really? What about interrupts and an associated ISR?

econjack:
Really? What about interrupts and an associated ISR?

Pause, not stop.

Thank you for the comments and links, I will take a look.

The first function I am working on controls two motors with PID and makes them rotate between two angles continuously until told to stop via the command send via Ethernet, it’s this stopping on command I need to figure out. This is the first function that Im working on. The for loop is just there so that the function does not continuously run for testing.

//processCommand() to determine what command has been received
void processCommand(String command)
{
  server.print("Processing Command ");
  server.println(command);

  if(command.indexOf("go")>-1){
    
    angleELsetpointA=50;
    angleELsetpointB=150;
    server.println("setting angle A= " );
    server.println(angleELsetpointA);
    server.println("setting angle B= " );
    server.println(angleELsetpointB);
    
    commandString = "";

for (int x=0; x<2; x++){
  while((int)angleEL != (int)angleELsetpointA)
  {
    readingEL = readPositionEL();                      //Get value from encoder
    angleEL = ((float)readingEL / 65536.0) * 360.0;    //Make that value in range of 0-360 degrees
    ELAPID.Compute();
    md.setM1Speed(speedEL);
    Serial.println(angleEL);
  }
  while((int)angleEL != angleELsetpointB)
  {
    readingEL = readPositionEL();                      //Get value from encoder
    angleEL = ((float)readingEL / 65536.0) * 360.0;    //Make that value in range of 0-360 degrees
    ELBPID.Compute();
    md.setM1Speed(speedEL);
    Serial.println(angleEL);    
  }  
}  
    
  }
   
    server.println("END OF ROUTINE...");    //Indicate that function has complete and will exit to loop.
    commandString = "";                     //Clear CommandString, ready for the next command.
    return;                                 //Return to loop
  
}

@AWOL: I guess I read this wrong:

Is there a way to send and implement a "stop" command so that if a function is mid way through will be stopped so that a different command can be sent to call a different function?

Terrorhertz:
The first function I am working on controls two motors

You need to take the WHILEs out of the function and arrange your code so it performs a single increment of the "WHILE" every time the function is called. That way the time in the function will be very short - perhaps just a few dozen microsecs.

...R

Robin, thanks for the advice, but I do not know of a way to make the function perform a single increment at a time if its not in a function while checking the condition for meeting the setpoint is true or false. My programming experience is being pushed with what I have come up with so far. Is there a name for this technique that you mention or examples?

I can’t see how this can be implemented by repeatedly calling the function, as it should be called once with the various setpoints (angle limits in this case), when the command is sent via Ethernet. I don’t know how practical this would be if the same command were to be repeatedly sent via Ethernet.

Terrorhertz:
I can't see how this can be implemented by repeatedly calling the function, as it should be called once with the various setpoints (angle limits in this case), when the command is sent via Ethernet. I don't know how practical this would be if the same command were to be repeatedly sent via Ethernet.

You can call a function once with the desired parameters and then each time through loop() change a variable to move towards the target angle. Replace the while with an if to prevent the code blocking until the target angle is reached. Something like this (untested)

  if ((int)angleEL != angleELsetpointB)
  {
    readingEL = readPositionEL();                      //Get value from encoder
    angleEL = ((float)readingEL / 65536.0) * 360.0;    //Make that value in range of 0-360 degrees
    ELBPID.Compute();
    md.setM1Speed(speedEL);
    Serial.println(angleEL);    
  }

Terrorhertz:
I can't see how this can be implemented by repeatedly calling the function,

I believe that is illustrated in the code in the links I gave you in Reply #3 - pretty much as @UKHeliBob has said.

...R

Thanks Bob and Robin, I printed out the whole sketch in the link, I didn't see the whole sketch last week! Hope I can make sense of it... I just hope the method can be applied without upsetting the PID control or sending of angular data back via Ethernet.