I'm having a bad time trying to call a correctly-working expression (ultra[currentSensor].ping_timer(echoCheck)
) from an own class method (myclass::run
) instead of from void loop()
.
I'm getting the following compiler error: "invalid use of non-static member function".
If I call ultra[currentSensor].ping_timer(echoCheck)
directly from void loop()
it gives me no error, so I may be missing something.
Here's the (shortened) piece of code which is generating the error:
#include <NewPing.h>
#define ULTRA_NUM 3
#define MAX_DISTANCE 200
class myclass {
public:
void run();
void echoCheck();
unsigned int distance[ULTRA_NUM];
uint8_t currentSensor = 0;
};
NewPing ultra[ULTRA_NUM] = {
NewPing(4, 5, MAX_DISTANCE),
NewPing(6, 7, MAX_DISTANCE),
NewPing(8, 9, MAX_DISTANCE)
};
void myclass::run() {
// ....
ultra[currentSensor].ping_timer(echoCheck); // Compiler error
}
void myclass::echoCheck() {
if (ultra[currentSensor].check_timer())
distance[currentSensor] = ultra[currentSensor].ping_result
}
ping_timer()
is defined as it follows:
void NewPing::ping_timer(void (*userFunc)(void), unsigned int max_cm_distance) {
if (max_cm_distance > 0) set_max_distance(max_cm_distance);
if (!ping_trigger()) return;
timer_us(ECHO_TIMER_FREQ, userFunc);
}
I hope any of you can help me!
Thanks in advance,
Eduardo
Edit:
What I am trying to do is to adapt this example (from NewPing Arduino Library) so that the sonar ping (sonar[currentSensor].ping_timer(echoCheck)
) is done from a class method, and not from void loop()
.