Pin change interrupt code help

Hi dispit so many tutorial I have read on this topic, I am still very foggy about how to set up a pin change interrupt in code.

I have a code like this one, which turn on one LED and shut off the other once the switch is pressed.

const byte interruptLED = 11;
const byte otherLED = 12;
const byte BUTTON = 2;

// Interrupt Service Routine (ISR)
void switchPressed ()
{
  if (digitalRead (BUTTON) == HIGH) {
    digitalWrite (interruptLED, LOW);
    digitalWrite (otherLED, HIGH);

  }
  else {
    digitalWrite (interruptLED, HIGH);
    digitalWrite (otherLED, LOW);

  }
}  // end of switchPressed

void setup ()
{
  pinMode (interruptLED, OUTPUT);  // so we can update the interruptLED
  pinMode (loopLED, OUTPUT);
  digitalWrite (BUTTON, HIGH);  // internal pull-up resistor
  attachInterrupt (0, switchPressed, CHANGE);  // attach interrupt handler
}  // end of setup

void loop ()
{
  // do the loop thing
}

This code uses the D2(interrupt 0);
How do I set up the Pin Change Interrupt to use pIND D13 instead of pin D2 as the interrupt?

Any help is greatly appreciated.

Why do you think you need an interrupt to read the state of a switch? I'm almost certain that you don't.

This looks promising:

http://www.geertlangereis.nl/Electronics/Pin_Change_Interrupts/PinChange_en.html

How do I set up the Pin Change Interrupt to use pIND D13 instead of pin D2?

The code you posted uses a hardware interrupt not a pin change interrupt

Have you looked at the examples that come with the PinChangeInt library ?

PaulS:
Why do you think you need an interrupt to read the state of a switch? I'm almost certain that you don't.

In the book I'm currently using as my main reference, it says that being able to use interrupts and counters is what separates good microcontroller programmers from great. Reading a button may seem simple enough, as long as your program does nothing more, but most programs do.

:slight_smile:

In the book I'm currently using as my main reference, it says that being able to use interrupts and counters is what separates good microcontroller programmers from great.

No. Being able to use interrupts separates adequate programmers from good programmers. Great programmers recognize WHEN an interrupt is appropriate and use them ONLY then.

Reading the state of a switch is something the Arduino is capable of doing 1000's of times per second as long as there is no blocking code.

Reading a button may seem simple enough, as long as your program does nothing more, but most programs do.

and the vast majority of properly written programs do not need to use an interrupt to read the state of an input even if the program does many other things.

Thank you for all the replies.

My trouble is I am trying to display data of the MPU 6050 to a SPFD5408 Screen. However, the SPFD5408 needs pin D2 and D3 to work properly on the Nano, and the MPU6050 needs pin D2 for the interrupt, The two modules don't co-exist. Someone on this forum suggested me to look into pin change interrupt as the solution. That's why I want to learn about pin change interrupt.

Like I said, I am still very foggy about pin change interrupt. My understand of the concept of pin change is that in the code I can manipulate any capable pins, such as D10 on the Nano to be an interrupt hence the Nano can have one more interrupt pin in addition of pin D2 and D3. Am I totally wrong about that?

Am I totally wrong about that?

No.

My understand of the concept of pin change is that in the code I can manipulate any capable pins

No, you can't manipulate the pins. A pin that supports pin change interrupts can trigger an interrupt when the pin changes state.

What, specifically, is your problem? The pin change interrupt library shows how to assign handlers and provides some clues about what a handler can do.