attachInterrupt() and object like code

I tried to make my arduino code for my Spider controller ( http://wiki.jmoon.co/boards/spider-controller/ ) a bit more object like but the attachInterrupt() throws errors like

cannot convert 'Motor::encoder_intproc' from type 'void (Motor::)()' to type 'void (*)()'

when using the interrupt as member function.

Is there some version of attachInterrupt() which works with interrupt functions as member function?

I read

but I did not find a solution there.

Thx in advance

Post your code.

MEgg:
a bit more object like

Does that offer any value in the small memory of an Arduino?

At the moment it just seems to be making trouble for you.

...R

Like here?

/dev:
Like here?

Yes that is my project and the simple code from the beginning.
:slight_smile:
Encoder problem was accepted as HW bug from the good robosavvy support after some more
measurements.

Robin2:
Post your code.

That was the code that produced the error message:
class Motor
{
...
public:
unsigned long encodercount;

void encoder_intproc()
{
encodercount++;
}

Motor(int DIRpin, int PWMpin, int ENCINTpin, int CURpin)
{
// init the values, do pinMode()

attachInterrupt(digitalPinToInterrupt(ENCINTpin),
encoder_intproc, CHANGE);
}
}

Does that offer any value in the small memory of an Arduino?

At the moment it just seems to be making trouble for you.

...R

It helped to clean the code a bit.
Instead of doing 4 times nearly the same for 4 motors.
The code as such works and does what it is supposed to.

When the interrupt happens, which instance of the class should deal with it?

You might say that there will only be once instance, but you can't guarantee that. There might be zero instances, one instance, or 214 instances.

You can have a static method that belongs to the class that you can use as the interrupt handler. But, that simply moves the "which instance needs to deal with the interrupt" question to your class.

PaulS:
When the interrupt happens, which instance of the class should deal with it?

You might say that there will only be once instance, but you can't guarantee that. There might be zero instances, one instance, or 214 instances.

You can have a static method that belongs to the class that you can use as the interrupt handler. But, that simply moves the "which instance needs to deal with the interrupt" question to your class.

When doing
Motor M_VL(8, 7, 3, 56);
Motor M_VR(10, 9, 2, 57);

I had the (probably wrong) impression, that M_VL.encoder_intproc(), M_VR.encoder_intproc() and so on should
handle the interrupt.

I had the (probably wrong) impression, that M_VL.encoder_intproc(), M_VR.encoder_intproc() and so on should
handle the interrupt.

Suppose you have a bunch of teenagers at home, and the phone rings (an interrupt). Do you really expect each of them to answer the phone (process the interrupt)? In any kind of logical/orderly fashion? Dream on!

PaulS:
Suppose you have a bunch of teenagers at home, and the phone rings (an interrupt). Do you really expect each of them to answer the phone (process the interrupt)? In any kind of logical/orderly fashion? Dream on!

:slight_smile:
If they all have the same ring tone, then...
No seriously.
The explanation here:
http://collaboration.cmc.ec.gc.ca/science/rpn/biblio/ddj/Website/articles/CUJ/2002/0202/gatliff/gatliff.htm
is good and that was the point that I was missing.