-
Notifications
You must be signed in to change notification settings - Fork 2
/
sample-toggle.js
43 lines (31 loc) · 897 Bytes
/
sample-toggle.js
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
/*
The code waits for the button state to change. Whenever the button is pressed, the light is toggled on or off.
This sample relies on you connecting an LED and a momentary button.
You might find these useful for figuring out how to wire those up:
- https://www.arduino.cc/en/Tutorial/Button
- https://www.arduino.cc/en/Tutorial/Blink
*/
var GPIOHelper = require('./gpiohelper');
var helper = new GPIOHelper();
var toggle = false;
var pins = {
led: 14,
button: 23
};
// Function which updates the state of the light, based on the value of 'toggle'
var updateLight = function() {
if(toggle) {
helper.setPinSync(pins.led, true);
} else {
helper.setPinSync(pins.led, false);
}
}
// Listen for change events
helper.onPinChange(pins.button, function(isDown) {
console.log("Button is...", isDown);
if(isDown) {
toggle = !toggle;
}
updateLight();
});
updateLight();