I am attempting to set up an interrupt within a class. The class is a library for an HC SR04 sonic distance measurer. An instance of the class will take a trigger port and an interrupt port. When the trigger goes HIGH you wait for an interrupt on the input port. The time between the two events is the echo time and you do the math to get the distance. I am setting up the class to house this logic and am having difficulty getting the thing to compile.
spark_wiring_hcsr04.cpp: In constructor ‘HCSR04::HCSR04(uint16_t, uint16_t)’:
spark_wiring_hcsr04.cpp:18:31: error: invalid use of member function (did you forget the ‘()’ ?)
make: *** [spark_wiring_hcsr04.o] Error 1
Can I set up an interrupt within a class or does it have to be in the main program?
Tried to set it up in a method and in init()… no joy. I am also dusting off my C from a long time on the shelf and might be missing some nuance on what you have suggested.
Ok, apparently not… the attachInterrupt() routine needs a specific type of function pointer. This works, but obviously is not super pretty like if it was all wrapped up in a Class. I’m sure there’s a way to do it… just not thinking of it right now.
Well your initial problem (referencing the method from a Class in the attachInterrupt() ) is probably a feature request.
The other thing I was originally thinking you had a problem with was things like pinMode() and digitalWrite() not working in Class constructors. I guess that would have been your next problem. I would call this a bug.
Thanks! We’ve started talking about how best to accommodate initializing hardware before setup, we’ll let you know when we have a good path for fixing this.