2

Currently to use TimedAction for a function a program would look like so:

#include <TimedAction.h>
void functionX();
TimedAction funtionTimed = TimedAction(1000, functionX);

void setup()
{

}


void loop()
{
functionTimed.check();
}


void functionX()
{
 //run function
}

My question is say I have an object class with some functions I would like to use with this TimedAction library. How would this be written?

1 Answer 1

1

You can't. The TimedAction class doesn't support the function prototype required for your undefined class (it has to match your class, so can't be written).

You have to call a normal function which then calls the class function, or you have to have your class function as static so it doesn't include the class in the function prototype.

class foo {
    public:
        static void runMe() { 
            // blah blah
        }
};

TimedAction funtionTimed = TimedAction(1000, foo::runMe);

However, TimedAction is pretty simplistic and you don't really need it. You can just write a function to only run every so often:

class foo {
    private:
        uint32_t _runMeTimestamp;
    public:
        void runMe() {
            if (millis() - _runMeTimestamp < 1000) return;
            _runMeTimestamp = millis();
            // blah blah
        }
};

foo myFoo;

void loop() {
    myFoo.runMe();
}

Those two lines of code, plus a variable to store the timestamp in, are all you need to make a function run at periodic intervals.

2
  • 1
    Thanks, yes I had been using it to call a normal function which then calls the class function, was just curious if I could reduce redundancy
    – walkman118
    May 26, 2018 at 14:50
  • Protip: You can re-write the library to use std::function instead of raw function pointers. You can then use lambda expressions or std::bind to call into a member function of a particular object. This would be the C++ way to do it. May 26, 2018 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.