Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
var keypress = require('keypress');
var five = require("johnny-five");
var EtherPortClient = require("etherport-client").EtherPortClient;
var board = new five.Board({
port: new EtherPortClient({
host: "xx.xx.xx.xx",
port: 3030
}),
timeout: 1e5,
repl: false
});
board.on("ready", function() {
console.log("READY!");
var leftWheel= new five.Motor({
pins: {
pwm: 15,
dir: 13
}
});
var rightWheel = new five.Motor({
pins: {
pwm: 14,
dir: 12
}
});
leftWheel.setPWM(15,0);
leftWheel.setPin(13,0);
rightWheel.setPWM(14,0);
rightWheel.setPin(12,0);
keypress(process.stdin);
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.setRawMode(true);
console.log("press a key");
process.stdin.on('keypress', function (ch, key) {
if ( !key ) { return; }
if ( key.name === 'q' ) {
console.log('Quitting');
process.exit();
} else if ( key.name === 'up' ) {
console.log('Forward');
leftWheel.setPWM(15,2000);
leftWheel.setPin(13,0);
rightWheel.setPWM(14,2000);
rightWheel.setPin(12,0);
} else if ( key.name === 'down' ) {
console.log('Backward');
leftWheel.setPWM(15,0);
leftWheel.setPin(13,1)
rightWheel.setPWM(14,0);
rightWheel.setPin(12,1);
} else if ( key.name === 'space' ) {
console.log('Stopping');
leftWheel.setPin(13,0);
leftWheel.setPWM(15,0);
rightWheel.setPin(12,0);
rightWheel.setPWM(14,0);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment