Teensy 2.0 ++ attachInterrupt issue

Status
Not open for further replies.

binux

Member
Hello everybody,

I'm building a simple weather station based on the sparkfun sen 08942 hardware. --> Datasheet
My circuit consists out of a Teensy 2 ++ and the aneometer directly wired. (the other ones I don't need in the moment)

Now in order to log this properly I use the
Code:
attachInterrupt(0, functionanme, FALLING);

function. It works. However the interrupt is not triggered correctly. More precisely it gets triggered three times while the switch actually only closes once.

I use the simplest code for testing:

Code:
volatile int aneoRevs = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  
  pinMode(1, INPUT_PULLUP);
  attachInterrupt(1, aneometer, FALLING);
}

void aneometer() {
   aneoRevs++;
   Serial.println(aneoRevs);
}

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

As you see the aneometer is connected to the D1 pin on the ++.

The strange thing is, that once I switch from the teensy to an Arduino Uno, the issue is gone and he's counting the revs correctly.

I guess this has to do with the Teensy 2 ++ hardware, but I don't know what it is. Can someone may point me out what I have to change in my circuit to get it running properly?

Thanks in advance.
 
Last edited:
The reason why the code works on UNO and not on Teensy could be that Serial.println(aneoRevs) in the interrupt handler will take only a few microseconds using the USB Serial on Teensy, but on UNO using 9600 baud, it will take around 5 or 6 milliseconds using the UART serial port. This extra time is probably enough for the contact bounce to stop before the interrupt handler returns. So the UNO only sees one interrupt per turn, but the faster Serial output, and therefore faster interrupt handler, allows the Teensy to count several contact bounces.
 
Normally the Bounce library should be used. Interrupts are terribly frustrating with mechanical chatter. The faster the processor, the more able it is to respond to the mechanical chatter. Teensy is much, much faster than Arduino Uno.

Use the Bounce library. It really works well.

Or if you really want to use the interrupt, you can build hardware to deal with the chatter, or at code to measure the microseconds since the last interrupt and ignore interrupts that occur too quickly.
 
Status
Not open for further replies.
Back
Top