How to use attachInterrupt in class

Hi guys,

I am creating the class MyOptocoupler supposed to manage the optocoupler on my robot. I have created a methode begin() for the pinMode declaration and the attacheInterrupt function, but I have got an error:

C:\Users\USER\Documents\Arduino\libraries\MyOptocoupler\MyOptocoupler.cpp: In member function 'void MyOptocoupler::begin(int)':
C:\Users\USER\Documents\Arduino\libraries\MyOptocoupler\MyOptocoupler.cpp:29:31: error: cannot convert 'void (MyOptocoupler::*)()' to 'void (*)()' for argument '2' to 'void attachInterrupt(uint8_t, void (*)(), int)'
   attachInterrupt(0, p, CHANGE);
                               ^
Erreur lors de la compilation.

So do you know how to convert void (MyOptocoupler::)() to void ()() ?

I give you my code :

MyOptocoupler.h

#ifndef MyOptocoupler_h
#define MyOptocoupler_h

#if defined(__AVR__)
    #include "Arduino.h"
#elif defined(__PIC32MX__)
    #include "WProgram.h"
#elif defined(__arm__)
    #include "Arduino.h"
#endif

class MyOptocoupler
{
    public:
        MyOptocoupler(int pin);             
        void begin();                       
        void begin(int goalOfCount);
        void changement();                  
        bool isOutOfCount();    



    private:
        int _pin;
        int _goalOfCount;
        volatile int _counter;

};

#endif

My Optocoupler.cpp:

#include "MyOptocoupler.h"

    MyOptocoupler::MyOptocoupler(int pin): _pin(pin), _counter(0)
    {

    }

    void MyOptocoupler::begin()
    {
                //Does not metter it will be quite the same as begin(int goalOfCount)

        //_goalOfCount = 0;
        //pinMode(_pin, INPUT);
        //attachInterrupt(digitalPinToInterrupt(_pin), changement, CHANGE);
    }

    void MyOptocoupler::begin(int goalOfCount)
    {
        _goalOfCount = goalOfCount;
        pinMode(_pin, INPUT);
        void (MyOptocoupler::*p)();
        p = &MyOptocoupler::changement;
        attachInterrupt(0, p, CHANGE);
    }

    void MyOptocoupler::changement()
    {
        _counter++;
    }

    bool MyOptocoupler::isOutOfCount()
    {
        return _counter >= _goalOfCount;
    }

Thank you so much for your help.

Frank

The only way you can get a pointer to a class member function is if that function is defined static. This topic is beat to death on a regular basis in this forum. A search will turn up numerous threads that explain the few options you have.

Regards, Ray L.

I am creating the class MyOptocoupler supposed to manage the optocoupler on my robot.

Creating a class to deal with ONE instance of a piece of hardware is rarely useful. Why do you need a class?

Thank you for your answers ! The code I shared with you yesterday was an attempt but the first code I wrote was this:

void MyOptocoupler::begin(int goalOfCount)
    {
        _goalOfCount = goalOfCount;
        pinMode(_pin, INPUT);
        attachInterrupt(0, changement, CHANGE);
    }

And I don't uderstand why is it impossible. The error is:

C:\Users\USER\Documents\Arduino\libraries\MyOptocoupler\MyOptocoupler.cpp: In member function 'void MyOptocoupler::begin(int)':
C:\Users\USER\Documents\Arduino\libraries\MyOptocoupler\MyOptocoupler.cpp:29:40: error: cannot convert 'MyOptocoupler::changement' from type 'void (MyOptocoupler::)()' to type 'void (*)()'
   attachInterrupt(0, changement, CHANGE);
                                        ^
Erreur lors de la compilation.

And if I write this:

attachInterrupt(0, this->changement, CHANGE);

Here is the error:

C:\Users\USER\Documents\Arduino\libraries\MyOptocoupler\MyOptocoupler.cpp: In member function 'void MyOptocoupler::begin(int)':
C:\Users\USER\Documents\Arduino\libraries\MyOptocoupler\MyOptocoupler.cpp:29:46: error: cannot convert 'MyOptocoupler::changement' from type 'void (MyOptocoupler::)()' to type 'void (*)()'
   attachInterrupt(0, this->changement, CHANGE);
                                              ^
Erreur lors de la compilation.

[u]@PaulS :[/u]

I need a class because I think it is more friendly to use. Moreover some guys in my team of work do not really understand how to code....

Thank you again for your help.

Thank you for your answers !

You need to actually read them. Ray told you the answer in reply #1.

Here is an example I worked out with Ray's advice:

http://forum.arduino.cc/index.php?topic=362294.5;wap2