Skip to content

Instantly share code, notes, and snippets.

@ghtomcat ghtomcat/drive.js
Created Dec 16, 2015

Embed
What would you like to do?
var five = require('johnny-five');
var keypress = require('keypress');
var board = new five.Board();
board.on('ready', function() {
console.log('Welcome to Sumobot!');
console.log('Control the bot with the arrow keys, and SPACE to stop.');
var leftWheel= new five.Motor({
pins: {
pwm: 5,
dir: 6
},
invertPWM: true
});
var rightWheel = new five.Motor({
pins: {
pwm: 9,
dir: 10,
},
invertPWM: true
});
keypress(process.stdin);
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.setRawMode(true);
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.reverse(180);
rightWheel.forward(180);
} else if ( key.name === 'down' ) {
console.log('Backward');
leftWheel.forward(160);
rightWheel.reverse(160);
} else if ( key.name === 'left' ) {
console.log('Left');
leftWheel.reverse(160);
rightWheel.reverse(160);
} else if ( key.name === 'right' ) {
console.log('Right');
leftWheel.forward(160);
rightWheel.reverse(160);
} else if ( key.name === 'space' ) {
console.log('Stopping');
leftWheel.stop();
rightWheel.stop();
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.