paulo-raca / YetAnotherArduinoPcIntLibrary

An Arduino library to handle Pin Change Interrupts
21 stars 5 forks source link

Yet Another Arduino PcInt Library

As you may have guessed, this is an Arduino library to handle Pin Change Interrupts.

Examples

#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() {}

Features

API

attachInterrupt

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:

detachInterrupt

void PcInt::detachInterrupt(
    uint8_t pin);

Removes the callback function from Pin Change Interruptions on the specified pin.

Arguments:

About Pin Change Interruptions

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 :)

Comparisson with other libraries

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.