How to avoid using delay in classes?

Hi all, tl;dr: What is the proper way of putting a delay in a class function?

background: I'm trying to write a class for brushless motor control using an ESC. There are some commands which require waiting (i.e. in order to arm the motor I need to send 0 throttle and wait for 100 ms), so if I only wanted to controll one motor and do nothing else I might have used:

setThrottle(0);
delay(100);

But I want to be able to use many instances of the class, along with some other tasks, so it doesn't make sense to hold everything for 100ms for each motor.

So I thought about using an additional variable (int breakEnds) in the class, and then create my own wait function:

void motorWait(int waitTime){
    breakEnds = millis()+waitTime;
}

Now each time the motor gets a command, I can check if millis()>breakEnds, so if that specific motor is currently on hold, it will just ignore the command. But now I need to actively check if the motor is waiting any time I want to do something with it, which is both error-prone and plain annoying, and I also need to return a value from each public function, so that a command will be re-sent if ignored.

Which brings me back to my question: What is the proper way of putting a delay in a class function?

thanks a lot for any input! :)

You're on the right track with the 'motorWait' function.

Each loop of loop() you'll need to poll the class instance so it can test for 'millis()>breakEnds' and run code if needed.

Just how the blink without delay code would poll the time using an if statement instead of a class function (containing the 'if').

But now I need to actively check if the motor is waiting any time I want to do something with it, which is both error-prone and plain annoying, and I also need to return a value from each public function, so that a command will be re-sent if ignored.

Just poll the class indiscriminately, the class can decide itself if there is work to do, and if there is a timeout.

Ordinarily I would say you should make the main program wait for this kind of setup action. Usually if you have a significant device like a motor you can't do anything useful until it starts. A robot can't start moving until the motors are online so it doesn't need to check sensors or do any other output during that time.

But streamlining many motors to start simultaneously, that's a good idea. I would not do it in a class. If running the motors is one of the core functions of your sketch, it should be in the main sketch and not a class. Only use a class when it encapsulates a single purpose that isn't highly dependent on other things. You might make a ManyMotors class that runs all the motors independently of the rest of the sketch.

You could try creating a member function call it update() for example which constantly checks the timer.

Put that update() function in loop.

pYro_65: You're on the right track with the 'motorWait' function.

Each loop of loop() you'll need to poll the class instance so it can test for 'millis()>breakEnds' and run code if needed.

Just how the blink without delay code would poll the time using an if statement instead of a class function (containing the 'if').

Just poll the class indiscriminately, the class can decide itself if there is work to do, and if there is a timeout.

That's more or less what I'm doing now. I was wondering if there is a smarter way (maybe using interrupts?)

MorganS: Ordinarily I would say you should make the main program wait for this kind of setup action. Usually if you have a significant device like a motor you can't do anything useful until it starts. A robot can't start moving until the motors are online so it doesn't need to check sensors or do any other output during that time.

But streamlining many motors to start simultaneously, that's a good idea. I would not do it in a class. If running the motors is one of the core functions of your sketch, it should be in the main sketch and not a class. Only use a class when it encapsulates a single purpose that isn't highly dependent on other things. You might make a ManyMotors class that runs all the motors independently of the rest of the sketch.

The arming is just one example, the other being the ESC command fetching rate which is 100Hz, so I need the class to wait for 10ms between each two commands.

I might be mistaken, but I don't see wy the user should be concerned with the waiting, instead of it being handled by the class, in a way that is transparent to the user.