Skip to content

Commit

Permalink
Merge pull request #14 from rwldrn/ldr
Browse files Browse the repository at this point in the history
Adds Sensor module for reading arbitrary input sensors
  • Loading branch information
Cam Pedersen committed Apr 24, 2012
2 parents 299f1b4 + 8e92682 commit b75fc5b
Show file tree
Hide file tree
Showing 15 changed files with 652 additions and 85 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1 +1,2 @@
node_modules
.DS_Store
node_modules
4 changes: 4 additions & 0 deletions examples/piezo.js
Expand Up @@ -15,3 +15,7 @@ board.on('ready', function(){
piezo.note('b', 1100);
}, 1000);
});


// Resources
// http://arduino.cc/en/Tutorial/Tone4
31 changes: 31 additions & 0 deletions examples/ping.js
@@ -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
52 changes: 52 additions & 0 deletions examples/pir.js
@@ -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
36 changes: 36 additions & 0 deletions examples/sensor-throttled.js
@@ -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
25 changes: 25 additions & 0 deletions examples/sensor.js
@@ -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
55 changes: 49 additions & 6 deletions examples/servo.js
@@ -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
7 changes: 5 additions & 2 deletions index.js
Expand Up @@ -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')
};
67 changes: 37 additions & 30 deletions lib/board.js
Expand Up @@ -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);
});
}

Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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(', '));
}
}

Expand Down
7 changes: 5 additions & 2 deletions lib/button.js
Expand Up @@ -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);
}
}
});
Expand Down

0 comments on commit b75fc5b

Please sign in to comment.