Skip to content

Instantly share code, notes, and snippets.

@marcmerlin
Created January 14, 2015 18:07
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 marcmerlin/11fdf0c9e049b8ce6430 to your computer and use it in GitHub Desktop.
Save marcmerlin/11fdf0c9e049b8ce6430 to your computer and use it in GitHub Desktop.
Marc's mybot2.js: LEDs show distance from obstacle and bot turns, reverses or stops depending on obstacle distance
// =======================
// Derived from the work done by @makenai on the
// SumoBot Jr
// =======================
var five = require("johnny-five");
var keypress = require('keypress');
var pixel = require("../../lib/pixel.js");
var RSTOPVAL = 93;
var LSTOPVAL = 93;
var distance = 0;
var opts = {};
var state = 1;
opts.port = process.argv[2] || "";
keypress(process.stdin);
var board = new five.Board(opts);
board.on("ready", function() {
console.log("Control the bot with the arrow keys, and SPACE to stop.")
var ping = new five.Ping(8);
var left_wheel = new five.Servo({ pin: 9, type: 'continuous' });
var right_wheel = new five.Servo({ pin: 10, type: 'continuous' });
ping.on("change", function( err, value ) {
console.log('Object is ' + this.cm + ' cm away');
distance = this.cm;
});
// ---------------------------------------------------------------
strip = new pixel.Strip({
data: 6,
length: 4,
board: this
});
var pos = 0;
var colors = ["red", "green", "blue", "yellow", "cyan", "magenta", "white"];
var current_color = 0;
var blinker = setInterval(function() {
strip.color("#000"); // blanks it out
current_color = parseInt(distance/14);
for (pos = 0; pos < strip.stripLength(); pos++ ) {
strip.pixel(pos).color(colors[current_color]);
}
if (current_color == 1) {
left_wheel.to(LSTOPVAL);
right_wheel.to(RSTOPVAL);
}
else if (current_color > 1 && state == 0) {
console.log('Forward');
state = 1;
left_wheel.cw();
right_wheel.ccw();
}
if (current_color == 0) {
console.log('Backward');
left_wheel.ccw();
right_wheel.cw();
state = -1;
} else if (state == -1) {
state = 0;
console.log('Left');
left_wheel.ccw();
right_wheel.ccw();
}
strip.show();
}, 1000/2);
// ---------------------------------------------------------------
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.to(LSTOPVAL);
right_wheel.to(RSTOPVAL);
}
});
});
board.on("error", function(err) {
console.log(err.message);
process.exit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment