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 {

    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,


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().

  • This might help. There is a discussion about calling a class function from an ISR.
    – Nick Gammon
    Commented Jan 25, 2017 at 5:20

2 Answers 2


The problem is that echo_check isn't void echo_check() but void echo_check(myclass *this) because it's a member function of a class.

You can make the whole thing static (along with run()) by:

class myclass {
        static void run();
        static void echo_check();

static void myclass::run() {

void myclass::echo_check() {

And then you call:


instead of instantiating an object.

  • mm seems to solve the problem, but I'm getting now compiler errors wherever the class members are used (and also inside the class definition): "invalid use of member 'Ultrabot::_xxxxxxx' in static member function" Commented Jan 24, 2017 at 22:54
  • 1
    A static function can only call other static functions or global normal functions, or functions of other classes that are defined as static members.
    – Majenko
    Commented Jan 24, 2017 at 22:57
  • So now I have compiling functions which can't make what they are supposed to? :/ Commented Jan 24, 2017 at 23:08
  • I don't know since you never bothered to show us your real code, only some tiny snippet.
    – Majenko
    Commented Jan 24, 2017 at 23:13
  • 1
    It's not about bothering to do it or not, it's about trying not to bother you with all my partially built code. I hope it's more clear now with the edit. Commented Jan 24, 2017 at 23:25


I moved the event handler out of the class, and made it static void.

Then the code compiled properly


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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