Permalink
Browse files

Merge pull request #14 from rwldrn/ldr

Adds Sensor module for reading arbitrary input sensors
  • Loading branch information...
2 parents 299f1b4 + 8e92682 commit b75fc5b2fea7f036eaeaf62795e57cb066090731 @ecto ecto committed Apr 24, 2012
Showing with 652 additions and 85 deletions.
  1. +2 −1 .gitignore
  2. +4 −0 examples/piezo.js
  3. +31 −0 examples/ping.js
  4. +52 −0 examples/pir.js
  5. +36 −0 examples/sensor-throttled.js
  6. +25 −0 examples/sensor.js
  7. +49 −6 examples/servo.js
  8. +5 −2 index.js
  9. +37 −30 lib/board.js
  10. +5 −2 lib/button.js
  11. +71 −0 lib/ping.js
  12. +69 −0 lib/pir.js
  13. +45 −0 lib/sensor.js
  14. +128 −24 lib/servo.js
  15. +93 −20 src/du.ino
View
@@ -1 +1,2 @@
-node_modules
+.DS_Store
+node_modules
View
@@ -15,3 +15,7 @@ board.on('ready', function(){
piezo.note('b', 1100);
}, 1000);
});
+
+
+// Resources
+// http://arduino.cc/en/Tutorial/Tone4
View
@@ -0,0 +1,31 @@
+var arduino = require('../'),
+ board, ping;
+
+board = new arduino.Board({
+ debug: false
+});
+
+ping = new arduino.Ping({
+ board: board,
+ pin: 7
+});
+
+// 'read' events fire approx ~50ms
+ping.on('read', function(err) {
+
+ // Current sensor data stored in properties
+ // of this Ping instance:
+ //
+ // this.microseconds - time lapse from fire to read
+ // this.inches - calculated distance to object in inches
+ // this.centimeters - calculated distance to object in centimeters
+
+ console.log('Object is ~' + Math.round(this.inches) + ' inches from sensor');
+
+});
+
+// To test, use the following:
+// http://arduino.cc/en/uploads/Tutorial/ping_bb.png
+//
+// More information:
+// http://arduino.cc/en/Tutorial/Ping
View
@@ -0,0 +1,52 @@
+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
+// http://bildr.org/blog/wp-content/uploads/2011/06/PIR-Arduino_hookup.png
+//
+// More information:
+// http://www.ladyada.net/learn/sensors/pir.html
@@ -0,0 +1,36 @@
+var arduino = require('../'),
+ board, sensor, piezo;
+
+board = new arduino.Board({
+ debug: false
+});
+
+sensor = new arduino.Sensor({
+ board: board,
+ pin: 'A0',
+ throttle: 100
+});
+
+piezo = new arduino.Piezo({
+ board: board,
+ pin: 11
+});
+
+sensor.on('read', function(err, value) {
+ value = +value;
+
+ // |value| is the raw sensor output
+ console.log( value );
+
+ if ( value > 0 ) {
+ piezo.note('b', 100);
+ }
+});
+
+// Tested with:
+// SoftPot
+// http://www.spectrasymbol.com/how-it-works-softpot
+// http://www.sparkfun.com/datasheets/Sensors/Flex/SoftPot-Datasheet.pdf
+//
+// sensor
+// http://www.ladyada.net/learn/sensors/cds.html
View
@@ -0,0 +1,25 @@
+var arduino = require('../'),
+ board, sensor;
+
+board = new arduino.Board({
+ debug: true
+});
+
+sensor = new arduino.Sensor({
+ board: board,
+ pin: 'A0'
+});
+
+sensor.on('read', function(err, value) {
+ value = +value;
+ // |value| is the raw sensor output
+ console.log( value );
+});
+
+// Tested with:
+// SoftPot
+// http://www.spectrasymbol.com/how-it-works-softpot
+// http://www.sparkfun.com/datasheets/Sensors/Flex/SoftPot-Datasheet.pdf
+//
+// sensor
+// http://www.ladyada.net/learn/sensors/cds.html
View
@@ -1,13 +1,56 @@
-var arduino = require('../');
+var arduino = require('../'),
+ board, led, servo;
-var board = new arduino.Board({
+// Construct instances
+board = new arduino.Board({
debug: true
});
-var servo = new arduino.Servo({
- board: board
+led = new arduino.Led({
+ board: board,
+ pin: 13
});
-board.on('ready', function(){
- servo.sweep();
+servo = new arduino.Servo({
+ board: board,
+ pin: 9
});
+
+// Once servo is attached:
+// - "read"
+// - log position
+// - "aftersweep"
+// - blink the led
+// - read the position
+// - detach the servo
+// - "detached"
+// - log detach message
+//
+// - execute full sweep
+
+servo.on('attached', function(err) {
+ console.log('attached');
+
+ this.on('read', function(err, pos) {
+ console.log(pos);
+ });
+
+ this.on('detached', function(err) {
+ console.log('detached');
+ });
+
+ this.on('aftersweep', function(err) {
+ led.blink();
+
+ this.read();
+ this.detach();
+ });
+
+ this.sweep();
+});
+
+// To test, use the following:
+// http://arduino.cc/en/uploads/Tutorial/sweep_BB.png
+//
+// More information:
+// http://www.ladyada.net/learn/sensors/pir.html
View
@@ -4,5 +4,8 @@ module.exports = {
Led: require('./lib/led'),
Piezo: require('./lib/piezo'),
Button: require('./lib/button'),
- Servo: require('./lib/servo')
-}
+ Servo: require('./lib/servo'),
+ Sensor: require('./lib/sensor'),
+ Ping: require('./lib/ping'),
+ PIR: require('./lib/pir')
+};
View
@@ -68,30 +68,38 @@ util.inherits(Board, events.EventEmitter);
* Loop through all USB devices and try to connect
* This should really message the device and wait for a correct response
*/
-Board.prototype.detect = function (cb) {
+Board.prototype.detect = function (callback) {
this.log('info', 'attempting to find Arduino board');
var self = this;
child.exec('ls /dev | grep usb', function(err, stdout, stderr){
- var possible = stdout.slice(0, -1).split('\n'),
- found = false;
- for (var i in possible) {
- var tempSerial, err;
- try {
- tempSerial = new serial.SerialPort('/dev/' + possible[i], {
- baudrate: 115200,
- parser: serial.parsers.readline("\n")
- });
- } catch (e) {
- err = e;
- }
- if (!err) {
- found = tempSerial;
- self.log('info', 'found board at ' + tempSerial.port);
- break;
+ var usb = stdout.slice(0, -1).split('\n'),
+ found = false,
+ err = null,
+ possible, temp;
+
+ while ( usb.length ) {
+ possible = usb.pop();
+
+ if (possible.slice(0, 2) !== 'cu') {
+ try {
+ temp = new serial.SerialPort('/dev/' + possible, {
+ baudrate: 115200,
+ parser: serial.parsers.readline('\n')
+ });
+ } catch (e) {
+ err = e;
+ }
+ if (!err) {
+ found = temp;
+ self.log('info', 'found board at ' + temp.port);
+ break;
+ } else {
+ err = new Error('Could not find Arduino');
+ }
}
}
- if (found) cb(null, found);
- else cb(new Error('Could not find Arduino'));
+
+ callback(err, found);
});
}
@@ -131,20 +139,18 @@ Board.prototype.write = function (m) {
* Add a 0 to the front of a single-digit pin number
*/
Board.prototype.normalizePin = function (pin) {
- return ("00" + pin).substr(-2);
- /*pin = String(pin).split('');
- if (pin.length > 1) {
- return pin.join('');
- } else {
- pin.unshift('0');
- return pin.join('');
- }*/
+ return this.lpad( 2, '0', pin );
}
Board.prototype.normalizeVal = function(val) {
- return ("000" + val).substr(-3);
+ return this.lpad( 3, '0', val );
}
+//
+Board.prototype.lpad = function(len, chr, str) {
+ return (Array(len + 1).join(chr || ' ') + str).substr(-len);
+};
+
/*
* Define constants
*/
@@ -209,9 +215,10 @@ Board.prototype.delay = function (ms) {
/*
* Logger utility function
*/
-Board.prototype.log = function (level, message) {
+Board.prototype.log = function (/*level, message*/) {
+ var args = [].slice.call(arguments);
if (this.debug) {
- console.log(String(+new Date()).grey + ' duino '.blue + level.magenta + ' ' + message);
+ console.log(String(+new Date()).grey + ' duino '.blue + args.shift().magenta + ' ' + args.join(', '));
}
}
View
@@ -18,16 +18,19 @@ var Button = function (options) {
}, 50);
this.board.on('data', function (m) {
m = m.slice(0, -1).split('::');
+
+ var err = null;
+
if (m.length > 1 && m[0] == self.pin) {
// 0 is up
// 1 is down
if (m[1] == 0 && self.down) {
self.down = false;
- self.emit('up');
+ self.emit('up', err);
}
if (m[1] == 1 && !self.down) {
self.down = true;
- self.emit('down');
+ self.emit('down', err);
}
}
});
Oops, something went wrong.

0 comments on commit b75fc5b

Please sign in to comment.