Attach Interrupt not being triggered on state change

Hello,

I am working on an ARM Cortex-M0+ 48-QFN mc that I have attached a single line from an encoder output to in order to measure the number of rotations the motor has made.

I am very confused as I appear to be able to measure changes using the digitalRead() function on the line I have connected, yet the interrupt function does not appear to be getting triggered at any point. Turning the motor increases the output of hpEncoderPosition but not hpEncoderInt.

Here is the code for reference:

volatile unsigned int   hpEncoderPosition = 100;
volatile unsigned int   hpEncoderInt = 100;
volatile unsigned short   encoderState = 0;
volatile unsigned short   encoderState_old = 0;

void interrupt_Test(){
  //hpEncoderPosition++;
  hpEncoderInt++; 
}

void setup() {
  // put your setup code here, to run once:
  //Serial.println(hpEncoderPosition);
  delay(300);
  pinMode(9, INPUT);
  attachInterrupt(digitalPinToInterrupt(9), interrupt_Test, CHANGE);
}

void loop() {
  // put your main code here, to run repeatedly:
  encoderState_old = encoderState;
  encoderState = digitalRead(9);
  if (encoderState != encoderState_old) {
      hpEncoderPosition++;
  }
  Serial.print("No Int:");
  Serial.println(hpEncoderPosition);
  Serial.print("Int:");
  Serial.println(hpEncoderInt);
  delay(10);
}

I would appreciate any suggestions to help figure out why the interrupt is not triggering on activity in the connected line.

what board are you selecting for your project when you compile?

I would try this line as the last line of your if statement. As is, you're updating the old state no matter what. I don't think that's what you want?

Thanks for the quick response, I am using Teensyduino board manager as the Teensy LC board.

More: shouldn't it be?

void loop() {
  // put your main code here, to run repeatedly:

  encoderState = digitalRead(9);
  if (encoderState != encoderState_old) {
    hpEncoderPosition++;
    Serial.print("No Int:");
    Serial.println(hpEncoderPosition);
    Serial.print("Int:");
    Serial.println(hpEncoderInt);
    delay(10);
    encoderState_old = encoderState;
  }
}

The exact behavior of the if statement is not important. My issue is that I am able to read changes in the line via the digitalRead() function but attaching an interrupt to the same pin does not trigger the interrupt even when the digital reading is changing.

Please post a link to the encoder data sheet, and a schematic diagram showing all the encoder connections.

This is what I would try. I moved a couple other lines as I'm trying to understand your workflow.

void loop() {
  // put your main code here, to run repeatedly:
  encoderState = digitalRead(9);
  if (encoderState != encoderState_old) {
    hpEncoderPosition++;
    Serial.print("No Int:");
    Serial.println(hpEncoderPosition);
    delay(10);
    encoderState_old = encoderState;
  }
  int myEncoderInt = hpEncoderInt;
  Serial.print("Int: ");
  Serial.print(hpEncoderInt);
  Serial.print(" myInt: ");
  Serial.println(myEncoderInt);
}

With no Serial.begin(), I wonder how your code manages to print anything.

void setup() {
  // put your setup code here, to run once:
  //Serial.println(hpEncoderPosition);
  delay(300);
  pinMode(9, INPUT);
  attachInterrupt(digitalPinToInterrupt(9), interrupt_Test, CHANGE);
}

Is this some "special feature" of a Teensy?

On an ARM with native USB, there's no UART to be configured for Serial. The only thing Serial.begin() does is check to see if the USB is up yet. And, perhaps, a little delay.

Thanks, I was unaware of that.

I will try to find the encoder data sheet when I have the time, I don't remember the exact part name off the top of my head. For the schematic, the parts I have described are part of a much more complex system, at the moment I am merely trying to understand why attaching the interrupt to the signal from the encoder is not triggering the interrupt.

I have verified that the signal from the encoder is working via an oscilloscope, and I am able to increment a counter (hpEncoderPosition) by reading the change in states from the encoder signal using digitalRead(). When I turn the motor, hpEncoderPosition increments properly. What I am confused about, is that the interrupt I have attached to the signal on the same port does not fire when the signal changes.

Is it possible that I have to address the pin directly? Could it be possible that Arduino IDE can't interface with the mc properly?

You haven't posted enough information for forum members to say what the problem might be.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.