Skip to content

Commit

Permalink
Basic change sensitivity settings for Sensor instances
Browse files Browse the repository at this point in the history
Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
  • Loading branch information
rwaldron committed Dec 1, 2012
1 parent df3d1d0 commit aa5b2b2
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/sensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function Sensor( opts ) {
// Servo instance properties
this.freq = opts.freq || 25;
this.range = opts.range || [ 0, 1023 ];
this.sensitivity = opts.sensitivity === undefined ? 1 : opts.sensitivity;

// Analog Read event loop
this.firmata.analogRead( this.pin, function( data ) {
Expand All @@ -83,7 +84,7 @@ function Sensor( opts ) {

// Throttle
setInterval(function() {
var err, median;
var err, median, low, high;

err = null;

Expand All @@ -99,9 +100,14 @@ function Sensor( opts ) {
//
// Includes all aliases
if ( median !== last ) {
aliases.change.forEach(function( change ) {
this.emit( change, err, median );
}, this );
low = last - this.sensitivity;
high = last + this.sensitivity;

if ( median < low || median > high ) {
aliases.change.forEach(function( change ) {
this.emit( change, err, median );
}, this );
}
}

// Store this media value for comparison
Expand Down

0 comments on commit aa5b2b2

Please sign in to comment.