Example 5

Back to Arduino Interrupts

#include <PinChangeInt.h>

const byte LED = 13, SW = 5;

volatile byte count = 0;
int lastCount = -1;

void handleSW() {
    digitalWrite(LED, digitalRead(SW));
    count++;
}

void handleOtherStuff() {
    if (count != lastCount) {
        Serial.print("Count ");
        Serial.println(count);
        Serial.println(PCintPort::pinState);
        lastCount = count;
    }
}

void setup() {
    //Start up the serial port
    Serial.begin(9600);
    while (!Serial); // for Leonardo
    Serial.println(F("Initialized example5"));

    // configure the pins
    pinMode(LED, OUTPUT);
    pinMode(SW, INPUT_PULLUP);

    // Attach the ISR
    PCintPort::attachInterrupt(SW, handleSW, CHANGE);
}

void loop() {
    handleOtherStuff();
} 

For this example, we replace the external interrupt 0 used in experiments 3 and 4 with a pin change interrupt.  Pin change interrupts are available on all pins of the ATmega328P chip used on an Arduino Uno.  I changed from pin 2 to pin 5 in this example, but pin change works on pin 2 as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

Triangle Embedded Interest Group