Can't use attachInterrupt in a library
arduino, interrupt, static-libraries
Solution
So the compiler (not the IDE) tells you exactly what's wrong:
argument of type 'void (HCSR04Interrupt::)()' does not match 'void (*)()
So, while `attachInterrupt()` takes a function pointer of type `void (*)()`, you're trying to pass it a non-static member function, which you can't. You can try making the member function `static` and casting:
static void echoHigh();
// ...
attachInterrupt(_echo_pin - 2, reinterpret_cast<void (*)()>(&echoHigh), RISING);
Problem
I'm writing a simple library for an ultrasonic distance sensor and thought i'd try using interrupts. However i can't set my functions in the `attachCallback` method properly. I want `HCSR04Interrupt::echoHigh()` and `HCSR04Interrupt::echoLow()` called when the pin goes high and low respectively. I've Googled this to no avail. The Ardiuno IDE says the following: ``` ./Arduino/libraries/HCSR04/HCSR04Interrupt.cpp: In member function 'void HCSR04Interrupt::getDistance()': ./Arduino/libraries/HCSR04/HCSR04Interrupt.cpp:31: error: argument of type 'void (HCSR04Interrupt::)()' does not match 'void (*)()' ./Arduino/libraries/HCSR04/HCSR04Interrupt.cpp: In member function 'void HCSR04Interrupt::echoHigh()': ./Arduino/libraries/HCSR04/HCSR04Interrupt.cpp:47: error: argument of type 'void (HCSR04Interrupt::)()' does not match 'void (*)()' ``` Here is my header: ``` #ifndef _HCSR04Interrupt_ #define _HCSR04Interrupt_ #include "Arduino.h" #define HCSR04_CM_FACTOR 58.0 #define HCSR04_IN_FACTOR 148.0 #define HCSR04_CM_MODE 0 #define HCSR04_IN_MODE 1 class HCSR04Interrupt { public: double distance; HCSR04Interrupt(int trigger_pin, int echo_pin, void (*callback)()); void setUnits(int units); void getDistance(); private: int _trigger_pin; int _echo_pin; int _units; unsigned long _micros_start; void (*_callback)(); void initialize(); void echoHigh(); void echoLow(); }; #endif ``` And my implementation (not complete since i cant get past the attachInterrupt step): ``` #include "Arduino.h" #include "HCSR04Interrupt.h" HCSR04Interrupt::HCSR04Interrupt(int trigger_pin, int echo_pin, void (*callback)()) { _trigger_pin = trigger_pin; _echo_pin = echo_pin; _callback = callback; initialize(); } void HCSR04Interrupt::setUnits(int units) { _units = units; } void HCSR04Interrupt::initialize() { pinMode(_trigger_pin, OUTPUT); pinMode(_echo_pin, INPUT); digitalWrite(_trigger_pin, LOW); } void HCSR04Interrupt::getDistance() { //Listen for the RISING interrupt attachInterrupt(_echo_pin - 2, echoHigh, RISING); //The trigger pin should be pulled high, digitalWrite(_trigger_pin, HIGH); //for 10 us. delayMicroseconds(20); //Then reset it. digitalWrite(_trigger_pin, LOW); } void HCSR04Interrupt::echoHigh() { _micros_start = micros(); detachInterrupt(_echo_pin - 2); attachInterrupt(_echo_pin - 2, echoLow, FALLING); } void HCSR04Interrupt::echoLow() { detachInterrupt(_echo_pin - 2); unsigned long us = micros() - _micros_start; distance = us; (*_callback)(); } ```