user42: Currently, my sketch is checking an input pin every time round the main loop. If it detects a change, it calls a custom function to respond to it. Here's the code (trimmed down to the essentials):
int pinValue = LOW;

void pinChanged()
{
    //...
}

void setup()
{
    pinMode(2, INPUT);
}

void loop()
{
    // Read current input
    int newValue = digitalRead(2);

    // Has the input changed?
    if (newValue != pinValue) {
        pinValue = newValue;
        pinChanged();
    }
}

Unfortunately, this doesn't always work properly for very short changes on the input (e.g. brief pulses), especially if loop() is running a bit slowly.
Is there a way to make the Arduino detect the input change and call my function automatically?

user60: Any state of change on any pin configured as digital input can create an interrupt. Unlike the unique vectors for the interrupts causes by INT1 or INT2 the PinChangeInt feature uses a common vector and then Interrupt Service Routine (aka ISR) for this vector needs to then determine which pin changed. 
Fortunately PinChangeInt Library makes this easy.
PCintPort::attachInterrupt(PIN, burpcount,RISING); // attach a PinChange Interrupt to our pin on the rising edge
// (RISING, FALLING and CHANGE all work with this library)
// and execute the function burpcount when that pin changes


user42: You can do this using external interrupts. Most Arduinos only support this on a limited number of pins though. For full details, see the documentation on attachInterrupt().
Assuming you're using an Uno, you could do it like this:
void pinChanged()
{
    //...
}

void setup()
{
    pinMode(2, INPUT);
    attachInterrupt(0, pinChanged, CHANGE);
}

void loop()
{
}

This will call pinChanged() whenever a change is detected on external interrupt 0. On the Uno, that corresponds to GPIO pin 2. The external interrupt numbering is different on other boards, so it's important to check the relevant documentation.
There are limitations to this approach though. The custom pinChanged() function is being used as an Interrupt Service Routine (ISR). That means the rest of the code (everything in loop()) is temporarily stopped while the call is executing. In order to prevent disrupting any important timing, you should aim to make ISRs as fast as possible.
It's also important to note that no other interrupts will run during your ISR. That means anything relying on interrupts (such as the core delay() and millis() functions) may not work properly inside it.
Lastly, if your ISR needs to change any global variables in the sketch, they should usually be declared as volatile, e.g.:
volatile int someNumber;

That's important because it tells the compiler that the value could change unexpectedly, so it should be careful not to use any out-of-date copies/caches of it.

42: <p>Currently, my sketch is checking an input pin every time round the main loop. If it detects a change, it calls a custom function to respond to it. Here's the code (trimmed down to the essentials):</p>

<pre><code>int pinValue = LOW;

void pinChanged()
{
    //...
}

void setup()
{
    pinMode(2, INPUT);
}

void loop()
{
    // Read current input
    int newValue = digitalRead(2);

    // Has the input changed?
    if (newValue != pinValue) {
        pinValue = newValue;
        pinChanged();
    }
}
</code></pre>

<p>Unfortunately, this doesn't always work properly for very short changes on the input (e.g. brief pulses), especially if <code>loop()</code> is running a bit slowly.</p>

<p>Is there a way to make the Arduino detect the input change and call my function automatically?</p>
 60: <p>Any state of change on any pin configured as digital input can create an interrupt. Unlike the unique vectors for the interrupts causes by INT1 or INT2 the PinChangeInt feature uses a common vector and then Interrupt Service Routine (aka ISR) for this vector needs to then determine which pin changed. </p>

<p>Fortunately <a href="http://playground.arduino.cc/Main/PinChangeInt">PinChangeInt Library</a> makes this easy.</p>

<pre><code>PCintPort::attachInterrupt(PIN, burpcount,RISING); // attach a PinChange Interrupt to our pin on the rising edge
// (RISING, FALLING and CHANGE all work with this library)
// and execute the function burpcount when that pin changes
</code></pre>
 42: <p>You can do this using external interrupts. Most Arduinos only support this on a limited number of pins though. For full details, see the documentation on <a href="http://arduino.cc/en/Reference/AttachInterrupt"><code>attachInterrupt()</code></a>.</p>

<p>Assuming you're using an Uno, you could do it like this:</p>

<pre><code>void pinChanged()
{
    //...
}

void setup()
{
    pinMode(2, INPUT);
    attachInterrupt(0, pinChanged, CHANGE);
}

void loop()
{
}
</code></pre>

<p>This will call <code>pinChanged()</code> whenever a change is detected on external interrupt 0. On the Uno, that corresponds to GPIO pin 2. The external interrupt numbering is different on other boards, so it's important to check the relevant documentation.</p>

<p>There are limitations to this approach though. The custom <code>pinChanged()</code> function is being used as an Interrupt Service Routine (ISR). That means the rest of the code (everything in <code>loop()</code>) is temporarily stopped while the call is executing. In order to prevent disrupting any important timing, you should aim to make ISRs as fast as possible.</p>

<p>It's also important to note that no other interrupts will run during your ISR. That means anything relying on interrupts (such as the core <code>delay()</code> and <code>millis()</code> functions) may not work properly inside it.</p>

<p>Lastly, if your ISR needs to change any global variables in the sketch, they should usually be declared as <code>volatile</code>, e.g.:</p>

<pre><code>volatile int someNumber;
</code></pre>

<p>That's important because it tells the compiler that the value could change unexpectedly, so it should be careful not to use any out-of-date copies/caches of it.</p>
