-
-
Save ikr7/1e46f39edbd2e0ce2ed3 to your computer and use it in GitHub Desktop.
GPIO WATCHER
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var b = require('bonescript'); | |
var EventEmitter = require('events').EventEmitter; | |
var Watcher = function(pinName, interval){ | |
var emitter = new EventEmitter(); | |
var f = 0; | |
b.pinMode(pinName, b.INPUT); | |
setInterval(function(){ | |
b.digitalRead(pinName, function(x){ | |
if(f !== x.value){ | |
if(x.value === 1){ | |
emitter.emit('high'); | |
} | |
if(x.value === 0){ | |
emitter.emit('low'); | |
} | |
} | |
f = x.value; | |
}); | |
}, interval); | |
return emitter; | |
}; | |
var pin8_8 = Watcher('P8_8', 50); | |
pin8_8.on('high', function(){ | |
console.log('HIGH!'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment