user33320: I am trying to make a library for Arduino, which requires an interruption, but the problem is that when I try the attachInterrupt function inside my library, there's a problem about attachInterrupt is static. Right now, in the library I have this in the header file:
#ifndef RegletaCMA_h
#define RegletaCMA_h

#include "Arduino.h"

class DimmerGlider {
public:
    DimmerGlider(int min, int max, int increase);

    int getMin() { return _min; }

    int getMax() { return _max; }

    int getIncrease() { return _increase; }

    void setMin(int min) { _min = min; }

    void setMax(int max) { _max = max; }

    void setIncrease(int increase) { _increase = increase; }

private:
    int _min, _max, _increase;
};

class Dimmer {

public:
    Dimmer(DimmerGlider &glider, int outputPin, int t3);

    void Dimmer::begin() {
        pinMode(_outputPin, OUTPUT);
        digitalWrite(_outputPin, false);
    }

    void Dimmer::dim() {
        delayMicroseconds(_DIMMER_counter);
        digitalWrite(_outputPin, true);  // Turn _DIMMER_pinDimmer on
        delayMicroseconds(_t3);
        digitalWrite(_outputPin, false); // Turn _DIMMER_pinDimmer off

        if (_DIMMER_dir) _DIMMER_counter += _glider.getIncrease(); // If dir is negative add _DIMMER_increase
        else _DIMMER_counter -= _glider.getIncrease();                  // If dir is negative substract _DIMMER_increase

        if (_DIMMER_counter > _glider.getMax()) _DIMMER_dir = false; // Change dir if reached bottom
        if (_DIMMER_counter < _glider.getMin()) _DIMMER_dir = true;  // Change dir if reached top

        _DIMMER_lastExecutionTime_micros = micros(); // Reset timer
    }

    void Dimmer::force(bool status) {
        digitalWrite(_outputPin, status);
    }

private:
    DimmerGlider &_glider;

    int _outputPin;

    int _DIMMER_counter = 1000;
    bool _DIMMER_dir = true;

    unsigned long long _DIMMER_lastExecutionTime_micros = 0;

    int _t3;
};

class DimmerImplementor {
public:
    DimmerImplementor(Dimmer dimmerList[], int optoPin);

    void DimmerImplementor::begin(){
        attachInterrupt(_optoPin, dimAll, RISING);
    }

    void DimmerImplementor::dimAllDimmers() {
        for (int c = 0; c < sizeof(_dimmerList); c++)
            _dimmerList[c].dim();
    }

    void DimmerImplementor::dimAll() {
        DimmerImplementor::dimAllDimmers();
    }

private:
    int _optoPin;
    Dimmer _dimmerList[];
};

#endif

and this in the cpp file:
#include "Arduino.h"
#include "RegletaCMA.h"

DimmerGlider::DimmerGlider(int min, int max, int increase) :
        _min(min),
        _max(max),
        _increase(increase) {
}

Dimmer::Dimmer(DimmerGlider &glider, int outputPin, int t3) :
        _glider(glider),
        _outputPin(outputPin),
        _t3(t3) {
}

DimmerImplementor::DimmerImplementor(Dimmer *dimmerList, int optoPin) :
        _dimmerList(dimmerList),
        _optoPin(optoPin) {
    for(int c = 0; c < sizeof(dimmerList); c++){
        DimmerImplementor::dimmers[c] = dimmerList[c];
    }

    for (int c = 0; c < sizeof(dimmers); c++) {
        Dimmer dimmer = dimmers[c];

        dimmer.begin();
    }
}

and when I try to run the library with the Arduino IDE, with this code:
DimmerGlider dimmer1Glider = DimmerGlider(_DIMMER_1_tempMin, _DIMMER_1_tempMax, _DIMMER_1_increase);
Dimmer dimmer1 = Dimmer(
                   dimmer1Glider,
                   _DIMMER_1_pin,
                   _DIMMER_t3
                 );
DimmerGlider dimmer2Glider = DimmerGlider(_DIMMER_2_tempMin, _DIMMER_2_tempMax, _DIMMER_2_increase);
Dimmer dimmer2 = Dimmer(
                   dimmer2Glider,
                   _DIMMER_2_pin,
                   _DIMMER_t3
                 );
DimmerGlider dimmer3Glider = DimmerGlider(_DIMMER_3_tempMin, _DIMMER_3_tempMax, _DIMMER_3_increase);
Dimmer dimmer3 = Dimmer(
                   dimmer3Glider,
                   _DIMMER_3_pin,
                   _DIMMER_t3
                 );

Dimmer dimmers[] = {
  dimmer1, dimmer2, dimmer3
};

DimmerImplementor implementor = DimmerImplementor(dimmers, _DIMMER_pinOpto);

void Dimmer_Init() {
  implementor.begin();
}

It throws the following error:
C:\Users\Arnym\Documents\Arduino\libraries\RegletaCMA/RegletaCMA.h: In member function 'void DimmerImplementor::begin()':

C:\Users\Arnym\Documents\Arduino\libraries\RegletaCMA/RegletaCMA.h:73:44: error: invalid use of non-static member function

         attachInterrupt(_optoPin, dimAll, 3); // 3 is the same as RISING

                                            ^

I have been messing around with this problem for some days and I can't find an answer, so I would be really pleased if you can help me.
If you need any more info, don't doubt on asking.
Thanks in advance.

user22924: In C++, you need to distinguish between two separate copies of an object, or having a reference to an existing object. @chrisl got it right when he identified that you were trying to create a second copy of DimmerGlider, so he suggested using a reference - and then showed using a pointer (*) instead.
He was right: Dimmer should hold a reference to the DimmerGlider object, but that is done with &. You need to make the following changes to the given code:
In the .h:
class Dimmer {

public:
    Dimmer(DimmerGlider &glider, int outputPin, int t3);
.
.
.
private:
    DimmerGlider &_glider;
.
.

And in the final .cpp:
DimmerGlider::DimmerGlider(int min, int max, int increase) :
              _min(min),
              _max(max),
              _increase(increase) {
}

Dimmer::Dimmer(DimmerGlider &glider, int outputPin, int t3) :
        _glider(glider),
        _outputPin(outputPin),
        _t3(t3) {
}

The syntax for the Dimmer constructor had to change. You were assigning the class members the values from the constructor parameters. References need to be initialised, not assigned. This is the syntax you need (note how they’re before the {?)
I fixed the DimmerGlider constructor code while I was at it...
Also, note the difference between using a reference versus using a pointer. A pointer needs to be dereferenced, so you need to use *ptr and ptr->_min to refer to the original object. A reference is already dereferenced, so you can simply use it like the original object: ref and ref._min.

Edit: If you want to use the function dimAll() as an interrupt handler function, then it cannot be a (non-static) member of a class. All class functions have a hidden this pointer as the first parameter in the argument list, since it has to know which DimmerImplementor's members to modify. Thus you have to declare the function as static. And as soon as you do that, you cannot call dimAllDimmers(), because that functions accesses an instance of DimmerImplementor's variables. Luckily, you have a global object called implementor, so you could use that:
static void DimmerImplementor::dimAll() {
    implementor.dimAllDimmers();
}

But it's usual to make that variable a static member of the class itself:
class DimmerImplementor {
    .
    .
public: // Static variables
    static DimmerImplementor implementor;
    .
    .
}; // DimmerImplementor

You'd initialise that as follows:
DimmerImplementor DimmerImplementor::implementor(dimmers, _DIMMER_pinOpto);


33320: <p>I am trying to make a library for Arduino, which requires an interruption, but the problem is that when I try the <code>attachInterrupt</code> function inside my library, there's a problem about attachInterrupt is static. Right now, in the library I have this in the header file:</p>

<pre><code>#ifndef RegletaCMA_h
#define RegletaCMA_h

#include "Arduino.h"

class DimmerGlider {
public:
    DimmerGlider(int min, int max, int increase);

    int getMin() { return _min; }

    int getMax() { return _max; }

    int getIncrease() { return _increase; }

    void setMin(int min) { _min = min; }

    void setMax(int max) { _max = max; }

    void setIncrease(int increase) { _increase = increase; }

private:
    int _min, _max, _increase;
};

class Dimmer {

public:
    Dimmer(DimmerGlider &amp;glider, int outputPin, int t3);

    void Dimmer::begin() {
        pinMode(_outputPin, OUTPUT);
        digitalWrite(_outputPin, false);
    }

    void Dimmer::dim() {
        delayMicroseconds(_DIMMER_counter);
        digitalWrite(_outputPin, true);  // Turn _DIMMER_pinDimmer on
        delayMicroseconds(_t3);
        digitalWrite(_outputPin, false); // Turn _DIMMER_pinDimmer off

        if (_DIMMER_dir) _DIMMER_counter += _glider.getIncrease(); // If dir is negative add _DIMMER_increase
        else _DIMMER_counter -= _glider.getIncrease();                  // If dir is negative substract _DIMMER_increase

        if (_DIMMER_counter &gt; _glider.getMax()) _DIMMER_dir = false; // Change dir if reached bottom
        if (_DIMMER_counter &lt; _glider.getMin()) _DIMMER_dir = true;  // Change dir if reached top

        _DIMMER_lastExecutionTime_micros = micros(); // Reset timer
    }

    void Dimmer::force(bool status) {
        digitalWrite(_outputPin, status);
    }

private:
    DimmerGlider &amp;_glider;

    int _outputPin;

    int _DIMMER_counter = 1000;
    bool _DIMMER_dir = true;

    unsigned long long _DIMMER_lastExecutionTime_micros = 0;

    int _t3;
};

class DimmerImplementor {
public:
    DimmerImplementor(Dimmer dimmerList[], int optoPin);

    void DimmerImplementor::begin(){
        attachInterrupt(_optoPin, dimAll, RISING);
    }

    void DimmerImplementor::dimAllDimmers() {
        for (int c = 0; c &lt; sizeof(_dimmerList); c++)
            _dimmerList[c].dim();
    }

    void DimmerImplementor::dimAll() {
        DimmerImplementor::dimAllDimmers();
    }

private:
    int _optoPin;
    Dimmer _dimmerList[];
};

#endif
</code></pre>

<p>and this in the cpp file:</p>

<pre><code>#include "Arduino.h"
#include "RegletaCMA.h"

DimmerGlider::DimmerGlider(int min, int max, int increase) :
        _min(min),
        _max(max),
        _increase(increase) {
}

Dimmer::Dimmer(DimmerGlider &amp;glider, int outputPin, int t3) :
        _glider(glider),
        _outputPin(outputPin),
        _t3(t3) {
}

DimmerImplementor::DimmerImplementor(Dimmer *dimmerList, int optoPin) :
        _dimmerList(dimmerList),
        _optoPin(optoPin) {
    for(int c = 0; c &lt; sizeof(dimmerList); c++){
        DimmerImplementor::dimmers[c] = dimmerList[c];
    }

    for (int c = 0; c &lt; sizeof(dimmers); c++) {
        Dimmer dimmer = dimmers[c];

        dimmer.begin();
    }
}
</code></pre>

<p>and when I try to run the library with the Arduino IDE, with this code:</p>

<pre><code>DimmerGlider dimmer1Glider = DimmerGlider(_DIMMER_1_tempMin, _DIMMER_1_tempMax, _DIMMER_1_increase);
Dimmer dimmer1 = Dimmer(
                   dimmer1Glider,
                   _DIMMER_1_pin,
                   _DIMMER_t3
                 );
DimmerGlider dimmer2Glider = DimmerGlider(_DIMMER_2_tempMin, _DIMMER_2_tempMax, _DIMMER_2_increase);
Dimmer dimmer2 = Dimmer(
                   dimmer2Glider,
                   _DIMMER_2_pin,
                   _DIMMER_t3
                 );
DimmerGlider dimmer3Glider = DimmerGlider(_DIMMER_3_tempMin, _DIMMER_3_tempMax, _DIMMER_3_increase);
Dimmer dimmer3 = Dimmer(
                   dimmer3Glider,
                   _DIMMER_3_pin,
                   _DIMMER_t3
                 );

Dimmer dimmers[] = {
  dimmer1, dimmer2, dimmer3
};

DimmerImplementor implementor = DimmerImplementor(dimmers, _DIMMER_pinOpto);

void Dimmer_Init() {
  implementor.begin();
}
</code></pre>

<p>It throws the following error:</p>

<pre><code>C:\Users\Arnym\Documents\Arduino\libraries\RegletaCMA/RegletaCMA.h: In member function 'void DimmerImplementor::begin()':

C:\Users\Arnym\Documents\Arduino\libraries\RegletaCMA/RegletaCMA.h:73:44: error: invalid use of non-static member function

         attachInterrupt(_optoPin, dimAll, 3); // 3 is the same as RISING

                                            ^
</code></pre>

<p>I have been messing around with this problem for some days and I can't find an answer, so I would be really pleased if you can help me.
If you need any more info, don't doubt on asking.
Thanks in advance.</p>
 22924: <p>In C++, you need to distinguish between two separate copies of an object, or having a reference to an existing object. @chrisl got it right when he identified that you were trying to create a second copy of <code>DimmerGlider</code>, so he suggested using a reference - and then showed using a pointer (<code>*</code>) instead.</p>

<p>He was right: <code>Dimmer</code> should hold a <em>reference</em> to the <code>DimmerGlider</code> object, but that is done with <code>&amp;</code>. You need to make the following changes to the given code:</p>

<p>In the <code>.h</code>:</p>

<pre><code>class Dimmer {

public:
    Dimmer(DimmerGlider &amp;glider, int outputPin, int t3);
.
.
.
private:
    DimmerGlider &amp;_glider;
.
.
</code></pre>

<p>And in the final <code>.cpp</code>:</p>

<pre><code>DimmerGlider::DimmerGlider(int min, int max, int increase) :
              _min(min),
              _max(max),
              _increase(increase) {
}

Dimmer::Dimmer(DimmerGlider &amp;glider, int outputPin, int t3) :
        _glider(glider),
        _outputPin(outputPin),
        _t3(t3) {
}
</code></pre>

<p>The syntax for the <code>Dimmer</code> constructor had to change. You were <em>assigning</em> the class members the values from the constructor parameters. References need to be <strong><em>initialised</em></strong>, not assigned. This is the syntax you need (note how they’re before the <code>{</code>?)</p>

<p>I fixed the <code>DimmerGlider</code> constructor code while I was at it...</p>

<p>Also, note the difference between using a reference versus using a pointer. A pointer needs to be dereferenced, so you need to use <code>*ptr</code> and <code>ptr-&gt;_min</code> to refer to the original object. A reference is <em>already</em> dereferenced, so you can simply use it like the original object: <code>ref</code> and <code>ref._min</code>.</p>

<hr>

<p>Edit: If you want to use the function <code>dimAll()</code> as an interrupt handler function, then it cannot be a (non-<code>static</code>) member of a <code>class</code>. All <code>class</code> functions have a hidden <code>this</code> pointer as the first parameter in the argument list, since it has to know <em>which</em> <code>DimmerImplementor</code>'s members to modify. Thus you have to declare the function as <code>static</code>. And as soon as you do that, you cannot call <code>dimAllDimmers()</code>, because that functions accesses an instance of <code>DimmerImplementor</code>'s variables. Luckily, you have a global object called <code>implementor</code>, so you could use that:</p>

<pre><code>static void DimmerImplementor::dimAll() {
    implementor.dimAllDimmers();
}
</code></pre>

<p>But it's usual to make that variable a <code>static</code> member of the class itself:</p>

<pre><code>class DimmerImplementor {
    .
    .
public: // Static variables
    static DimmerImplementor implementor;
    .
    .
}; // DimmerImplementor
</code></pre>

<p>You'd initialise that as follows:</p>

<pre><code>DimmerImplementor DimmerImplementor::implementor(dimmers, _DIMMER_pinOpto);
</code></pre>
