Kaluma
Search…
Interrupts

attachInterrupt(pin, callback[,events])

  • pin <number> The pin number which can support GPIO function.
  • callback <function>(pin, event) The function is called when the event is triggered on the pin. the pin and event are the arguments of the callback function.
  • events <number> Set the events of the pin. There are three events,FALLING (4), RISING (8), and CHANGE (12). Default: CHANGE.
Run the callback function when the events is triggered on the pin. There are three events for the interrupts. The FALLING (4) event is triggered when the pin state is changed from HIGH to LOW. The RISING (8) event is triggered when the pin state is changed from LOW to HIGH. The CHANGE (12) event is triggered when the pin state is changed to any states, which means the CHANGE event is the same as the FALLING + RISING events.
The interrupt does not support level triggering, you can use setWatch() function when you want to use level triggering events (LOW_LEVEL, HIGH_LEVEL).
1
// Set interrupt on the GPIO0
2
const {GPIO} = require('gpio');
3
// GPIO0 and GPIO1 are connected together
4
pinMode(0, INPUT); // GPIO0 is input port which check the state change of GPIO1
5
pinMode(1, OUTPUT); // GPIO1 is output
6
digitalWrite(1, LOW); // Set LOW on the GPIO1
7
8
// Callback function for the GPIO0
9
function callback0(pin, mode) {
10
console.log("Event " + mode + " is triggered on the GPIO" + pin);
11
}
12
13
// Set interrupt callback function on GPIO0
14
attachInterrupt(0, callback0, CHANGE);
15
16
digitalWrite(1, HIGH); // Callback is called bcause GPIO0 is changed from LOW to HIGH
17
delay(1);
18
digitalWrite(1, LOW); // Callback is called bcause GPIO0 is changed from HIGH to LOW
19
20
detachInterrupt(0); // Remove the interrupt on the GOIO0
21
22
digitalWrite(1, HIGH); // No callback function call for this event
23
delay(1);
24
digitalWrite(1, LOW); // No callback function call for this event
Copied!

detachInterrupt(pin)

  • pin <number> The pin number which can support GPIO function.
Remove the interrupts on the pin which is added by attachInterrupt() function.

enableInterrupts()

Enable all the interrupts which is attached by attachInterrupt() function. The interrupts are automatically enabled when attachedInterrupts() is called so it is not needed to use this function if you didn't call disableInterrupts() function.

disableInterrupts()

Disable all the interrupts which is attached by attachInterrupt() function. The interrupts will be enabled when enableInterrupts() is called.
1
// Set interrupt on the GPIO0
2
const {GPIO} = require('gpio');
3
// GPIO0 and GPIO1 are connected together
4
pinMode(0, INPUT); // GPIO0 is input port which check the state change of GPIO1
5
pinMode(1, OUTPUT); // GPIO1 is output
6
digitalWrite(1, LOW); // Set LOW on the GPIO1
7
8
// Callback function for the GPIO0
9
function callback0(pin, mode) {
10
console.log("Event " + mode + " is triggered on the GPIO" + pin);
11
}
12
13
// Set interrupt callback function on GPIO0
14
attachInterrupt(0, callback0, CHANGE);
15
16
digitalWrite(1, HIGH); // Callback is called bcause GPIO0 is changed from LOW to HIGH
17
delay(1);
18
digitalWrite(1, LOW); // Callback is called bcause GPIO0 is changed from HIGH to LOW
19
20
disableInterrupts(); // Disable all the interrupts
21
22
digitalWrite(1, HIGH); // No callback function call for this event
23
delay(1);
24
digitalWrite(1, LOW); // No callback function call for this event
25
26
enableInterrupts(); // Enable all the interrupts
27
28
digitalWrite(1, HIGH); // Callback is called bcause GPIO0 is changed from LOW to HIGH
29
delay(1);
30
digitalWrite(1, LOW); // Callback is called bcause GPIO0 is changed from HIGH to LOW
Copied!
Last modified 2mo ago