blown406 / arduino-pinchangeint

Automatically exported from code.google.com/p/arduino-pinchangeint
0 stars 0 forks source link

Issue with using pinchangeint #13

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
If there was a discussion forum, I would post this there, as it would be a more 
appropriate setting.

Anyways, pinchangeint has been very finnicky for me.  Sometimes it works, 
sometimes it doesn't.  It's not a compilation issue, but rather a runtime 
issue.  Sometimes it simply crashes my sketch.

This sketch compiles, and as far as I know SHOULD work, but doesn't:

#include <PinChangeInt.h>

int encoderPinA = 8;
int encoderPinB = 7;
bool A_set = false;
bool B_set = false;
volatile long encoderPos = 0;
long lastReportedPos = 1;

// Interrupt on A changing state
void doEncoderA(){
    // Test transition
    A_set = digitalRead(encoderPinA) == HIGH;
    // and adjust counter + if A leads B
    encoderPos += (A_set != B_set) ? +1 : -1;
}

// Interrupt on B changing state
void doEncoderB(){
    // Test transition
    B_set = digitalRead(encoderPinB) == HIGH;
    // and adjust counter + if B follows A
    encoderPos += (A_set == B_set) ? +1 : -1;
}

void setup()
{
    pinMode(encoderPinA, INPUT);
    pinMode(encoderPinB, INPUT);

    Serial.begin(9600);

    PCintPort::attachInterrupt(encoderPinA, &doEncoderA, CHANGE);
    PCintPort::attachInterrupt(encoderPinB, &doEncoderB, CHANGE);

    Serial.println("Start!");
}

void loop()
{
    if (encoderPos != lastReportedPos)
    {
        lastReportedPos = encoderPos;
        Serial.println(encoderPos);
    }
}

Any suggestions would be welcome.  

(The symptom: the Serial.println("Start!") never happens...because I never see 
it printed over the serial line)

Original issue reported on code.google.com by davidpru...@gmail.com on 26 Oct 2012 at 10:07

GoogleCodeExporter commented 8 years ago
By the way, this code works on an Arduino Uno.  It doesn't work on an Arduino 
Mega.

Original comment by davidpru...@gmail.com on 30 Oct 2012 at 10:19

GoogleCodeExporter commented 8 years ago
There is a link from the front page to the "Groups", which is the forum. I wish 
the forum were more prominent, *sigh*.

Regarding your issue, there are a couple of problems with your code.  You have 
correctly set encoderPos to be volatile, but not A_set and B_set. Those are 
used in both of your interrupts. The general rule is, if a variable is shared 
with any interrupt, you must make it volatile.  Sharing a variable between two 
interrupts means they should be volatile.

Also, you do not perform the digitalWrite(PIN, HIGH) to turn on the resistors 
on your input pins. I can imagine that the high input impedance is creating all 
sorts of noise on the pins and preventing the code from reaching your "Start!" 
print statement.

You do not need to use the digitalRead() commands in your interrupt handlers.  
See http://code.google.com/p/arduino-pinchangeint/wiki/Usage .  The 
PCintPort::pinState variable will tell you if it's HIGH or LOW. That will save 
a few micros, anyway. Besides, the pinState may have changed (because of switch 
bounce) by the time you read it in the Interrupt handler. Switch bounce is a 
very annoying thing.

Finally, if you are working on rotary encoders, you may want to refer to 
http://code.google.com/p/adaencoder/ . There I have a rotary encoder class. 
Maybe I have already done what you are trying to do.

Original comment by m...@schwager.com on 1 Nov 2012 at 4:50

GoogleCodeExporter commented 8 years ago

Original comment by m...@schwager.com on 1 Nov 2012 at 4:50

GoogleCodeExporter commented 8 years ago
P.S. The Mega uses entirely different pins, and mostly different ports, than 
its smaller cousins. Refer to the other posting in this issue section, and the 
Wiki, for more information.  Good luck.

Also, sorry it has taken me a number of days to get back to you. I've been 
hacking on an MP3 chip outside of work and haven't checked my email lately.

Original comment by m...@schwager.com on 1 Nov 2012 at 4:52

GoogleCodeExporter commented 8 years ago
No updates in a month; I am going to close this issue. Note there are a number 
of changes to the PinChangeInt code lately; try to take a look at that.

Original comment by m...@schwager.com on 29 Nov 2012 at 2:45