# How to attachInterrupt inside constructor?

**URL:** <https://forum.arduino.cc/t/how-to-attachinterrupt-inside-constructor/574866>\
**Category:** Programming\
**Created:** [February 18, 2019, 6:44pm UTC](https://forum.arduino.cc/t/how-to-attachinterrupt-inside-constructor/574866 "2019-02-18T18:44:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 18, 2019, 6:44pm UTC](https://forum.arduino.cc/t/how-to-attachinterrupt-inside-constructor/574866/1 "2019-02-18T18:44:50Z")

</div>

I am making a class for a color sensor, and wish to call attachInterrupt() right as it is created. What is the correct syntax?

```cpp
class ColorSensor {
 private:
  void UpdateISR();
 public:
  ColorSensor(int outPin) {
  attachInterrupt(digitalPinToInterrupt(outPin), this->*UpdateIRS, CHANGE);
  }
};

```

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [February 18, 2019, 6:57pm UTC](https://forum.arduino.cc/t/how-to-attachinterrupt-inside-constructor/574866/2 "2019-02-18T18:57:17Z")

</div>

You can't use attachInterrupt() with an instance function. You can use it with a static (class-level) function.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 18, 2019, 7:08pm UTC](https://forum.arduino.cc/t/how-to-attachinterrupt-inside-constructor/574866/3 "2019-02-18T19:08:59Z")

</div>

Is there a way to attach an interupt in the same time while creating a class?

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [February 18, 2019, 7:31pm UTC](https://forum.arduino.cc/t/how-to-attachinterrupt-inside-constructor/574866/4 "2019-02-18T19:31:13Z")

</div>

While a class is created? No. You can attach an interrupt to a static member function in the constructor when an instance is created. But, you should set a (static member variable) flag so you only do the attachInterrupt when the first instance is created.

Or, you can call a static member function that does the attachInterrupt to a different static member function.

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [February 19, 2019, 12:23am UTC](https://forum.arduino.cc/t/how-to-attachinterrupt-inside-constructor/574866/5 "2019-02-19T00:23:33Z")

</div>

If you create a global instance of the object, the constructor gets called before the Arduino core is initialized. That is why many object libraries have a '.begin()' member function. You could do that.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 11:38pm UTC](https://forum.arduino.cc/t/how-to-attachinterrupt-inside-constructor/574866/6 "2021-05-05T23:38:48Z")

</div>

Closed on May 5, 2021, 11:38pm UTC


