Kaluma
Search…
Button
The button module supports button. Use require('button') to access this module.

Class: Button

An instances of Button represents a button object. This class is a subclass of EventEmitter.

new Button(pin[, options])

  • pin <number> The pin number which can support button function.
  • options <object>
    • mode <number> The pin mode. Default: INPUT_PULLUP.
    • event <number> The the event of the pin. There are three events, FALLING (0), RISING (1), and CHANGE (2). Default: FALLING.
    • debounce <number> debounce time in ms(milliseconds). Default: 50ms
Create an instances of the Button class. Note that this class uses setWatch() function internally.
1
// Create the Button instance and print the message when button is pressed.
2
const {Button} = require('button');
3
const pin = 0; // Pin number for button
4
const btn0 = new Button(pin);
5
btn0.on('click', function () {
6
console.log('button 1 clicked');
7
})
Copied!

button.read()

  • Returns: <number> The return value is HIGH (1) or LOW (0)
1
// Create the Button instance and close it.
2
const {Button} = require('button');
3
const pin = 0; // Pin number for button
4
const btn0 = new Button(pin);
5
// ...
6
btn0.close();
Copied!

button.close()

This method closes the I/O watcher for the button.
1
// Create the Button instance and close it.
2
const {Button} = require('button');
3
const pin = 0; // Pin number for button
4
const btn0 = new Button(pin);
5
btn0.on('click', function () {
6
console.log('button 1 clicked');
7
})
8
// ...
9
btn0.close();
Copied!

Event: 'click'

The click event is emitted when the button is pressed down.
1
// Create the Button instance and toggle LED when the button is pressed.
2
const {Button} = require('button');
3
const pin = 0; // Pin number for button
4
const btn0 = new Button(pin);
5
const led = 25; // LED
6
pinMode(led, OUTPUT);
7
btn.on('click', function () {
8
digitalToggle(led);
9
})
Copied!
Last modified 7mo ago