Arduino Vader “Force Grip” remote car (a.k.a. smartphone virtual wheel)

The 3-wheel car is built with the Makeblock Robot Starter Kit. (Plus some Legos for style.) It has a Bluetooth module. It is controlled by the phone’s gyros via Bluetooth. You drive it like a steering wheel with two hands, or like Darth Vader with one hand.

1. Code for the smartphone below. I saved this as and transferred it to my phone (LG Optimus Dynamic). Then I execute/run it using SL4A from the phone itself:

If you use the code above, the part that you’ll need to tweak for your own build is the DroidGyroJoystick class. Particularly, the pitch/roll numbers my phone gives lying on one edge may be different from yours. The other things you might want to change are the tuning parameters. For example, I configured a “deadzone” with accelZero and turnZero so that small motions near “centered” position won’t count as joystick inputs.

(If you’re new to SL4A, I show how to get set up, as well as where the in “import android” above comes from, here.)

2. Arduino sketch for the robot car below. Sorry it’s unnecessarily complicated. It’s because it’s code for a 2-in-1 IR or Bluetooth + obstacle-avoidance car. If you connect only an IR sensor to Port 6, it will be IR-controlled. If you connect only a Bluetooth module to Port 7, it will be Bluetooth-controlled. Why did I do such a thing? Because I wanted to be able to switch remotes for my kids just by swapping modules, without uploading sketches.

Anyway, what’s important here is that in Bluetooth mode, it recognizes my silly custom command set (fwd, left, right, bwd, etc). These command strings are how the SL4A Python script on the phone instructs the car what to do, over Bluetooth. The other useful thing is that the Bluetooth commands let me specify the speed, in addition to direction. So the more you push your joystick forward, the faster it’ll go.

P.S. I just installed the WordPress “Crayon Syntax Highlighter” plugin. It prettifies blocks of code within ‹pre› tags, like above. I like it so far.