AttachInterrupt calling a function and passing input

I'm defining an attachinterrupt and I'm running into an error. The line in question is:

void setup() {
    attachInterrupt(digitalPinToInterrupt(stallsense1), changedirection(1), RISING);
    attachInterrupt(digitalPinToInterrupt(stallsense2), changedirection(2), RISING);
    }

and the error I'm getting is:

error: invalid use of void expression
    attachInterrupt(digitalPinToInterrupt(stallsense2), changedirection(2), RISING);
                                                                                    ^
    exit status 1
    invalid use of void expression

The idea is I have one function changedirection which takes either a 1 or 2 as input, depending on which interrupt was triggered. I believe the issue is that when I've written changedirection(1) I'm calling it. I want to simply tell attachinterrupt to call the function changedirection with an input of either 1 or 2. How would I do this?

Try using a lambda function:

    attachInterrupt(digitalPinToInterrupt(stallsense1), []{ changedirection(1); }, RISING);

This is exactly what I'm looking for, just couldn't find the right combination of words to get a google result. Thank you!

In ancient times, before lambda functions existed, we would have used regular old named functions:

void changedirection1() {changedirection(1);}
void changedirection2() {changedirection(2);}
void setup() {
    attachInterrupt(digitalPinToInterrupt(stallsense1), changedirection1, RISING);
    attachInterrupt(digitalPinToInterrupt(stallsense2), changedirection2, RISING);
    }
2 Likes

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.