aboutsummaryrefslogtreecommitdiffstats
path: root/source/arduino/attachinterrupt.rst
blob: 27a1d77646d9b99baaf21739d9d1110663254625 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
.. _arduino-attachinterrupt:

attachInterrupt(interrupt, function, mode)
==========================================



Description
-----------

Specifies a function to call when an external interrupt occurs.
Replaces any previous function that was attached to the interrupt.
Most Arduino boards have two external interrupts: numbers 0 (on
digital pin 2) and 1 (on digital pin 3). The Arduino Mega has an
additional four: numbers 2 (pin 21), 3 (pin 20), 4 (pin 19), and 5
(pin 18).



Parameters
----------

**interrupt**: the number of the interrupt (*int*)



**function**: the function to call when the interrupt occurs; this
function must take no parameters and return nothing. This function
is sometimes referred to as an *interrupt service routine.*



**mode** defines when the interrupt should be triggered. Four
contstants are predefined as valid values:


-  **LOW** to trigger the interrupt whenever the pin is low,
-  **CHANGE** to trigger the interrupt whenever the pin changes
   value
-  **RISING** to trigger when the pin goes from low to high,
-  **FALLING** for when the pin goes from high to low.



Returns
-------

none



Note
----

*Inside the attached function, delay() won't work and the value returned by millis() will not increment. Serial data received while in the function may be lost. You should declare as volatile any variables that you modify within the attached function.*



Using Interrupts
----------------

Interrupts are useful for making things happen automatically in
microcontroller programs, and can help solve timing problems. A
good task for using an interrupt might be reading a rotary encoder,
monitoring user input.



If you wanted to insure that a program always caught the pulses
from a rotary encoder, never missing a pulse, it would make it very
tricky to write a program to do anything else, because the program
would need to constantly poll the sensor lines for the encoder, in
order to catch pulses when they occurred. Other sensors have a
similar interface dynamic too, such as trying to read a sound
sensor that is trying to catch a click, or an infrared slot sensor
(photo-interrupter) trying to catch a coin drop. In all of these
situations, using an interrupt can free the microcontroller to get
some other work done while not missing the doorbell.



Example
-------

::

    int pin = 13;
    volatile int state = LOW;
    
    void setup()
    {
      pinMode(pin, OUTPUT);
      attachInterrupt(0, blink, CHANGE);
    }
    
    void loop()
    {
      digitalWrite(pin, state);
    }
    
    void blink()
    {
      state = !state;
    }



See also
--------


-  `detachInterrupt <http://arduino.cc/en/Reference/DetachInterrupt>`_