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;
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?_
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!"