Need code review/design suggestion

Working with Arduino Uno I ended up in need of a way to perform different actions at different time intervals. For that purpose I decided to go with an abstract 'Routine class'

class Routine
{
protected:
unsigned int _delay;
public:
unsigned int _timePassed;

virtual int Delay() = 0;
virtual void DoWork() = 0;
};

from which all other routines will inherit. _delay will be assigned in constructor and tells how at what intervals do I need to perform this routine. TimePassed measures time since last action. Delay() is a simple accessor and DoWork() is an action that needs to be performed.

In my .Ino file in main loop I have this structure

for (unsigned int i = 0; i < RoutineCount; i++)
{
if (Routines*->Delay() == -1)*

  • {*
    _ Routines*->DoWork();_
    _
    }_
    else if (Routines->_timePassed + (millis() - LastMeasure) >= Routines->Delay())
    _ {
    Routines->DoWork();_

    Routines->timePassed = 0;*
    * }
    else*

    * {_
    Routines->timePassed += (millis() - LastMeasure);*
    * }
    }[/quote]
    And Routines[] is defined like so:
    > USSRoutine Sound(200);
    > Routine Routines[]

    > {
    > &Sound
    > };
    Is this a good approach? Is there room for improvement? Are there any caveats to it?_

In my .Ino file in main loop I have this structure

for (unsigned int i = 0; i < RoutineCount; i++){

if (Routines->Delay() == -1)   {
      Routines->DoWork();
   } else if (Routines->_timePassed + (millis() - LastMeasure) >= Routines->Delay())

I think you should hide this code inside the class, and then have one of these choices in loop():

   Sound.timeCheck();   // check each routine timeout individually
   Lights.timeCheck();
// or
   for (byte i=0; i<nRoutines; i++) {
      Routines[i]->timeCheck();
   }

I actually did something relatively similar, without C++ classes (cause I'm an old C programmer), which you can look at here:

Barsik_The_CaT:
Is this a good approach? Is there room for improvement? Are there any caveats to it?

I am definitely NOT a C/C++ expert. I only use it on Arduinos because I have no choice.

However it seems to me your approach is no more convenient than the basic system in Several Things at a Time

...R

Thanks for feedback, I've managed to clean up my code a little.
Just for clarification - I am using classes because there are multiple people working on this project and the amount of routines might change, so I am providing a simple interface for adding them.

classes are fine. One of the reasons I didn't "publish" my code more widely was because I realized "hey, this should obviously be a C++ class rather than just C code!"