Fetching contributors…
Cannot retrieve contributors at this time
68 lines (53 sloc) 2.93 KB
// EnableInterrupt Simple example sketch. Demonstrates operation on a single pin of your choice.
// See and the for more information.
#include <EnableInterrupt.h>
// Modify this at your leisure. Refer to
#if defined __AVR_ATmega640__ || defined __AVR_ATmega2560__ || defined __AVR_ATmega1280__ || \
defined __AVR_ATmega1281__ || defined __AVR_ATmega2561__
#define ARDUINOPIN 10
// Pin 7 is useful on Arduino Uno, Leonardo, Mighty1284, ATtiny84...
#define ARDUINOPIN 7
volatile uint16_t interruptCount=0; // The count will go back to 0 after hitting 65535.
void interruptFunction() {
void setup() {
#ifdef MIGHTY1284
DDRA=0x0; DDRB=0x0; DDRC=0x0; DDRD=0x0; // set all pins as inputs
PORTA=0xFF; PORTB=0xFF; PORTC=0xFF; PORTD=0xFF; // turn on all pullup resistors.
enableInterrupt(ARDUINOPIN, interruptFunction, CHANGE);
// In the loop we just display interruptCount. The value is updated by the interrupt routine.
void loop() {
Serial.print("Pin was interrupted: ");
Serial.print(interruptCount, DEC);
Serial.println(" times so far.");
////////////////////////////////////// GORY DETAILS //////////////////////////////////////
// This has only been tested on an Arduino Duemilanove and Mega ADK.
// It is designed to work with the Arduino Duemilanove/Uno, Arduino Mega2560/ADK, the Arduino
// Leonardo, and the Arduino Due. Please let me know how you fare on the Leonardo or Due.
// To use:
// 1. You must be using a fairly recent version of the Arduino IDE software on your PC/Mac,
// that is, version 1.0.1 or later. Check Help->About Arduino in the IDE.
// 2. Wire a simple switch to any Analog or Digital pin (known as ARDUINOPIN, defined below)
// that supports interrupts. See
// Attach the other end to a GND pin. A "single pole single throw momentary contact normally
// open" // pushbutton switch is best for the most interrupting fun.
// See and
// 3. When pressed, the switch will connect the pin to ground ("low", or "0") voltage, and interrupt the
// processor. See
// 4. The interrupt is serviced immediately, and the ISR (Interrupt SubRoutine) modifies the value of
// the global variable interruptCount. Open Tools->Serial Monitor in the IDE to see the results of your
// interrupts.
// 5. Peruse the Examples directory for more elaborate examples.
// 6. Create your own sketch using the EnableInterrupt library!