Skip to content

Commit 4ea8a38

Browse files
committed
Updates to attachInterrupt()
1 parent 5097f04 commit 4ea8a38

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/content/reference/firmware.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3954,13 +3954,17 @@ void loop()
39543954
Interrupts are a way to write code that is run when an external event occurs.
39553955
As a general rule, interrupt code should be very fast, and non-blocking. This means
39563956
performing transfers, such as I2C, Serial, TCP should not be done as part of the
3957-
interrupt handler. Rather, the interrupt handleer can set a variable which instructs
3957+
interrupt handler. Rather, the interrupt handler can set a variable which instructs
39583958
the main loop that the event has occurred.
39593959

39603960
### attachInterrupt()
39613961

39623962
Specifies a function to call when an external interrupt occurs. Replaces any previous function that was attached to the interrupt.
39633963

3964+
**NOTE:**
3965+
`pinMode()` MUST be called prior to calling attachInterrupt() to set the desired mode for the interrupt pin (INPUT, INPUT_PULLUP or INPUT_PULLDOWN).
3966+
3967+
39643968
```C++
39653969
// EXAMPLE USAGE
39663970

@@ -3971,6 +3975,7 @@ volatile int state = LOW;
39713975
void setup()
39723976
{
39733977
pinMode(ledPin, OUTPUT);
3978+
pinMode(D2, INPUT_PULLUP);
39743979
attachInterrupt(D2, blink, CHANGE);
39753980
}
39763981

@@ -3991,6 +3996,7 @@ You can attach a method in a C++ object as an interrupt handler.
39913996
class Robot {
39923997
public:
39933998
Robot() {
3999+
pinMode(D2, INPUT_PULLUP);
39944000
attachInterrupt(D2, &Robot::handler, this, CHANGE);
39954001
}
39964002
void handler() {
@@ -4026,14 +4032,18 @@ External interrupts are supported on the following pins:
40264032

40274033
The function does not return anything.
40284034

4029-
**NOTE:**
4030-
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.
4031-
4032-
*Using Interrupts:*
4035+
**Using Interrupts:**
40334036
Interrupts are useful for making things happen automatically in microcontroller programs, and can help solve timing problems. Good tasks for using an interrupt may include reading a rotary encoder, or monitoring user input.
40344037

40354038
If you wanted to insure that a program always caught the pulses from a rotary encoder, so that it never misses 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 input.
40364039

4040+
**About Interrupt Service Routines:**
4041+
ISRs are special kinds of functions that have some unique limitations most other functions do not have. An ISR cannot have any parameters, and they shouldn't return anything.
4042+
4043+
Generally, an ISR should be as short and fast as possible. If your sketch uses multiple ISRs, only one can run at a time, other interrupts will be executed after the current one finishes in an order that depends on the priority they have. `millis()` relies on interrupts to count, so it will never increment inside an ISR. Since `delay()` requires interrupts to work, it will not work if called inside an ISR. Using `delayMicroseconds()` will work as normal.
4044+
4045+
Typically global variables are used to pass data between an ISR and the main program. To make sure variables shared between an ISR and the main program are updated correctly, declare them as `volatile`.
4046+
40374047

40384048
### detachInterrupt()
40394049

0 commit comments

Comments
 (0)