Skip to content

Instantly share code, notes, and snippets.

@lynnaloo
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save lynnaloo/0dcded35d28a91250172 to your computer and use it in GitHub Desktop.

Select an option

Save lynnaloo/0dcded35d28a91250172 to your computer and use it in GitHub Desktop.

Revisions

  1. lynnaloo revised this gist Apr 2, 2015. 1 changed file with 22 additions and 16 deletions.
    38 changes: 22 additions & 16 deletions spark-sumo.js
    Original file line number Diff line number Diff line change
    @@ -2,20 +2,24 @@ var keypress = require("keypress");
    var Spark = require("spark-io");
    var five = require("johnny-five");
    var Sumobot = require("sumobot")(five);

    var dotenv = require('dotenv');

    // load the spark token and device id from .env file
    dotenv.load();

    keypress(process.stdin);

    var board = new five.Board({
    io: new Spark({
    token: process.env.SPARK_TOKEN,
    deviceId: process.env.SPARK_DEVICE_2
    deviceId: process.env.SPARK_DEVICE_ID
    })
    });

    board.on("ready", function() {

    console.log("Welcome to Sumobot Jr!");

    // Initialize a new Sumobot.
    // - Left Servo is attached to pin D0
    // - Right Servo is attached to pin D1
    @@ -26,7 +30,7 @@ board.on("ready", function() {
    right: "D1",
    speed: 0.50
    });

    // Maps key names to bot methods
    var actions = {
    up: "fwd",
    @@ -35,30 +39,32 @@ board.on("ready", function() {
    right: "right",
    space: "stop"
    };


    var mode;

    // Ensure the bot is stopped
    bot.stop();

    // 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, mode;
    var action;

    if (!key) {
    return;
    }

    action = actions[key.name] || key.name;

    if (action == "q") {
    console.log("Quitting");
    console.log("Shutting down");
    bot.stop();
    setTimeout(process.exit, 500);
    }

    if (bot[action]) {
    if (mode === action) {
    return;
  2. lynnaloo created this gist Mar 31, 2015.
    70 changes: 70 additions & 0 deletions spark-sumo.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    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_2
    })
    });

    board.on("ready", function() {

    console.log("Welcome to Sumobot Jr!");

    // Initialize a new Sumobot.
    // - Left Servo is attached to pin D0
    // - Right Servo is attached to pin D1
    // - Speed set to 0.50 (half of max speed)
    //
    var bot = new Sumobot({
    left: "D0",
    right: "D1",
    speed: 0.50
    });

    // 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();

    // 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, mode;

    if (!key) {
    return;
    }

    action = actions[key.name] || key.name;

    if (action == "q") {
    console.log("Quitting");
    bot.stop();
    setTimeout(process.exit, 500);
    }

    if (bot[action]) {
    if (mode === action) {
    return;
    }
    bot[action]();
    mode = action;
    }
    });
    });