Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No matching function error when using attachInterrupt

I'm having a little error with my latest Arduino project code which uses the TimerOne library to display numbers on a 4 digit, 7 segment display. I use an interrupt to make the microprocessor constantly flick between each digit as they are essentially wired together.

I have the code working perfectly if I keep it all in the main PDE file, but I thought it would be better to isolate the display in its own class.

My compiler is having trouble with the second line of the following code in the PDE:

Timer1.initialize(500);
Timer1.attachInterrupt(digitDisplay.flashDigit,500); 

The second arg in attachInterrupt should be optional, I have tried with and without this! Anyhow I get the following error:

DigitDisplayTest.cpp: In function 'void setup()':
DigitDisplayTest:29: error: no matching function for call to     'TimerOne::attachInterrupt(<unresolved overloaded function type>)'
C:\Program Files (x86)\arduino-0022\arduino-0022\libraries\Timer1/TimerOne.h:62: note: candidates are: void TimerOne::attachInterrupt(void (*)(), long int)

Within DigitDisplay (of which digitDisplay is an instance), I define flashDigit as follows:

class DigitDisplay
{
  private:
    /*...*/
  public:
    /*...*/
    void flashDigit();
}

void DigitDisplay::flashDigit()
{ 
  wipeDisplay();
  for (int i = 0; i < _digitCount ; i++)
  {
    if ( i == _digit ) digitalWrite( _digitPins[i], HIGH );
    else digitalWrite( _digitPins[i], LOW );
  }
  displayNumber(_digits[_digit]);
  _digit++ ;
  _digit %= _digitCount; 
}

If you require more code please let me know but I am pretty sure there is nothing wrong with the gubbings of the flashDigit() method - it certainly worked before I put it inside its own class.

Obviously I can circumvent this error by adding

void Interrupt()
{
   digitDisplay.flashDigit();
}

to the main PDE and attaching that function, but that is just a work around, it would be nice if I could call it directly.

I see the error is to do with making a function pointer (of which one does not exist hence the error), but pointers are not my strong point so I could really do with a hand sorting this.

like image 558
SmallJoeMan Avatar asked Oct 09 '22 21:10

SmallJoeMan


1 Answers

You are very close. The problem is a member function (flashDigit()) is not the same thing as a function (void function()). A member function is ptr to a function that could change at runtime, unlike a function that is known at compile time. (hence the error msg about unresolved function type). There are two "work arounds". The first envelope function that you point out. And second, if the function doesn't need to take advantage of unique member values of an instance of the class, the member function could be declared static.

static void flashDigit();

This is described in more detail in section 33.1-33.3 of the Cline's C++ FAQ

like image 165
jdh Avatar answered Oct 13 '22 11:10

jdh