Teensy Interrupts for Dummies

Status
Not open for further replies.

thomen

Well-known member
Hi I was hoping to get some information on interrupts for dummies. Alot of the interrupt page on pjrc needs a few more reads to digest.

I previously had attachInterrupt(0, pin_ISR, CHANGE); on a uno but it doesn't work on a teensy.

I was wondering why?
Thanks :)
 
attachInterrupt() does indeed work on Teensy.

However, on all Teensy 3.x boards, the first input is the pin number. On Uno, interrupt 0 means to use pin 2. On Teensy, this uses pin 0. If you want to use pin 2, you would write:

attachInterrupt(2, pin_ISR, CHANGE);

Arduino supports a digitalPinToInterrupt() function, so you could write this code:

attachInterrupt(digitalPinToInterrupt(2), pin_ISR, CHANGE);

If you use digitalPinToInterrupt, the same code will work on Arduino Uno and Teensy, and all other boards which may have different interrupts assigned to pin numbers.
 
Also, please keep in mind Teensy is *much* faster than Arduino Uno. If you try to use attachInterrupt for switches or pushbuttons, the mechanical chatter is very frustrating. For buttons, use the Bounce library to automatically deal with mechanical chatter.
 
Again it always helps to give a little more information, like example program that is not working and wiring setup and which version of Arduino and Teensyduino ...

More information in the documentation: https://www.arduino.cc/en/Reference/attachInterrupt

But my assumption is here you are used to UNO where that attachIntterupt(0... ) Implies on Interrupt 0 which I believe is on UNO pin 2...

On Teensy boards as well as more advanced Arduino boards like DUE, the first parameter is the pin number, so you are asking for an interrupt on Pin 0...

So to make code work properly for simple AVR boards as well as more advanced, you will see up the docs:
Code:
const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}

void loop() {
  digitalWrite(ledPin, state);
}

void blink() {
  state = !state;
}
In here you will see they use the mapper function digitalPinToInterrupt, to map differently depending on which board. On UNO
digitalPinToInterrupt(2) will return 0, on Teensy it will return 2...
 
Again it always helps to give a little more information, like example program that is not working and wiring setup and which version of Arduino and Teensyduino ...

More information in the documentation: https://www.arduino.cc/en/Reference/attachInterrupt

But my assumption is here you are used to UNO where that attachIntterupt(0... ) Implies on Interrupt 0 which I believe is on UNO pin 2...

On Teensy boards as well as more advanced Arduino boards like DUE, the first parameter is the pin number, so you are asking for an interrupt on Pin 0...

So to make code work properly for simple AVR boards as well as more advanced, you will see up the docs:
Code:
const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}

void loop() {
  digitalWrite(ledPin, state);
}

void blink() {
  state = !state;
}
In here you will see they use the mapper function digitalPinToInterrupt, to map differently depending on which board. On UNO
digitalPinToInterrupt(2) will return 0, on Teensy it will return 2...

Thank you Paul & Kurt,
Apologies for the lack of detail AGAIN!

So I was bench testing on an uno before I rigged up the teensy for a stepper motor home scenario.. The bottom of the stepper motor position is determined by a hall effect and some magnets.. The top is determined by a micro switch (I wanted to try both for fun).

The hall effect gets noticed well but I noticed the motor was travelling past the button and pushing for a bit before realising it had been hit so I added an interrupt to fix it which it worked pretty well.

Thanks both for taking the time to write some plain english replies I really appreciate it. I need to read up on the different types of interrupts and when to use them etc.. I'd love to learn more about them as we're doing a visualisation project for our next assignment with some audio as well and I think they could come in handy..

Thanks again for your time.
 
Status
Not open for further replies.
Back
Top