[SOLVED] Own Library says no matching function for call to

Hello,

I hope you can help me… I am trying to write my own library for some functions. In all there are 4 or 5 libraries… Now I have the following problem:

sha_switch.cpp:37:47: error: no matching function for call to 'attachInterrupt(int&, <unresolved overloaded function type>, InterruptMode)'
     attachInterrupt(_inputPin, lever,  FALLING);
                                               ^

Here is my sha_switch.h:

#include "application.h"

#ifndef sha_switch_h
#define sha_switch_h

class SHASwitch {
    
    public:
    
        SHASwitch();
        
        void setup(int inputPin, int pwmPin, int relais1, int relais2);
        bool turnOff();
        bool turnOn();
        bool lever();
        
    private: 
    
        int _inputPin;
        int _pwmPin;
        int _relais1;
        int _relais2;
        int _state;
        
};

#endif

Here is my sha_switch.cpp:

#include "sha_switch.h"

SHASwitch::SHASwitch() {
    
}


void SHASwitch::setup(int inputPin, int pwmPin, int relais1, int relais2) {
    
    /* ----- Setting Up Pins for Lever ----- */
    
    pinMode(inputPin,INPUT);
    pinMode(pwmPin, OUTPUT);
    
    /* ----- Setting Up Pin for Relais ----- */
    
    pinMode(relais1,OUTPUT);
    pinMode(relais2, OUTPUT);
    
    /* ----- Save Pins into private variables ----- */
    
    _inputPin = inputPin;
    _pwmPin = pwmPin;
    _relais1 = relais1;
    _relais2 = relais2;
    
    /* Setup Interrupt */
    
    attachInterrupt(_inputPin, lever, FALLING);
    
}

bool SHASwitch::turnOff() {
    
    digitalWrite(_relais1, HIGH);
    digitalWrite(_relais2, LOW);
    
    delay(100);
    
    digitalWrite(_relais1, LOW);
    
    _state = 0;
    
    return true;
    
}

bool SHASwitch::turnOn() {
    
    digitalWrite(_relais2, HIGH);
    digitalWrite(_relais1, LOW);
    
    delay(100);
    
    digitalWrite(_relais2, LOW);
    
    _state = 1;
    
    return true;
    
}

bool SHASwitch::lever() {
    
    switch(_state) {
        
        case 1:
        
            turnOff();
        
        case 0:
        
            turnOn();
            
    }
    
    return true;
    
}

I think you would find the error quickly… I also take a look in some communities and also locked into other libraries how they have done this… but I have no more ideas…

Best greeting,

Stefan

I think the correct syntax is:

attachInterrupt(_inputPin, &SHASwitch::lever, this, FALLING);

It’s weird, but it’s necessary when the interrupt handler is a non-static class member function.

Also, you can’t call delay from an interrupt handler. You can call delayMicroseconds but 100 milliseconds is a long time to delay in an interrupt hander; you should probably find a different way to implement that, like setting a flag and doing turnOn and turnOff from loop.

Also, if the input is a physical switch, you probably will need debouncing. In that case, you probably will want to skip the interrupt and just do it all from loop.

2 Likes

@rickkas7

Thanks for your answer! I have tried this before… I get the following error:

sha_switch.cpp:38:64: error: no matching function for call to 'attachInterrupt(int&, bool (SHASwitch::*)(), SHASwitch* const, InterruptMode)'
     attachInterrupt(_inputPin, &SHASwitch::lever, this, FALLING);

Best greeting!

The interrupt handler must be void, not return a bool. Sorry, I didn’t notice that initially.

void lever();
3 Likes

Thanks! That was the solution! Now everything is working really nice.

Best greeting!