Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Last active April 14, 2016 16:58
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 rwaldron/7fc097a6058ffc6df0ed to your computer and use it in GitHub Desktop.
Save rwaldron/7fc097a6058ffc6df0ed to your computer and use it in GitHub Desktop.
Alternate Stop & Go bot programs
var keypress = require("keypress");
var Spark = require("spark-io");
var five = require("johnny-five");
var Sumobot = require("sumobot")(five);
keypress(process.stdin);
var board = new five.Board({
io: new Spark({
token: process.env.SPARK_TOKEN,
deviceId: process.env.SPARK_DEVICE
})
});
board.on("ready", function() {
console.log("Welcome to Sumobot Jr: Stop & Go!");
var bot = new Sumobot({
left: "D0",
right: "D1",
speed: 0.50
});
var red = new five.Led("D4");
var green = new five.Led("D3");
var leds = new five.Leds([red, green]);
// Maps key names to bot methods
var actions = {
up: "fwd",
down: "rev",
left: "left",
right: "right",
space: "stop"
};
// Ensure the bot is stopped
bot.stop();
leds.off();
// A bit of keypress ceremony ;)
process.stdin.resume();
process.stdin.setEncoding("utf8");
process.stdin.setRawMode(true);
process.stdin.on("keypress", function(ch, key) {
var action;
if (!key) {
return;
}
leds.off();
action = actions[key.name] || key.name;
if (action == "q") {
console.log("Quitting");
bot.stop();
setTimeout(process.exit, 500);
}
if (bot[action]) {
bot[action]();
if (action === "stop") {
red.on();
} else {
green.on();
}
}
});
});
var keypress = require("keypress");
var five = require("johnny-five");
var Sumobot = require("sumobot")(five);
keypress(process.stdin);
var board = new five.Board();
board.on("ready", function() {
console.log("Welcome to Sumobot Jr: Stop & Go!");
var bot = new Sumobot({
left: 9,
right: 10,
speed: 0.50
});
var red = new five.Led(6);
var green = new five.Led(5);
var leds = new five.Leds([red, green]);
// Maps key names to bot methods
var actions = {
up: "fwd",
down: "rev",
left: "left",
right: "right",
space: "stop"
};
// Ensure the bot is stopped
bot.stop();
leds.off();
// A bit of keypress ceremony ;)
process.stdin.resume();
process.stdin.setEncoding("utf8");
process.stdin.setRawMode(true);
process.stdin.on("keypress", function(ch, key) {
var action;
if (!key) {
return;
}
leds.off();
action = actions[key.name] || key.name;
if (action == "q") {
console.log("Quitting");
bot.stop();
setTimeout(process.exit, 500);
}
if (bot[action]) {
bot[action]();
if (action === "stop") {
red.on();
} else {
green.on();
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment