Skip to content

Instantly share code, notes, and snippets.

@garrows
Created July 16, 2014 14:48
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 garrows/5997a9218086e2586a54 to your computer and use it in GitHub Desktop.
Save garrows/5997a9218086e2586a54 to your computer and use it in GitHub Desktop.
Sumo.js
var five = require("johnny-five");
board = new five.Board({
// port: "/dev/rfcomm1"
port: "/dev/tty.NodeBot-DevB"
});
board.on("ready", function() {
(new five.Led(13)).strobe(500);
var motorLeft = new five.Motor({
pins: {
pwm: 10,
dir: 8
}
});
var motorRight = new five.Motor({
pins: {
pwm: 9,
dir: 7
}
});
var distance = new five.IR.Distance({
device: "GP2Y0A21YK", //aka Sharp 2Y0A21
pin: "A1",
freq: 200
});
var reversing = false;
var reversingTimeout = null;
distance.on("data", function() {
if (!reversing) {
if (this.cm < 75 && this.cm > 10) {
motorLeft.reverse((this.cm * 2) + 50);
motorRight.reverse((this.cm * 2) + 50);
} else if (this.cm < 10) {
motorLeft.forward(255);
motorRight.forward(50);
reversing = true;
clearTimeout(reversingTimeout);
reversingTimeout = setTimeout(function() {
reversing = false;
}, 1000);
} else {
motorLeft.reverse(255);
motorRight.reverse(255);
}
}
// console.log("cm: ", this.cm, this.raw, this.value);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment