Go Down

Topic: Need a S-I-M-P-L-E example of using PinChangeInt or ooPinChangeInt (Read 1 time) previous topic - next topic


Jul 05, 2013, 10:14 pm Last Edit: Jul 05, 2013, 10:16 pm by warren631 Reason: 1
I need two interrupts on a Nano (interrupts on pins 2 and 3) to measure the duration of pulses from two wheels to get speed feedbacks for PID loops.  I'm already using pin 3 for PWM to a motor drive and I am changing the PWM frequency on timers 1 and 2 (pins 3,9,10,11).  So I need two software interrupts.  I have been trying to understand PinChangeInt and ooPinChangeInt (what's the difference?) and I find it tough going.  I don't know a class from a hole in the ground, let alone sub classes.  Do I have to use different ports (D,B,C) for the two interrupt pins or can I use pins 4 and 5?

I have tried to understand the examples downloaded with the libraries and the Wiki but I am dumbfounded.  Anyone know of a simple example with two software interrupts?  I learn by copying and trying.  Thanks for any help!


[ I won't respond to messages, use the forum please ]


Jul 06, 2013, 12:00 am Last Edit: Jul 06, 2013, 12:10 am by warren631 Reason: 1
That is simple, but how would I add another interrupt pin?  And what is pin 15? I don't have a input 15. Can I use pins 4 and 5? Is this OK:
Code: [Select]
Copyright 2011 Lex.V.Talionis at gmail
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   #include <PinChangeInt.h>
   #include <PinChangeIntConfig.h>
   #define PIN 4  // the pin we are interested in
   #define PIN2 5  // the pin we are interested in
   volatile byte burp=0;    // a counter to see how many times the pin has changed
   volatile byte burp2=0;    // a counter to see how many times the pin has changed
   byte cmd=0;     // a place to put our serial data
   void setup() {
     Serial.print("PinChangeInt test on pin ");
     Serial.print("PinChangeInt test on pin ");
     pinMode(PIN, INPUT);     //set the pin to input
     digitalWrite(PIN, HIGH); //use the internal pullup resistor
     pinMode(PIN2, INPUT);     //set the pin to input
     digitalWrite(PIN2, HIGH); //use the internal pullup resistor
     PCintPort::attachInterrupt(PIN, burpcount,RISING); // attach a PinChange Interrupt to our pin on the rising edge
     PCintPort::attachInterrupt(PIN2, burpcount2,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
   void loop() {
     if (cmd=='p')
       Serial.println(burp, DEC);
       Serial.println(burp2, DEC);
   void burpcount()
   void burpcount2()


pin 15 on the Uno is the same as A1.

The example shows attachInterrupt being called for two pins, check with the library sources but I suspect
you just call it for any and all pins you want, and the library handles dispatching to your handlers - that would
be the obvious way to do it.
[ I won't respond to messages, use the forum please ]


The original example was for only one pin - 15.  I modified it above for two pins - 4 and 5.  Is that OK?  Or do the pins have to be on different vectors (whatever they are) or ports?


What Arduino are you using, not all of them support pin change int on every pin.

See here for an example using multiple pin change ints and the last link for a guide to which pins can be used on which Arduinos -

Background - because of the way the class is written, you dont need to know that its a class, just use a slightly different way of calling the functions -

Example usage

Usable pins

Duane B

Read this
then watch this



Aug 02, 2013, 07:23 am Last Edit: Aug 02, 2013, 10:21 am by Measureino Reason: 1
Like others I#ve some problems understanding and using PinChangeInt. On my Mega2560 I need three interrupt pins. In the past I tested with two external Pins and had success.But now externals can't be used, because of usage by shields.

I want to use PintChangedInt with the analogue pins A1,A2 and A3 for interrupts with light barriers, which change when reflecting from HIGH to LOW. Is the follwoing code snippet correct for this?
Code: [Select]

#define Pin1 15
#define Pin2 16
#define Pin3 17
pinMode(Pin1, INPUT);     //set the pin to input
digitalWrite(Pin1, HIGH); //use the internal pullup resistor
pinMode(Pin2, INPUT);    
digitalWrite(Pin2, HIGH);
pinMode(Pin3, INPUT);    
digitalWrite(Pin3, HIGH);
PCintPort::attachInterrupt(Pin1, ISR_for_Pin1,FALLING); // attach
PCintPort::attachInterrupt(Pin2, ISR_for_Pin2,FALLING); // attach
PCintPort::attachInterrupt(Pin3, ISR_for_Pin3,FALLING); // attach

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131