Pages: [1]   Go Down
Author Topic: Need a S-I-M-P-L-E example of using PinChangeInt or ooPinChangeInt  (Read 846 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 1
Posts: 105
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
« Last Edit: July 05, 2013, 03:16:45 pm by warren631 » Logged

0
Offline Offline
Shannon Member
****
Karma: 199
Posts: 11649
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So what confuses you about the example here: http://playground.arduino.cc//Main/PinChangeIntExample
Logged

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

Offline Offline
Full Member
***
Karma: 1
Posts: 105
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
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.begin(9600);
      Serial.print("PinChangeInt test on pin ");
      Serial.println(PIN);
      Serial.print("PinChangeInt test on pin ");
      Serial.println(PIN2);
      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() {
      cmd=Serial.read();  
      if (cmd=='p')
      {
        Serial.print("burpcount:\t");
        Serial.println(burp, DEC);
        Serial.print("burpcount2:\t");
        Serial.println(burp2, DEC);
      }
      cmd=0;
    }
    
    void burpcount()
    {
      burp++;
    }
    void burpcount2()
    {
      burp2++;
    }


« Last Edit: July 05, 2013, 05:10:14 pm by warren631 » Logged

0
Offline Offline
Shannon Member
****
Karma: 199
Posts: 11649
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

Offline Offline
Full Member
***
Karma: 1
Posts: 105
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 -
http://rcarduino.blogspot.ae/2012/03/need-more-interrupts-to-read-more.html

Example usage
http://rcarduino.blogspot.ae/2012/04/how-to-read-multiple-rc-channels-draft.html

Usable pins
http://rcarduino.blogspot.ae/2013/04/the-problem-and-solutions-with-arduino.html

Duane B

rcarduino.blogspot.com
Logged


Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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
« Last Edit: August 02, 2013, 03:21:42 am by Measureino » Logged

Pages: [1]   Go Up
Jump to: