Skip to content

Instantly share code, notes, and snippets.

@ikr7
Created August 26, 2014 08:06
Show Gist options
  • Save ikr7/1e46f39edbd2e0ce2ed3 to your computer and use it in GitHub Desktop.
Save ikr7/1e46f39edbd2e0ce2ed3 to your computer and use it in GitHub Desktop.
GPIO WATCHER
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