4

Interrupts and Timers

Interrupts allow microcontrollers to respond to events without having to poll continually to see if anything has changed. In addition to associating interrupts with certain pins you can also use timer-generated interrupts.

Hardware Interrupts

As an example of how to use interrupts, let’s revisit digital inputs. The most common way to detect when something has happened at an input (say a switch has been pressed) is to use some code like this:

Images

This code means we continually check inputPin and the moment it reads LOW, we do whatever is specified at the //do something comment. This process works well, but what if you have ...

Get Programming Arduino Next Steps: Going Further with Sketches, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.