I have been using a different method...
unsigned long nextVoidNoArgsFunction = 0;
long returnedValue = 0;
void setup() {
Serial.begin(9600);
}
void doFunctionAtInterval( void ( *callBackFunction )(), unsigned long *nextEvent, unsigned long interval ) {
unsigned long now = millis();
if ( now >= *nextEvent ) {
*nextEvent = now + interval;
callBackFunction();
}
}
void loop() {
doFunctionAtInterval(voidNoArgsFunction, &nextVoidNoArgsFunction, 400);
}
void voidNoArgsFunction() {
returnedValue = value = random(0,100);
Serial.println(returnedValue);
}
This has worked great for me... as long as the callback function is void and has no arguments.
I'd like to be able to use code such as below in the loop():
doFunctionAtInterval(voidOneArgFunction(&returnedValue), &nextVoidOneArgFunction, 1000);
but I have not had any success getting things to work. See below:
unsigned long nextVoidNoArgsFunction = 0;
unsigned long nextVoidOneArgFunction = 0;
long returnedValue = 0;
void setup() {
Serial.begin(9600);
}
void doFunctionAtInterval( void ( *callBackFunction )(), unsigned long *nextEvent, unsigned long interval ) {
unsigned long now = millis();
if ( now >= *nextEvent ) {
*nextEvent = now + interval;
callBackFunction();
}
}
/*
// Trying to overload doFunctionAtInterval here, but get compiler error on callbackFunction(*alue); line
void doFunctionAtInterval( void ( *callBackFunction )(float *value), unsigned long *nextEvent, unsigned long interval ) {
unsigned long now = millis();
if ( now >= *nextEvent ) {
*nextEvent = now + interval;
callBackFunction(*alue);
}
}
*/
void loop() {
doFunctionAtInterval(voidNoArgsFunction, &nextVoidNoArgsFunction, 400);
/*
// No success here with efforts so far, but this is the shape of how I'd like it to work
doFunctionAtInterval(voidOneArgFunction(&returnedValue), &nextVoidOneArgFunction, 1000);
*/
// This works, but feels like cheating
doFunctionAtInterval(encapsulatingVoidOneArgFunction, &nextVoidOneArgFunction, 1000);
}
void voidOneArgFunction(long *value) {
*value = random(0,100);
}
void voidNoArgsFunction() {
Serial.println(returnedValue);
}
void encapsulatingVoidOneArgFunction() {
voidOneArgFunction(&returnedValue);
}
Can anyone help please?