Skip to content

Reed Sensor #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions examples/reed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var arduino = require('../');

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

var reed = new arduino.Reed({
board : board,
pin : 7,
emitInitState: true
});

reed.on('calibrated', function(err, date) {
console.log('calibrated');

this.on('reedopen', function(err, date) {
console.log('reedopen', this.state);
});

this.on('reedclose', function(err, date) {
console.log('reedclose');
});
});
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ module.exports = {
Sensor: require('./lib/sensor'),
Ping: require('./lib/ping'),
PIR: require('./lib/pir'),
LCD: require('./lib/lcd')
LCD: require('./lib/lcd'),
Reed: require('./lib/reed')
};
74 changes: 74 additions & 0 deletions lib/reed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
var events = require('events'),
util = require('util');

/*
* Main Reed constructor
* Process options
* Tell the board to set it up
*/
var Reed = 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 || 7);
this.state = null;
this.calibrated = false;
this.emitInitState = options.emitInitState;

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

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 reed instance
this.state = +data;

// 'reedclose' event fired when reed is closed
if (data === '01') {
this.emit('reedclose', err, timestamp);
}

// 'reedopen' event fired when reed is open
if (data === '00') {
this.emit('reedopen', err, timestamp);
}
}

// 'calibrated' event
if (!this.calibrated) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we emit the initial state of the reed sensor as well?

this.calibrated = true;
this.state = +data;
this.emit('calibrated', err, timestamp);

if(this.emitInitState) {
if(data === '01') {
this.emit('reedclose', err, timestamp);
} else if(data === '00') {
this.emit('reedopen', err, timestamp);
}
}
}
}
}.bind(this));
};

util.inherits(Reed, events.EventEmitter);

module.exports = Reed;