Skip to content

Instantly share code, notes, and snippets.

@psbots
Created January 21, 2014 18:44
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 psbots/8545737 to your computer and use it in GitHub Desktop.
Save psbots/8545737 to your computer and use it in GitHub Desktop.
Android Accelerometer controlled bot via WebSockets. For more info visit : http://psbots.blogspot.in/2014/01/open-web-and-open-hardware-android.html
var webSocket = require('ws'),
ws = new webSocket('ws://10.0.0.3:9000/ws'),
five = require('johnny-five'),
board = new five.Board();
board.on("ready", function() {
var xspeed, turnspeed, motors;
var i1=new five.Pin(8),
i2=new five.Pin(10),
//ea=new five.Pin(9),
i3=new five.Pin(5),
i4=new five.Pin(7);
//eb=new five.Pin(6);
xspeed = 255;
turnspeed = 100;
motors = {
a: new five.Motor(9),
b: new five.Motor(6)
};
function drive(dir) {
if (dir === "space") {
motors.a.stop(),motors.b.stop();
}
if (dir === "right") {
i3.low(),i4.high(),i1.high(),i2.low();
motors.a.start(turnspeed),motors.b.start(turnspeed);
}
if (dir === "left") {
i4.low(),i3.high(),i1.low(),i2.high();
motors.a.start(turnspeed),motors.b.start(turnspeed);
}
if (dir === "up") {
i3.low(),i4.high(),i1.low(),i2.high();
motors.a.start(xspeed),motors.b.start(xspeed);
}
if (dir === "down") {
i3.high(),i4.low(),i1.high(),i2.low();
motors.a.start(xspeed),motors.b.start(xspeed);
}
}
ws.on('message', function(data) {
sensor = data.split(" ");
if(sensor[3]>3.0)
drive("right");
else if(sensor[3]<-3.0)
drive("left");
else if(sensor[4]<-3.0)
drive("up");
else if(sensor[4]>3.0)
drive("down");
else
drive("space");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment