Advanced -> Is this possible??

Hi Forummembers,

I'm currently creating a library whose interface has to be as simple as possible.
it is a bit hard to explain but this is what i want to do:

The standard Arduino projects exist of 2 basic functions: Setup() and Loop().
I want to add a third function (lets call that one Interrupt()) which is called every time an interrupt is generated.

Untill here it is all really simple but here is my problem:
A function inside my library has to do some stuff first.
when that function is done the function Interrupt() (in my .ino) is called.
I also want to hide the process of configuring the interrupt.

So the only thing that can be seen inside my .ino is:
Setup()
{

}

Loop()
{

}

Interrupt()
{

}

Does anyone have a idea how i can realize this?

WhySoSerious:
The standard Arduino projects exist of 2 basic functions: Setup() and Loop().
I want to add a third function (lets call that one Interrupt()) which is called every time an interrupt is generated.

Every time which interrupt is run? It is certainly possible to put the ISR definition in your header and simply have it call the interrupt() function from the sketch. You would need an extern declaration in there to let it know that the interrupt() function is in another file.

Thanks for your respone Delta_G,

i want to run the function every time there is a rising edge on pin3 (on arduino uno) so int.1.
But wouldn't you still need to include the file in which the function or variable is declared when you use the extern declaration?

Like this? attachInterrupt() - Arduino Reference

@MrMark: I allready have that part in my code.
The Interrupt service routine is executed in my library.
The problem is that i have to call my function in my sketch from the interrupt service routine (which is in my library)

library.c sketch.ino

void interruptroutine() void setup() {
{ }

~~~~~ }
interruptfunction ----------------------------------------> void interruptfunction() {
}
}

Right, in the library you can do this:

extern void interrptfunction();

void interruptroutine() {
  // code
  interruptfunction();
  // more code
}

// This line goes in a begin or init function to be called from setup.  
attachInterrupt(1, interruptroutine, RISING);

And in your .ino or somewhere included from it there needs to be a function definition for interruptfunction. The extern just tells the compiler that the definition will exist somewhere.

It may not even need the extern. Just the forward declaration may be enough.

Look at how loop and setup are called from main.cpp.

Your library needs a begin() method, just like Serial.begin(), for example. Use attachInterrupt() in the begin().

@Delta_G: Your method did indeed work (i made a little fault in my first inplementation)
So i realized what i wanted.

Thanks all, and especially Delta of course :slight_smile: