diff --git a/README.md b/README.md index f3d84d2..c66698c 100644 --- a/README.md +++ b/README.md @@ -184,6 +184,53 @@ Increment position from 0 to 180. Instruct the servo to immediately go to a position from 0 to 180. +##joystick + +There are 4 events to handle ups/downs on X and Y axis. + +````javascript +var joystick = new arduino.JoyStick({ + board: board +}); + +joystick.on('y-up',function(amount,real_volts){ + console.log('Y Increasing, amount:',amount,real_volts); +}); + +joystick.on('y-down',function(amount,real_volts){ + console.log('Y Decreasing , amount:',amount,real_volts); +}); + +joystick.on('x-up',function(amount,real_volts){ + console.log('X Increasing , amount:',amount,real_volts); +}); + +joystick.on('x-down',function(amount,real_volts){ + console.log('X Decreasing , amount:',amount,real_volts); +}); + +```` +VRYPin is by default A0, VRXPin is by default A1, but they can be specified in the options: + +````javascript +var joystick = new arduino.JoyStick({ + board: board, + vrypin: 'A0', + vrxpin: 'A1' +}); + +```` +Each event returns 2 parameters: "amount" that is a percentage and "real_voltage": + +````javascript + +joystick.on('x-down',function(amount,real_volts){ + console.log('X Decreasing , amount:',amount,real_volts); +}); + +```` + + ##motor ##potentiometer diff --git a/examples/7seg.js b/examples/7seg.js new file mode 100644 index 0000000..8d33804 --- /dev/null +++ b/examples/7seg.js @@ -0,0 +1,286 @@ +var arduino = require('../'); +var board = new arduino.Board({ + debug: true +}); + + + +// segment in led display +var sA = new arduino.Led({ + board: board, + pin: 8 +}); + +var sB = new arduino.Led({ + board: board, + pin: 9 +}); +var sC = new arduino.Led({ + board: board, + pin: 2 +}); +var sD = new arduino.Led({ + board: board, + pin: 3 +}); +var sE = new arduino.Led({ + board: board, + pin: 4 +}); +var dot = new arduino.Led({ + board: board, + pin: 5 +}); +var sG = new arduino.Led({ + board: board, + pin: 6 +}); + +var sF = new arduino.Led({ + board: board, + pin: 7 +}); + +function shownumber(number){ + switch(number){ + case 0: + sA.on(); // - + sB.on(); // | + sG.on(); // | + sF.off(); // - + sE.on(); // | + sC.on(); // | + sD.on(); // - + dot.off();// . + break; + case 1: + sA.off(); // - + sB.on(); // | + sG.off(); // | + sF.off(); // - + sE.off(); // | + sC.on(); // | + sD.off(); // - + dot.off();// . + break; + case 2: + sA.on(); // - + sB.on(); // | + sG.off(); // | + sF.on(); // - + sE.on(); // | + sC.off(); // | + sD.on(); // - + dot.off();// . + break; + case 3: + sA.on(); // - + sB.on(); // | + sG.off(); // | + sF.on(); // - + sE.off(); // | + sC.on(); // | + sD.on(); // - + dot.off();// . + break; + case 4: + sA.off(); // - + sB.on(); // | + sG.on(); // | + sF.on(); // - + sE.off(); // | + sC.on(); // | + sD.off(); // - + dot.off();// . + break; + case 5: + sA.on(); // - + sB.off(); // | + sG.on(); // | + sF.on(); // - + sE.off(); // | + sC.on(); // | + sD.on(); // - + dot.off();// . + break; + case 6: + sA.off(); // - + sB.off(); // | + sG.on(); // | + sF.on(); // - + sE.on(); // | + sC.on(); // | + sD.on(); // - + dot.off();// . + break; + case 7: + sA.on(); // - + sB.on(); // | + sG.off(); // | + sF.off(); // - + sE.off(); // | + sC.on(); // | + sD.off(); // - + dot.off();// . + break; + case 8: + sA.on(); // - + sB.on(); // | + sG.on(); // | + sF.on(); // - + sE.on(); // | + sC.on(); // | + sD.on(); // - + dot.off();// . + break; + case 9: + sA.on(); // - + sB.on(); // | + sG.on(); // | + sF.on(); // - + sE.off(); // | + sC.on(); // | + sD.off(); // - + dot.off();// . + break; + } +} + +function hello(step){ + switch(step){ + case 0: + sA.off(); // - + sB.on(); // | + sG.on(); // | + sF.on(); // - + sE.on(); // | + sC.on(); // | + sD.off(); // - + dot.off();// . + break; + case 1: + sA.on(); // - + sB.off(); // | + sG.on(); // | + sF.on(); // - + sE.on(); // | + sC.off(); // | + sD.on(); // - + dot.off();// . + break; + case 2: + sA.off(); // - + sB.off(); // | + sG.on(); // | + sF.off(); // - + sE.on(); // | + sC.off(); // | + sD.on(); // - + dot.off();// . + break; + case 3: + sA.off(); // - + sB.off(); // | + sG.on(); // | + sF.off(); // - + sE.on(); // | + sC.off(); // | + sD.on(); // - + dot.off();// . + break; + case 4: + sA.on(); // - + sB.on(); // | + sG.on(); // | + sF.off(); // - + sE.on(); // | + sC.on(); // | + sD.on(); // - + dot.off();// . + break; + } + +} + +function snake(step){ + + switch(step){ + case 0: + sG.off(); + sD.off(); // - + sA.on(); // - + break; + case 1: + sA.off(); + sB.on(); // | + + break; + case 2: + sB.off(); // | + sF.on(); // - + + break; + case 3: + sF.off(); // - + sE.on(); // | + break; + case 4: + sE.off(); // | + sD.on(); // - + break; + case 5: + sD.off(); // | + sC.on(); + break; + case 6: + sC.off(); // | + sF.on(); + break; + case 7: + sF.off(); // | + sG.on(); + break; + + } +} + +board.on('ready', function(){ + + + var number = 0; + var showtime = function() { + if (number <= 7){ + snake(number); + number++ + } else { + number = 0; + } + }; + + + // var showtime = function() { + // if (number <= 4){ + // hello(number); + // number++ + // } else { + // number = 0; + // } + // }; + + + + // var showtime = function() { + // if (number <= 9){ + // shownumber(number); + // number++ + // } else { + // number = 0; + // } + + // } + + + setInterval( showtime, 100 ); +}); + + diff --git a/examples/joystick.js b/examples/joystick.js new file mode 100644 index 0000000..ef1da46 --- /dev/null +++ b/examples/joystick.js @@ -0,0 +1,30 @@ +var arduino = require('../'); + +var board = new arduino.Board(); + +board.on('ready', function(){ + + var joystick = new arduino.JoyStick({ board: board }); + + joystick.on('y-up',function(amount,real_volts){ + console.log('Y Increasing, amount:',amount,real_volts); + + }); + + joystick.on('y-down',function(amount,real_volts){ + console.log('Y Decreasing , amount:',amount,real_volts); + }); + + joystick.on('x-up',function(amount,real_volts){ + console.log('X Increasing , amount:',amount,real_volts); + }); + + joystick.on('x-down',function(amount,real_volts){ + console.log('X Decreasing , amount:',amount,real_volts); + }); + +}); + + + + diff --git a/index.js b/index.js index a30f8f9..5a1809c 100644 --- a/index.js +++ b/index.js @@ -7,5 +7,6 @@ module.exports = { Servo: require('./lib/servo'), Sensor: require('./lib/sensor'), Ping: require('./lib/ping'), - PIR: require('./lib/pir') + PIR: require('./lib/pir'), + JoyStick: require('./lib/joystick') }; diff --git a/lib/board.js b/lib/board.js index a1acf89..1e181a1 100644 --- a/lib/board.js +++ b/lib/board.js @@ -70,7 +70,7 @@ util.inherits(Board, events.EventEmitter); * This should really message the device and wait for a correct response */ Board.prototype.detect = function (callback) { - this.log('info', 'attempting to find Arduino board'); + this.log('info', 'attempting to find Arduino board FORKED'); var self = this; child.exec('ls /dev | grep usb', function(err, stdout, stderr){ var usb = stdout.slice(0, -1).split('\n'), diff --git a/lib/joystick.js b/lib/joystick.js new file mode 100644 index 0000000..07d0367 --- /dev/null +++ b/lib/joystick.js @@ -0,0 +1,57 @@ +var events = require('events'), + util = require('util'); + /** + * Main JoyStick constructor + */ +/** + * Tell the board to set it up + * @param object options + */ + var JoyStick = function (options) { + + if (!options || !options.board) throw new Error('Must supply required options to JoyStick'); + this.board = options.board; + this.vrypin = options.pin || 'A0'; + this.vrxpin = options.pin || 'A1'; + this.board.pinMode(this.vrypin, 'in'); + this.board.pinMode(this.vrxpin, 'in'); + + var self = this; + + // Poll for x and y reads + setInterval(function () { + self.board.analogRead(self.vrypin); + self.board.analogRead(self.vrxpin); + }, 50); + + this.toPercentage = function(amount){ + return amount > 530 ? Math.ceil(((amount - 530) * 100) / 495) : Math.floor(( 530 - amount ) * 100 / 530); + }; + + // When data is received, parse inbound voltages and return events + this.board.on('data', function (m) { + m = m.slice(0, -1).split('::'); + + var amount = self.toPercentage(m[1]); + var real_voltage = m[1]; + + if (m.length > 1 && m[0] == self.vrypin) { + m[1] > 530 ? self.emit('y-up', amount , real_voltage) : ''; + m[1] < 500 ? self.emit('y-down', amount, real_voltage) : ''; + } + if (m.length > 1 && m[0] == self.vrxpin) { + m[1] > 530 ? self.emit('x-up', amount, real_voltage) : ''; + m[1] < 500 ? self.emit('x-down', amount, real_voltage) : ''; + } + + }); + +} + + +/* + * EventEmitter, I choose you! + */ +util.inherits(JoyStick, events.EventEmitter); + +module.exports = JoyStick;