ISRHandler is a flexible and reusable C++ library for handling interrupts in Arduino-based projects. It simplifies managing interrupts by encapsulating them in a class structure and using templates to handle multiple interrupt pins efficiently.
This library is designed for embedded systems where precise and efficient interrupt handling is essential.
libraries
folder.#include <ISRHandler.h>
.The ISRHandler class allows handling multiple interrupt pins using a single intr(uint8_t interruptNum)
function. You can attach multiple interrupts by specifying their pin numbers and modes (e.g., RISING
, FALLING
) through template parameters.
#include "ISRHandler.h"
// intr() triggered on INT=2 RISING and INT=3 FALLING
class MyInterruptHandler : public ISRHandler<2, RISING, 3, FALLING> {
protected:
void intr(uint8_t interruptNum) override {
// Handle interrupts here, INT2 and INT3 both trigger this handler
Serial.print("Interrupt ");
Serial.print(interruptNum);
Serial.println(" triggered!");
}
};
MyInterruptHandler handler;
void setup() {
Serial.begin(115200);
while (!Serial); // Wait for Serial to initialize
Serial.println("ISRHandler test");
// Set the pins as input with pull-up resistors
pinMode(2, INPUT_PULLUP); // Set pin 2 as input with pull-up
pinMode(3, INPUT_PULLUP); // Set pin 3 as input with pull-up
handler.begin(); // Start handling interrupts on pins 2 and 3
}
void loop() {
// Main code logic
}
MyInterruptHandler
inherits from ISRHandler
, specifying two interrupts (pin 2 and pin 3) with their respective modes (RISING
for pin 2 and FALLING
for pin 3).The ISRHandler class uses template parameters to configure interrupt modes and pin numbers:
ISRHandler<pin1, mode1, pin2, mode2, ...>
RISING
, FALLING
, CHANGE
, etc.).begin()
: Initializes the interrupt handler by attaching the ISR to the specified interrupt pins.end()
: Stops handling interrupts by detaching the ISR and resetting the handler pointer to nullptr
.When using multiple interrupts, the ISRHandler provides the interrupt number to the intr()
function. You can handle different interrupt sources within a single intr()
function by checking the interrupt number.
void intr(uint8_t interruptNum) override {
if (interruptNum == 2) {
// Handle interrupt from pin 2
} else if (interruptNum == 3) {
// Handle interrupt from pin 3
}
}
begin()
is called after setting up the pins, and that interrupts are enabled on the pins you want to use.INPUT_PULLUP
if needed).INPUT
or INPUT_PULLUP
before calling begin()
.Q: Can I handle multiple interrupts with a single handler?
Yes, ISRHandler allows you to manage multiple interrupts with a single intr()
function by checking the interrupt number.
Q: How do I debounce an interrupt signal?
For mechanical buttons or switches, you can debounce the signal in software by ignoring rapid successive triggers, or use a hardware debouncing circuit.
Q: Can I use ISRHandler with multiple classes?
Yes, you can instantiate different handler objects in multiple classes, each managing its own set of interrupts.
For a detailed explanation of the static pointer method used in the ISRHandler library, read the article: Interrupts in C++ Using a Static Pointer Method.
The ISRHandler library simplifies interrupt handling across multiple pins in Arduino projects. By encapsulating interrupt logic in a class and using templates to manage multiple interrupt pins, it provides flexibility and ease of use for developers working with embedded systems.
The library is ideal for applications requiring multiple interrupt sources, and the template system allows efficient and clean code for managing these interrupts.
For more examples and detailed documentation, see the examples
folder in the library repository.
If you have any questions or would like to reach out for any reason, feel free to Contact Me through my online form.
Thank you for visiting this repository, and I hope you find the tools and information useful!