Skip to content

Commit

Permalink
Update johnny-five, include throttle, servo examples
Browse files Browse the repository at this point in the history
  • Loading branch information
HipsterBrown committed Aug 12, 2019
1 parent 129404e commit 7deb5d8
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 5 deletions.
11 changes: 8 additions & 3 deletions code/blink.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
const five = require('johnny-five');
const {Board, Led} = require('johnny-five');

const board = new five.Board({
const board = new Board({
port: '', // path to bluetooth connection, i.e. /dev/tty.ROBOT_NAME-SPPDev or COMX
});

board.on('ready', () => {
const led = new five.Led(13); // use built-in led on Arduino
const led = new Led(13); // use built-in led on Arduino
led.blink();
});

board.on('error', error => {
console.error(error);
process.exit(1);
});
3 changes: 1 addition & 2 deletions code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"description": "",
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"johnny-five": "^0.15.0"
"johnny-five": "1.3.0"
}
}
73 changes: 73 additions & 0 deletions code/throttle-servo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const {Board, ESC, Fn, Led, Servo} = require('johnny-five');
const keypress = require('keypress');

const board = new Board({
port: '', // path to bluetooth connection, i.e. /dev/tty.ROBOT_NAME-SPPDev or COMX
});

board.on('ready', () => {
const led = new Led(13);
const esc = new ESC({
device: 'FORWARD_REVERSE',
pin: 11,
});
const servo = new five.Servo(10);
let speed = 0;
let last = null;

servo.center();

// just to make sure the program is running
led.blink(500);

function controller(_, key) {
let change = 0;

if (key) {
if (!key.shift) {
change = esc.neutral;
speed = 0;
} else {
if (key.name === 'up' || key.name === 'down') {
if (last !== key.name) {
change = esc.neutral;
speed = 0;
} else {
speed += 1;

change =
key.name === 'up' ? esc.neutral + speed : esc.neutral - speed;
}
last = key.name;
}

if (key.name === 'left') {
servo.to(45, 1000);
}

if (key.name === 'right') {
servo.to(135, 1000);
}

if (key.name === 'c') {
servo.center();
}
}

if (change) {
esc.throttle(change);
}
}
}

keypress(process.stdin);

process.stdin.on('keypress', controller);
process.stdin.setRawMode(true);
process.stdin.resume();
});

board.on('error', error => {
console.error(error);
process.exit(1);
});
58 changes: 58 additions & 0 deletions code/throttle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const {Board, ESC, Fn, Led} = require('johnny-five');
const keypress = require('keypress');

const board = new Board({
port: '', // path to bluetooth connection, i.e. /dev/tty.ROBOT_NAME-SPPDev or COMX
});

board.on('ready', () => {
const led = new Led(13);
const esc = new ESC({
device: 'FORWARD_REVERSE',
pin: 11,
});
let speed = 0;
let last = null;

// just to make sure the program is running
led.blink(500);

function controller(_, key) {
let change = 0;

if (key) {
if (!key.shift) {
change = esc.neutral;
speed = 0;
} else {
if (key.name === 'up' || key.name === 'down') {
if (last !== key.name) {
change = esc.neutral;
speed = 0;
} else {
speed += 1;

change =
key.name === 'up' ? esc.neutral + speed : esc.neutral - speed;
}
last = key.name;
}
}

if (change) {
esc.throttle(change);
}
}
}

keypress(process.stdin);

process.stdin.on('keypress', controller);
process.stdin.setRawMode(true);
process.stdin.resume();
});

board.on('error', error => {
console.error(error);
process.exit(1);
});

0 comments on commit 7deb5d8

Please sign in to comment.