Skip to content
Browse files

Implements PIR sensor inferface; calibrated, motionstart, motionend e…

…vents

Signed-off-by: Rick Waldron waldron.rick@gmail.com <waldron.rick@gmail.com>
  • Loading branch information...
1 parent efa684c commit d9d7284bedb0ae6c55237d629d84b28bef15ffc3 @rwaldron rwaldron committed Mar 18, 2012
Showing with 122 additions and 1 deletion.
  1. +51 −0 examples/pir.js
  2. +2 −1 index.js
  3. +69 −0 lib/pir.js
View
51 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
View
3 index.js
@@ -6,5 +6,6 @@ module.exports = {
Button: require('./lib/button'),
Servo: require('./lib/servo'),
Sensor: require('./lib/sensor'),
- Ping: require('./lib/ping')
+ Ping: require('./lib/ping'),
+ PIR: require('./lib/pir')
};
View
69 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.
Something went wrong with that request. Please try again.