Breaking while loops inside class .cpp file.

Hi All.
A quick question about breaking out of while statements in class objects.

I have a function within a class that, when called, sets a flag within the same class to true.
Within the function is a while statement scanning for that flag and, while true, it executes. Naturally, this creates a loop that will run until the flag is set to false.
The thing is, that flag is only set to false from within a different function which, while the loop is running, never gets called...

assume for simplicity that my void loop looks like this...

void loop(){
class::classObject.function1(); //flag within class' .cpp file set to true. while(flag==true) loop runs 
                                  happily.
delay(1000);
class::classObject.function2(); //wants to set the flag to false and break the while loop, but never 
                                  called as function 1 doesn't return to allow the void loop() to 
                                  continue.
delay(1000);
}

I need a way to continue running the first function from the Class, until another function is called from within void loop().
I guess I need some kind of asynchronicity to keep running the void loop() and enable the second function to set the class flag to false and break the loop created in the first function?

Can you change the while to an if and call function1 frequently from loop(), thus giving you an opportunity to call function2 to set/unset the flag thus causing function1 to stop executing the dependant code ?

Thanks for the reply.

The trouble is, I’m running a stepper motor and within the class function is a nested for loop driving the coil pattern. Also, everything to do with how long the stepper runs for is determined from variables within the class .cpp file. So I’d like to keep void loop() as clean as possible by just calling the “stepper go” function until and “stepper stop” function in the void loop is called. Otherwise I’m bringing in a lot of stuff from the class .cpp file unnecessarily.

I think that makes sense, right?

Are you using one of the existing stepper driver libraries, or code you wrote yourself?

For stepper control, the best way to go is to use a hardware timer, in compare mode, to generate the steps. Any changes in step timing, like acceleration and deceleration, can then be handled in an interrupt handler triggered by the timer compare match. Then you can do whatever you need to in the foreground, and the stepper will keep running as you want. If your stepper driver requires running in a loop to properly time the steps, you'll never get it to work like you want.

I think that makes sense, right?

It may make sense in terms of how your program is currently written, but as it not doing what you want it would seem that it is not a viable solution and needs to be restructured

Thanks for the replies.

Ray, could you go into more detail about the hardware timer and compare mode? How would that be implemented?

jack_prior:
Thanks for the replies.

Ray, could you go into more detail about the hardware timer and compare mode? How would that be implemented?

It is very chip-dependant, as you have to program a timer at the register level. You'll be limited to using a timer the Arduino core for the board you're running is not already using. Basically, setup a timer in compare mode, so each time a compare occurs, the compare output pin, which drives the step pin, toggles, AND an interrupt is generated. The interrupt handler takes care of tracking the step count, determining the step periods, etc. SOMEBODY must have such code for the common Arduinos.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.