Created
July 28, 2014 18:12
-
-
Save wlaurance/a5e5ecb0ddf42d910d62 to your computer and use it in GitHub Desktop.
node-bots script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ======================= | |
// Sumobot Jr demo program | |
// ======================= | |
var five = require("johnny-five"); | |
var keypress = require('keypress'); | |
keypress(process.stdin); | |
var board = new five.Board(); | |
board.on("ready", function() { | |
console.log("Welcome to Sumobot Jr!") | |
console.log("Control the bot with the arrow keys, and SPACE to stop.") | |
var left_wheel = new five.Servo({ pin: 9, type: 'continuous' }).stop(); | |
var right_wheel = new five.Servo({ pin: 10, type: 'continuous' }).stop(); | |
var perfect_angle = 100; | |
var attack_angle = 0; | |
var sweeper = new five.Servo({ pin: 6, type: 'standard' }).to(perfect_angle); | |
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'); | |
left_wheel.cw(); | |
right_wheel.ccw(); | |
} else if ( key.name == 'down' ) { | |
console.log('Backward'); | |
left_wheel.ccw(); | |
right_wheel.cw(); | |
} else if ( key.name == 'left' ) { | |
console.log('Left'); | |
left_wheel.ccw(); | |
right_wheel.ccw(); | |
} else if ( key.name == 'right' ) { | |
console.log('Right'); | |
left_wheel.cw(); | |
right_wheel.cw(); | |
} else if ( key.name == 'space' ) { | |
console.log('Stopping'); | |
left_wheel.stop(); | |
right_wheel.stop(); | |
} else if ( key.name == 's' ) { | |
sweeper.to(attack_angle); | |
} else if ( key.name == 'd' ) { | |
sweeper.to(perfect_angle); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment