Interrupt handler inside C++ class

I have written an interrupt handler inside a sketch and copied essentially the same code to a C++ class. It works fine in the sketch but not in the class.

I remember stumbling across this awhile ago but I don’t remember the solution. How do I get an interrupt handler working inside a C++ class?

I have included my test program and my .h and .cpp files. I am trying to get four variants working:

  1. .ino file handles using polling (works)
  2. .ino file handles using interrupts (works)
  3. .cpp file handles using polling (works)
  4. .cpp file handles using interrupts (does not work)

BJDetectorTester.zip (8.98 KB)

There is nothing in your zip file numbered 1, 2, 3, or 4. Some real names would be useful.

Some better description of "not working" would also be useful. The code does something. You expect it to do something. All we know is that the two things are not the same thing. Assuming, of course, that the code even compiles.

There are three files in the ZIP file:

  1. BJDetectorTester.ino
  2. BJMotionDetector.h
  3. BJMotionDetector.cpp

The .ino file can be compiled in four different ways using the #define statements on lines 89 and 92. The code is currently set with both defines as true which means that it will use the Interrupt Handling code in the .cpp file. What isn't working is the interrupt handler in the cpp file. None of the interrupts are firing.

If I change the USE_INTERRUPT_CLASS define to false, the Interrupt handlers in the .ino file will be used. This works fine. Using the interrupt handler inside a class does not work. That is what I am seeking help on.

You can't use a member function as an interrupt. The member function has to know what instance it is a part of. Just the method by itself without being part of an instance is meaningless. If you want an interrupt function defined in a class then it MUST be a static member so it belongs to all instances of the class and it doesn't matter which instance you call it from. But then it can't touch any non-static members of the class.

The normal way to handle this is to define a function out in the .ino that acts as the interrupt handler and just calls the member function you want called.

Delta_G:
You can’t use a member function as an interrupt. The member function has to know what instance it is a part of. Just the method by itself without being part of an instance is meaningless. If you want an interrupt function defined in a class then it MUST be a static member so it belongs to all instances of the class and it doesn’t matter which instance you call it from. But then it can’t touch any non-static members of the class.

The normal way to handle this is to define a function out in the .ino that acts as the interrupt handler and just calls the member function you want called.

Or… Make the handler a static member function, and add another function that stores a pointer to the instance the handler is to act on as static member data.

Regards,
Ray L.

RayLivingston:
Or.... Make the handler a static member function, and add another function that stores a pointer to the instance the handler is to act on as static member data.

Or, have some sort of "interrupt delegator function" with some logic to work out which member/function it's supposed to call. Maybe even more than one (a list of listeners).

It all boils down to the same thing: all member functions have a hidden extra parameter put there by the compiler - the address of the class instance. Interrupts don't pass any parameters, they just go "ping!".

See my post about Calling an ISR from a class.

I have been a programmer for closing in on 50 years ...

About the same time as me, then. :slight_smile: