0

So I want to do something like this:

template<int PIN, int CHANNEL>
void initPin() {
 attachInterrupt(PIN, rising<PIN, CHANNEL>, RISING);
}
template<int PIN, int CHANNEL>
void rising() {
 startTimes[CHANNEL] = micros();
 attachInterrupt(PIN, falling<PIN, CHANNEL>, FALLING);
}
template<int PIN, int CHANNEL>
void falling() {
 values[CHANNEL] = micros() - startTimes[CHANNEL];
 attachInterrupt(PIN, rising<PIN, CHANNEL>, RISING);
}

but the compiler gives me this error

no matches converting function ‘rising’ to type ‘void (*)()’

Can I even do this, or do I have to write all the functions by myself?

asked Aug 6, 2016 at 9:55

2 Answers 2

1

Try this:

template<int PIN, int CHANNEL> void initPin() {
 attachInterrupt(PIN, rising<PIN, CHANNEL>, RISING);
}
template<int PIN, int CHANNEL> void rising() {
 startTimes[CHANNEL] = micros();
 attachInterrupt(PIN, falling<PIN, CHANNEL>, FALLING);
}
template<int PIN, int CHANNEL> void falling() {
 values[CHANNEL] = micros() - startTimes[CHANNEL];
 attachInterrupt(PIN, rising<PIN, CHANNEL>, RISING);
}

The problem is actually the Arduino preprocessor. It adds illegal forward declarations if template functions are formated as your original code.

answered Aug 6, 2016 at 14:42
1

Please have a look implementation https://github.com/Yurik72/SimpleButton

There are implementation how to assign function with arguments to interrupt handler

answered Feb 27, 2020 at 11:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.