Interrupt and PIN selection

I am porting code from an Arduino UNO, and so far, all is good - except for the Interrupt pin.

I’m not sure what my options are with regards this line:

attachInterrupt(SILENT_BOTTON_INTERRUPT, handleButtonStateISR, RISING);

What should SILENT_BOTTON_INTERRUPT be, and for that to be used, which digital pin would I use on the board?

I’m currently using D0-D3 as normal pins. Would I need to free up one of those? Or can I use D4, D5 or D6?

You assume we all know what exact UNO code you are porting. I am not familiar and a quick google search for SILENT_BOTTON_INTERRUPT results in only this post as a result. I would suggest posting some code for reference and so we can see the context in which SILENT_BOTTON_INTERRUPT is declared or defined. Also, which Particle device are you using? If using mesh, then choose just about any Ax or Dx pin. From the docs for the Xenon:

“All A and D pins (including TX, RX, and SPI) on Gen 3 (mesh) devices can be used for interrupts, however you can only attach interrupts to 8 pins at the same time.”

1 Like

Ah yeah, sorry about that.

The code I have, which I use on the UNO, is:

int SILENT_BUTTON_PIN = 3; // Interrupt pin on UNO so…
int SILENT_BOTTON_INTERRUPT = 1; // Pin 3 = Interrupt 1. If using Pin 2, then Interrupt is 0.

So, I use pin D2 as the pin my button is connected to. On the UNO, Pin D2 and D3 can be used as interrupts. If using D2, then the interrupt would be 0. And on D3, the interrupt is 1. (You only have 2 interrupt pins).

So when I attach the interrup in my setup(), I use:

attachInterrupt(SILENT_BOTTON_INTERRUPT, handleButtonStateISR, RISING); // Attach an Interrup to the button press pin.

That calls my method, handleButtonStateISR(), which simply toggles a LED on and off.

attachInterrupt([Interrupt number], [MethodToExecute], [StateChange - RISING, FALLING or CHANGE]);

What I am trying to find is which pins on the Particle Photon I can use, an what their Interrupt number would be.

On the UNO, it’s:
D2 = Interrupt 0
D3 = Interrupt 1

What I am trying, on the Photon is assigning the same Pin number as the Interrupt:


And then:

attachInterrupt(SILENT_BOTTON_INTERRUPT, handleButtonStateISR, RISING);

And …

Oh my God, that worked!!
I had the attachInterrupt line commented out! :slight_smile:

Is this the right way to do it? Basically, on the Photon, all pins can be used as Interrupts, and … you just assign the pin number to the attach call?

Glad you got it working. Most pins on the Particle devices can be used as an interrupt pin. On Gen 2 devices, the interrupts are shared by several pins so you have to choose your interrupt pins carefully. On Gen 3 devices, just about any pin can be used as an interrupt but you can only have 8 interrupts active at a time.

In general, it is not good practice to assign your pins by numeric designations (i.e. 1,2,3,etc.) Using pin numbers makes it very difficult to move your code from device to device. For example, you cannot guarantee that the pin numbers are the same from a Photon to an Electron… or even an Electron to an E-Series. If you did use numeric designations only, you would have to port your code for each device. The better practice is to use the alpha numeric designations (i.e. D0,D1,D2,etc.) If you use alpha numeric designations, you can move your code amongst most of the Particle devices and can have confidence the code will run (usually) without any porting. The Particle engineers already put an abstraction layer in there so that the Dx/Ax designations map back to pin numbers.