Hardware Reference
In-Depth Information
Solution
Connect the switch to a GPIO pin and use BoneScript
pinMode()
and
attachInterrupt()
functions.
To make this recipe, you will need:
▪ Breadboard and jumper wires (see
“Prototyping Equipment”
)
▪ Pushbutton switch (see
“Miscellaneous”
)
▪ Magnetic reed switch (optional, see
“Miscellaneous”
)
You can wire up either a pushbutton, a magnetic reed switch, or both on the Bone, as
shown in
Figure 2-5
.
Figure 2-5. Diagram for wiring a pushbutton and magnetic reed switch input
Example 2-1. Monitoring a pushbutton using a callback function (pushbutton.js)
#!/usr/bin/env node
var
b
=
require
(
'bonescript'
);
var
button
=
'P9_42'
;
b
.
pinMode
(
button
,
b
.
INPUT
,
7
,
'pulldown'
,
'fast'
,
doAttach
);
function
doAttach
(
x
) {
if
(
x
.
err
) {
console
.
log
(
'x.err = '
+
x
.
err
);