As you may have guessed, this is an Arduino library to handle Pin Change Interrupts.
#define PCINT_PIN A15
#include <YetAnotherPcInt.h>
void pinChanged(const char* message, bool pinstate) {
Serial.print(message);
Serial.println(pinstate ? "HIGH" : "LOW");
}
void setup() {
Serial.begin(115200);
pinMode(PCINT_PIN, INPUT_PULLUP);
PcInt::attachInterrupt(PCINT_PIN, pinChanged, "Pin has changed to ", CHANGE);
}
void loop() {}
Easy to install and use
Callbacks provide useful arguments:
This is specially useful when developing components that can be instantiated many times (See example)
Providing it as an argument prevents concurrency issues.
It should support RISING
/FALLING
/CHANGE
modes
Code is very efficient (It's an ISR, after all)
Code is very small (~250 lines) and readable
Attaches a callback function to Pin Change Interruptions on the specified pin.
void PcInt::attachInterrupt(
uint8_t pin,
callback func,
T *userdata,
uint8_t mode=CHANGE,
bool trigger_now=false);
Arguments:
pin
: The pin number you are listening.
callback
: The funcion called whenever the interrupt was triggered.
Your funcion must look like one of these:
void myfunction() { ... }
void myfunction(bool newstate) { ... }
void myfunction(T* userdata) { ... }
void myfunction(T* userdata, bool newstate) { ... }
userdata
: User-provided argument for callback
. Skip this If your callback doesn't have a userdata
argument.
User-provided arguments are useful when reusing the same callback funcion to handle changes on multiple pins.
mode
: The transition Which triggers the callbacks: CHANGE
, RISING
or FALLING
.
trigger_now
: If set, the callback is called immediately. This is useful for initialization.
void PcInt::detachInterrupt(
uint8_t pin);
Removes the callback function from Pin Change Interruptions on the specified pin.
Arguments:
pin
: The pin number you are no longer listening to.AVR microcontrollers only have a few external Interruption pins. But I want to monitor more pins... Looots more!
The alternative is using pin change interrupts, which is supported on lots of pins simultaneously.
The interrupt can be enabled for each supported pin individually, but there are only a few interrupt vectors, so up to 8 pins share the same service routine.
It's up to the software to figure out which pins changed state and act accordingly. This library does it for you :)
As the name suggests, there are many other libraries out there for handling Pin Change Interruptions.
In particular, this project started as a fork of Sodaq_PcInt and uses PinChangeInterruptBoards.h from NicoHood's PinChangeInterrupt Library
I've looked at many of them before I decided to create this library, and this is how they compare.
PcInt example code from arduino.cc
RISING
/FALLING
)RISING
/FALLING
/CHANGE
modesSodaq_PcInt by SODAQ
RISING
/FALLING
)RISING
/FALLING
/CHANGE
modes (Unless you are using GabrielNotman's fork)PinChangeInterrupt Library by NicoHood
RISING
/FALLING
) via getPinChangeInterruptTrigger()
RISING
/FALLING
/CHANGE
modesEnableInterrupt by Mike "GreyGnome" Schwager
EI_ARDUINO_INTERRUPTED_PIN
macro)RISING
/FALLING
)RISING
/FALLING
/CHANGE
modesRISING
/FALLING
) via getPinChangeInterruptTrigger()
RISING
/FALLING
/CHANGE
modes