Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

Interrupts + Pin 3 with Zumo Arduino Shield

I’d like to add whisker bumpers to my Zumo (the Arduino Shield version).

My Uno has hardware interrupts on pins 2 and 3, I believe. I can access Pin 2 on the front expansion area, easy peasy. I can access Pin 3 on the side expansion area. I have jumpered the buzzer into life, which is on Pin 3.

I have a few questions:

Did enabling the buzzer Pin 3 mean I can’t use it for anything else? (I haven’t tried yet because disassembly and soldering a piece of header takes more time than simply asking.) Or does it just mean I won’t be using the buzzer while I’m also using it for an interrupt?

Rather than an external interrupt, I was also considering a timer interrupt (detailed here) to use any ol’ pin for the whiskers, and just check them every millisecond. But I see this part of the code:

  OCR0A = 0xAF;
  TIMSK0 |= _BV(OCIE0A);

and I glaze over. I don’t really understand what’s happening there other than about halfway through the timer (175), it does a thing. In the tutorial, it seems that the ISR is whatever function is just below it? Or is the gibberish above calling the ISR by name? Is that what SIGNAL(TIMER0_COMPA_vect) does? If so, how does TIMER0_COMPA_vect translate from gibberishy code above it?

I’m just getting back into Arduino and robotics, and was always a rank amateur anyway. Thank you in advance for over-explaining any advice.

-Thom
Portland, Ore.

I found a little more info that I think explains the timer interrupt questions here.

Output Compare Match:
When a output compare match interrupt occurs, the OCFxy flag will be set in the interrupt flag register TIFRx . When the output compare interrupt enable bit OCIExy in the interrupt mask register TIMSKx is set, the output compare match interrupt service ISR(TIMERx_COMPy_vect) routine will be called.

Hi, Thom.

Unfortunately, it’s hard to use that pin for anything else while it’s controlling the buzzer. Since it’s set up as a PWM output that drives both high and low, connecting a switch to the same pin could create a short circuit if the switch is pressed while the pin is driving. Also, even if the Arduino pin were set to an input, leaving the buzzer connected would cause it to click or pop whenever the switch is pressed. You could probably work around the first issue by adding an inline resistor to avoid a direct short, and you could just ignore the buzzer clicks, but it would probably be a better idea to use a different pin if possible.

I assume you’re using an Arduino Uno, in which case you’re right that using external interrupts limits you to pins 2 and 3. Instead, you might consider using pin change interrupts, which are available on basically every I/O pin. They are a little harder to use (the Arduino environment doesn’t provide any functions that make it easier), but this article might be a good introduction, and you can also find other resources by doing a Web search for something like “Arduino pin change interrupt”.

I think the page you linked should explain timer interrupts pretty well, but let us know if you are still unsure about something specific.

Kevin

Re: Pin 3: that makes sense. Sounds like I’ll just abandon that as an option (at least until I disassemble it the next time).

I also found a pin change interrupt library that seems promising, and I suppose I could bake my own like that article (which I seem to remember from… whenever I was last exploring it).

Thanks in advance — I’ll post my little project when it’s done. I think it may end up being some sort of roving Christmas tree defender against the cat.