Skip to content

Commit

Permalink
Implements PIR sensor inferface; calibrated, motionstart, motionend e…
Browse files Browse the repository at this point in the history
…vents

Signed-off-by: Rick Waldron waldron.rick@gmail.com <waldron.rick@gmail.com>
  • Loading branch information
rwaldron committed Mar 18, 2012
1 parent efa684c commit d9d7284
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 1 deletion.
51 changes: 51 additions & 0 deletions examples/pir.js
@@ -0,0 +1,51 @@
var arduino = require('../'),
board, pir;

board = new arduino.Board({
debug: false
});

pir = new arduino.PIR({
board: board,
pin: 7
});

// 'calibrated' event fired when PIR sensor is
// ready to detect movement/motion in observable range
//
// All events receive error and date arguments
pir.on('calibrated', function(err, date) {

console.log('calibrated');

// Current sensor data stored in properties
// of this PIR instance:
//
// this.state - current state of motion
// 0 No motion currently detected
// 1 Motion currently detected

// 'motionstart' event fired when motion occurs
// within the observable range of the PIR sensor
this.on('motionstart', function(err, date) {

console.log('motionstart', this.state);
console.log( date );

});

// 'motionend' event fired when motion has ceased
// within the observable range of the PIR sensor
this.on('motionend', function(err, date) {

console.log('motionend', this.state);

});
});


// To test, use the following:
// http://www.ladyada.net/images/sensors/pirardbb.gif
//
// More information:
// http://www.ladyada.net/learn/sensors/pir.html
3 changes: 2 additions & 1 deletion index.js
Expand Up @@ -6,5 +6,6 @@ module.exports = {
Button: require('./lib/button'), Button: require('./lib/button'),
Servo: require('./lib/servo'), Servo: require('./lib/servo'),
Sensor: require('./lib/sensor'), Sensor: require('./lib/sensor'),
Ping: require('./lib/ping') Ping: require('./lib/ping'),
PIR: require('./lib/pir')
}; };
69 changes: 69 additions & 0 deletions lib/pir.js
@@ -0,0 +1,69 @@
var events = require('events'),
util = require('util');

/*
* Main PIR constructor
* Process options
* Tell the board to set it up
*/
var PIR = function (options) {
if (!options || !options.board) {
throw new Error('Must supply required options to PIR');
}
this.board = options.board;
this.pin = this.board.normalizePin(options.pin || 9);
this.state = null;
this.calibrated = false;

setInterval(function () {
this.board.digitalRead(this.pin);
}.bind(this), 50);

this.board.on('data', function (message) {
var m = message.slice(0, -1).split('::'),
timestamp = new Date(),
err = null,
pin, data;

if (!m.length) {
return;
}

pin = m[0];
data = m[1];

if (pin === this.pin) {

// If this is not a calibration event
if (this.state != null && this.state != +data) {

// Update current state of PIR instance
this.state = +data;

// 'motionstart' event fired when motion occurs
// within the observable range of the PIR sensor
if (data === '01') {
this.emit('motionstart', err, timestamp);
}

// 'motionend' event fired when motion has ceased
// within the observable range of the PIR sensor
if (data === '00') {
this.emit('motionend', err, timestamp);
}
}

// 'calibrated' event fired when PIR sensor is
// ready to detect movement/motion in observable range
if (!this.calibrated) {
this.calibrated = true;
this.state = +data;
this.emit('calibrated', err, timestamp);
}
}
}.bind(this));
};

util.inherits(PIR, events.EventEmitter);

module.exports = PIR;

0 comments on commit d9d7284

Please sign in to comment.