Created
January 21, 2014 18:44
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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