Skip to content

Instantly share code, notes, and snippets.

@ghtomcat
Created December 16, 2015 12:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghtomcat/2643efca0e03a46b3e9a to your computer and use it in GitHub Desktop.
Save ghtomcat/2643efca0e03a46b3e9a to your computer and use it in GitHub Desktop.
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