Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwaldron/0e34f40e65c60530ea14 to your computer and use it in GitHub Desktop.
Save rwaldron/0e34f40e65c60530ea14 to your computer and use it in GitHub Desktop.
var five = require("johnny-five");
var Spark = require("spark-io");
var keypress = require("keypress");
var board = new five.Board({
io: new Spark({
token: "{YOURS}",
deviceId: "{YOURS}"
})
});
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 = new five.Servo.Continuous({ pin: "D0" }),
var right = new five.Servo.Continuous({ pin: "D1", isInverted: true });
var wheels = new five.Servos([ left, right ]);
var led = new five.Led("D7");
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");
wheels.ccw();
} else if ( key.name == "l" ) {
led.on();
} else if ( key.name == "down" ) {
console.log("Backward");
wheels.cw();
} else if ( key.name == "left" ) {
console.log("Left");
left.cw();
right.cw();
} else if ( key.name == "right" ) {
console.log("Right");
left.cw();
right.cw();
} else if ( key.name == "space" ) {
console.log("Stopping");
wheels.stop();
led.off();
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment