Permalink
Browse files

Add led.isRunning property; New Force Sensitive Resistor program

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
  • Loading branch information...
1 parent cc791d7 commit a07b70e575dfa093c2a99935508b4b09ce320b34 @rwaldron committed May 16, 2012
Showing with 63 additions and 5 deletions.
  1. BIN docs/breadboard/sensor-fsr.fzz
  2. BIN docs/breadboard/sensor-fsr.png
  3. +29 −0 eg/sensor-fsr.js
  4. +34 −5 lib/led.js
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -0,0 +1,29 @@
+var five = require("../lib/johnny-five.js"),
+ board, fsr, led;
+
+board = new five.Board();
+
+board.on("ready", function() {
+
+ // Create a new `fsr` hardware instance.
+ fsr = new five.Sensor({
+ pin: "A0",
+ freq: 25
+ });
+
+ led = new five.Led(9);
+
+ board.repl.inject({
+ led: led
+ });
+
+
+ fsr.scale([ 0, 255 ]).on("read", function() {
+ // set the led's brightness based on force
+ // applied to force sensitive resistor
+ //
+ //
+
+ led.brightness( this.value );
+ });
+});
View
@@ -42,6 +42,7 @@ function Led( opts ) {
led.set( this, {
isOn: false,
+ isRunning: false,
value: 0
});
@@ -74,6 +75,7 @@ Led.prototype.on = function() {
led.set( this, {
isOn: true,
+ isRunning: false,
value: 255
});
@@ -95,6 +97,7 @@ Led.prototype.off = function() {
led.set( this, {
isOn: false,
+ isRunning: false,
value: 0
});
@@ -112,7 +115,9 @@ Led.prototype.toggle = function() {
this.firmata.pinMode( this.pin, this.mode );
}
- if ( led.get( this ).isOn ) {
+ var ref = led.get( this );
+
+ if ( ref.isOn || ref.isRunning ) {
this.off();
} else {
this.on();
@@ -136,6 +141,7 @@ Led.prototype.brightness = function( val ) {
led.set( this, {
isOn: val ? true : false,
+ isRunning: val ? true : false,
value: val
});
@@ -156,10 +162,22 @@ Led.prototype.pulse = function( time ) {
this.firmata.pinMode( this.pin, this.mode );
}
- var to = ( time || 1000 ) / ( 255 * 2 );
+ var to = ( time || 1000 ) / ( 255 * 2 ),
+ ref = led.get( this );
+
+ // Avoid traffic jams
+ if ( ref.isRunning ) {
+ return;
+ }
+
+ led.set( this, {
+ isOn: true,
+ isRunning: true,
+ value: ref.value
+ });
this.interval = setInterval(function() {
- var valueAt = this.value;
+ var valueAt = led.get( this ).value;
if ( valueAt === 0 ) {
direction = 1;
@@ -190,10 +208,10 @@ Led.prototype.fade = function( val, time ) {
}
var to = ( time || 1000 ) / ( (val || 255) * 2 ),
- direction = this.value <= val ? 1 : -1;
+ direction = led.get( this ).value <= val ? 1 : -1;
this.interval = setInterval(function() {
- var valueAt = this.value;
+ var valueAt = led.get( this ).value;
if ( (direction > 0 && valueAt === 255) ||
(direction < 0 && valueAt === 0) ||
@@ -227,6 +245,17 @@ Led.prototype.strobe = function( rate ) {
this.firmata.pinMode( this.pin, this.mode );
}
+ // Avoid traffic jams
+ if ( led.get(this).isRunning ) {
+ return;
+ }
+
+ led.set( this, {
+ isOn: true,
+ isRunning: true,
+ value: this.value
+ });
+
this.interval = setInterval(function() {
this.toggle();

0 comments on commit a07b70e

Please sign in to comment.